-
-
Notifications
You must be signed in to change notification settings - Fork 273
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 #58 from Koed00/dev
Adds Amazon SQS broker
- Loading branch information
Showing
14 changed files
with
241 additions
and
15 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
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,65 @@ | ||
from django_q.conf import Conf | ||
from django_q.brokers import Broker | ||
from boto3 import Session | ||
|
||
|
||
class Sqs(Broker): | ||
def __init__(self, list_key=Conf.PREFIX): | ||
self.sqs = None | ||
super(Sqs, self).__init__(list_key) | ||
self.queue = self.get_queue() | ||
|
||
def enqueue(self, task): | ||
response = self.queue.send_message(MessageBody=task) | ||
return response.get('MessageId') | ||
|
||
def dequeue(self): | ||
# sqs supports max 10 messages in bulk | ||
if Conf.BULK > 10: | ||
Conf.BULK = 10 | ||
t = None | ||
if len(self.task_cache) > 0: | ||
t = self.task_cache.pop() | ||
else: | ||
tasks = self.queue.receive_messages(MaxNumberOfMessages=Conf.BULK, VisibilityTimeout=Conf.RETRY) | ||
if tasks: | ||
t = tasks.pop() | ||
if tasks: | ||
self.task_cache = tasks | ||
if t: | ||
return t.receipt_handle, t.body | ||
|
||
def acknowledge(self, task_id): | ||
return self.delete(task_id) | ||
|
||
def queue_size(self): | ||
return int(self.queue.attributes['ApproximateNumberOfMessages']) | ||
|
||
def delete(self, task_id): | ||
message = self.sqs.Message(self.queue.url, task_id) | ||
message.delete() | ||
|
||
def fail(self, task_id): | ||
self.delete(task_id) | ||
|
||
def delete_queue(self): | ||
self.queue.delete() | ||
|
||
def purge_queue(self): | ||
self.queue.purge() | ||
|
||
def ping(self): | ||
return 'sqs' in self.connection.get_available_resources() | ||
|
||
def info(self): | ||
return 'AWS SQS' | ||
|
||
@staticmethod | ||
def get_connection(list_key=Conf.PREFIX): | ||
return Session(aws_access_key_id=Conf.SQS['aws_access_key_id'], | ||
aws_secret_access_key=Conf.SQS['aws_secret_access_key'], | ||
region_name=Conf.SQS['aws_region']) | ||
|
||
def get_queue(self): | ||
self.sqs = self.connection.resource('sqs') | ||
return self.sqs.create_queue(QueueName=self.list_key) |
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
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
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
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
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 |
---|---|---|
|
@@ -7,3 +7,4 @@ redis | |
psutil | ||
django-redis | ||
iron-mq | ||
boto3 |
Oops, something went wrong.