-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" A QueueWorkCommand Command """ | ||
|
||
import inspect | ||
import pickle | ||
|
||
from cleo import Command | ||
|
||
from config import queue | ||
from masonite.exceptions import DriverLibraryNotFound | ||
|
||
|
||
def callback(ch, method, properties, body): | ||
from wsgi import container | ||
job = pickle.loads(body) | ||
if inspect.isclass(job): | ||
job = container.resolve(job) | ||
job.handle() | ||
ch.basic_ack(delivery_tag=method.delivery_tag) | ||
|
||
|
||
class QueueWorkCommand(Command): | ||
""" | ||
Start the queue worker | ||
queue:work | ||
{--c|channel=default : The channel to listen on the queue} | ||
{--f|fair : Send jobs to queues that have no jobs instead of randomly selecting a queue} | ||
""" | ||
|
||
def handle(self): | ||
try: | ||
import pika | ||
except ImportError: | ||
raise DriverLibraryNotFound( | ||
"Could not find the 'pika' library. Run pip install pika to fix this.") | ||
|
||
connection = pika.BlockingConnection(pika.URLParameters('amqp://{}:{}@{}:{}/%2F'.format( | ||
queue.DRIVERS['amqp']['username'], queue.DRIVERS['amqp']['password'], queue.DRIVERS['amqp']['host'], queue.DRIVERS['amqp']['port'], | ||
))) | ||
channel = connection.channel() | ||
|
||
channel.queue_declare(queue=self.option('channel'), durable=True) | ||
|
||
channel.basic_consume(callback, | ||
queue=self.option('channel')) | ||
if self.option('fair'): | ||
channel.basic_qos(prefetch_count=1) | ||
|
||
self.info(' [*] Waiting to process jobs on the "{}" channel. To exit press CTRL+C'.format( | ||
self.option('channel'))) | ||
channel.start_consuming() |
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,58 @@ | ||
""" Driver for AMQP support """ | ||
|
||
import pickle | ||
import threading | ||
|
||
from config import queue | ||
from masonite.contracts import QueueContract | ||
from masonite.drivers import BaseDriver | ||
from masonite.exceptions import DriverLibraryNotFound | ||
|
||
if 'amqp' in queue.DRIVERS: | ||
listening_channel = queue.DRIVERS['amqp']['channel'] | ||
else: | ||
listening_channel = 'default' | ||
|
||
|
||
class QueueAmqpDriver(QueueContract, BaseDriver): | ||
|
||
def __init__(self, Container): | ||
"""Queue AMQP Driver | ||
Arguments: | ||
Container {masonite.app.App} -- The application container. | ||
""" | ||
|
||
try: | ||
import pika | ||
self.pika = pika | ||
except ImportError: | ||
raise DriverLibraryNotFound( | ||
"Could not find the 'pika' library. Run pip install pika to fix this.") | ||
|
||
# Start the connection | ||
connection = self.pika.BlockingConnection( | ||
self.pika.ConnectionParameters('localhost') | ||
) | ||
|
||
# Get the channel | ||
self.channel = connection.channel() | ||
|
||
# Declare what queue we are working with | ||
self.channel.queue_declare(queue=listening_channel, durable=True) | ||
|
||
def push(self, *objects): | ||
"""Push objects onto the amqp stack. | ||
Arguments: | ||
objects {*args of objects} - This can be several objects as parameters into this method. | ||
""" | ||
|
||
for obj in objects: | ||
# Publish to the channel for each object | ||
self.channel.basic_publish(exchange='', | ||
routing_key=listening_channel, | ||
body=pickle.dumps(obj), | ||
properties=self.pika.BasicProperties( | ||
delivery_mode=2, # make message persistent | ||
)) |
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