Django Background Tasks for Amazon Elastic Beanstalk.
Created by Alexey "DataGreed" Strelkov.
django-eb-sqs-worker lets you handle background jobs on Elastic Beanstalk Worker Environment sent via SQS and provides methods to send tasks to worker.
You can use the same Django codebase for both your Web Tier and Worker Tier environments and send tasks from Web environment to Worker environment. Amazon fully manages autoscaling for you.
Tasks are sent via Amazon Simple Queue Service and are delivered to your worker with Elastic Beanstalk's SQS daemon. Periodic tasks are also supported.
Here's the diagram of how tasks move through the system, tasks movement is represented by arrows:
Install using pip #TODO: publish on pip
pip install -e git+git//github.com/DataGreed/django-eb-sqs-worker.git#egg=django-eb-sqs-worker
pip install django-eb-sqs-worker
Add eb_sqs_worker
to settings.INSTALLED_APPS
:
INSTALLED_APPS = [
# ...
"eb_sqs_worker",
]
Add eb-sqs-worker urls to your project's main urls.py
module:
# urls.py
urlpatterns = [
# your url patterns
# ...
]
from eb_sqs_worker.urls import urlpatterns as eb_sqs_urlpatterns
urlpatterns += eb_sqs_urlpatterns
#TODO settings
#TODO urls
#TODO
#TODO
#TODO
#TODO
(add link to https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features-managing-env-tiers.html#worker-periodictasks), explain configuration
If set to True
, tasks will be accepted and handled on this instance. If set to False
, the URL for handling
tasks will return 404. Defaults to False
.
Important: set this to True
only on your Worker environment
Dictionary of enabled tasks. Routes task names to actual task methods.
E.g.:
AWS_EB_ENABLED_TASKS = {
# name used in serialization # path to actual method that does the job
"accounts_confirmation_email": "accounts.tasks.send_confirmation_email",
"analytics_track_event": "analytics.tasks.track_event"
}
Default Elastic Beanstalk Region. Use the one that your app id deployed in.
Name of the queue used by default.
Amazon Access Key Id, refer to the docs
Amazon Secret Access Key, refer to the docs
If set to true, all tasks will be run locally and synchronnously instead of being sent to SQS Queue. Defaults to False
Always set AWS_EB_HANDLE_SQS_TASKS=False
on Web Tier Environment so the tasks could not be spoofed!
Web Tier environments are typically used for hosting publici websites and can be accessed by anoyone on the Internet,
meaning that anyone can send any jobs to your site if you leave this option on on Web environment.
Worker environments can only be accessed internally, e.g. via SQS Daemon that POSTs, so AWS_EB_HANDLE_SQS_TASKS=True
should be set only on worker environments.
Use Elastic Beanstalk Environment properties to supply different setting files for Web and Worker environments. See also: docs on designating the Django settings
#TODO
#TODO
#TODO
#TODO
When developing on local machine it might be a good idea to set AWS_EB_RUN_TASKS_LOCALLY=True
, so all the tasks
that should normally be sent to queue will be executed locally on the same machine in sync mode. This lets you test
your actual task methods in integration tests.
Clone the repository.
git clone https://github.com/DataGreed/django-eb-sqs-worker.git
Install requirements (use python virtual environment)
cd django-eb-sqs-worker
pip install -r requirements.txt
Run tests
sh test.sh
If you would like to contribute, please make a Pull Request with the description of changes and add tests to cover these changes.
Feel free to open issues if you have any problems or questions with this package.
- take advantage of the new environment link feature
- decorators for easier setup
- add pickle serialization
Search tags
Django Elastic Beanstalk Worker Web Tier Asynchronous Jobs Background Tasks SQS