芹菜消费者SQS消息

时间:2014-02-15 17:49:22

标签: python google-app-engine amazon-web-services celery amazon-sqs

我是CelerySQS的新手,并希望用它来定期检查SQS中存储的消息,然后解雇消费者。消费者和Celery都位于EC2,而消息是使用GAE库从boto发送的。

目前,我很困惑:

  1. creating_msg_gae.py的邮件正文中,我应该放置哪些task信息?我认为这些信息将是我的celery task
  2. 的名称
  3. creating_msg_gae.py的邮件正文中,url被视为我的消费者(function do_something_url(url)中的tasks.py)要处理的参数?
  4. 目前,我在命令行中使用命令celery worker -A celery_c -l info运行芹菜,似乎芹菜定期检查SQS。我是否需要在PeriodicTask中创建Celery
  5. 我非常感谢有任何建议可以帮助我解决这个问题。

    creating_msg_gae.py

    from boto import sqs
    conn = sqs.connect_to_region("us-east-1",
                                 aws_access_key_id='aaa',
                                 aws_secret_access_key='bbb')
    my_queue = conn.get_queue('uber_batch')
    msg = {'properties': {'content_type': 'application/json', 
                                  'content_encoding': 'utf-8', 
                                  'body_encoding':'base64', 
                                  'delivery_tag':None, 
                                  'delivery_info': {'exchange':None, 'routing_key':None}},}
    body = {'id':'theid',
            ###########Question 1#######
            'task':'what task name I should put here?',
            'url':['my_s3_address']}
    msg.update({'body':base64.encodestring(json.dumps(body))})
    my_queue.write(my_queue.new_message(json.dumps(msg)))
    

    我的Celery文件系统如下:

    ./ce_folder/
                celery_c.py, celeryconfig.py, tasks.py, __init__.py
    

    celeryconfig.py

    import os
    BROKER_BACKEND = "SQS"
    AWS_ACCESS_KEY_ID = 'aaa'
    AWS_SECRET_ACCESS_KEY = 'bbb'
    os.environ.setdefault("AWS_ACCESS_KEY_ID", AWS_ACCESS_KEY_ID)
    os.environ.setdefault("AWS_SECRET_ACCESS_KEY", AWS_SECRET_ACCESS_KEY)
    BROKER_URL = 'sqs://'
    
    BROKER_TRANSPORT_OPTIONS = {'region': 'us-east-1'}
    BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 60} 
    BROKER_TRANSPORT_OPTIONS = {'polling_interval': 30}
    
    CELERY_DEFAULT_QUEUE = 'uber_batch'
    CELERY_DEFAULT_EXCHANGE = CELERY_DEFAULT_QUEUE
    CELERY_DEFAULT_EXCHANGE_TYPE = CELERY_DEFAULT_QUEUE
    CELERY_DEFAULT_ROUTING_KEY = CELERY_DEFAULT_QUEUE
    CELERY_QUEUES = {
        CELERY_DEFAULT_QUEUE: {
            'exchange': CELERY_DEFAULT_QUEUE,
            'binding_key': CELERY_DEFAULT_QUEUE,
        }
    }         
    

    celery_c.py

    from __future__ import absolute_import
    from celery import Celery
    
    app = Celery('uber')
    app.config_from_object('celeryconfig')
    
    if __name__ == '__main__':
        app.start()
    

    tasks.py

    from __future__ import absolute_import
    from celery_c import app
    
    @app.task
    def do_something_url(url):
        ..download file from url
        ..do some calculations 
        ..upload results files to s3 and return the result url###
        return result_url
    

0 个答案:

没有答案