-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from cuda-networks/process-queue-command
Process queue Django Command
Showing
5 changed files
with
97 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
boto3==1.3.1 | ||
Django==1.9.6 | ||
boto3==1.4.4 | ||
Django==1.10.6 | ||
mock==2.0.0 | ||
moto==0.4.24 | ||
redis==2.10.5 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import boto3 | ||
import logging | ||
|
||
from django.core.management import BaseCommand, CommandError | ||
|
||
from eb_sqs import settings | ||
from eb_sqs.worker.worker import Worker | ||
from eb_sqs.worker.worker_factory import WorkerFactory | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Command to process tasks from one or more SQS queues' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--queues', '-q', | ||
dest='queue_names', | ||
help='Name of queues to process, separated by commas') | ||
|
||
def handle(self, *args, **options): | ||
if not options['queue_names']: | ||
raise CommandError('Queue names (--queues) not specified') | ||
|
||
queue_names = [queue_name.rstrip() for queue_name in options['queue_names'].split(',')] | ||
|
||
logger.debug('[django-eb-sqs] Connecting to SQS: {}'.format(', '.join(queue_names))) | ||
|
||
sqs = boto3.resource('sqs', region_name=settings.AWS_REGION) | ||
queues = [sqs.get_queue_by_name(QueueName=queue_name) for queue_name in queue_names] | ||
|
||
logger.debug('[django-eb-sqs] Connected to SQS: {}'.format(', '.join(queue_names))) | ||
|
||
worker = WorkerFactory.default().create() | ||
|
||
while True: | ||
for queue in queues: | ||
messages = queue.receive_messages( | ||
MaxNumberOfMessages=settings.MAX_NUMBER_OF_MESSAGES, | ||
WaitTimeSeconds=settings.WAIT_TIME_S, | ||
) | ||
|
||
for msg in messages: | ||
self._process_message(msg, worker) | ||
|
||
def _process_message(self, msg, worker): | ||
# type: (Any, Worker) -> None | ||
logger.debug('[django-eb-sqs] Read message {}'.format(msg.message_id)) | ||
try: | ||
worker.execute(msg.body) | ||
logger.debug('[django-eb-sqs] Processed message {}'.format(msg.message_id)) | ||
except Exception as exc: | ||
logger.error('[django-eb-sqs] Unhandled error: {}'.format(exc), exc_info=1) | ||
finally: | ||
msg.delete() | ||
logger.debug('[django-eb-sqs] Deleted message {}'.format(msg.message_id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters