Date post: | 28-Nov-2014 |
Category: |
Technology |
Upload: | gianluca-arbezzano |
View: | 731 times |
Download: | 2 times |
http://twitter.com/GianArb 1
Queue system
[email protected] Gianluca Arbezzano
Gianluca Arbezzano@GianArbhttp://github.com/gianarb
http://twitter.com/GianArbGianluca Arbezzano 2
TODO
• Send a mail• Upload big file• Imports• Messagging• …
http://twitter.com/GianArbGianluca Arbezzano 3
Only one Role
You DON’T MAKE to user wait
http://twitter.com/GianArbGianluca Arbezzano 5
Queue System
• Queue• Producer• Consumer• Message
http://twitter.com/GianArbGianluca Arbezzano 6
Queue
ZendQueue\Queue
// Create an array queue adapter$adapter = new ArrayAdapter();
// Create a queue object$queue = new Queue('queue1', $adapter);
Adapter• Db• MongoDb• MongoCappedCollection• ArrayAdapter• TODO: Redis• TODO: RabbitMQ
http://twitter.com/GianArbGianluca Arbezzano 7
Message Queue
Message queues provide an asynchronous communications protocol, meaning that the sender and receiver of the message do not need to interact with the message queue at the same time.
Cit. Wikipedia
ZendQueue\Message\Message implement Zend\Stdlib\Message
http://twitter.com/GianArbGianluca Arbezzano 8
Producer• Send messages quickly
Consumer
• Receive messages• Usually it’s a worker that consumes one
message at time.
http://twitter.com/GianArbGianluca Arbezzano 9
Queue System, when?
Registration Query Mail
• Time-shifts job processing when the “CPU” is less busy• Distributed computing• In a web app allows to run tasks in backgroundreducing the http request time
http://twitter.com/GianArbGianluca Arbezzano 10
Workflow
Registration QuerySend
Message in queue
Send mail
Send mail
Send mail
Send mail
Send mailWORKER
http://twitter.com/GianArbGianluca Arbezzano 11
Worker (old way)
use ZendQueue\Queue;
do {$messages = $queue->receive();//do some worksleep(1);
} while (…)
http://twitter.com/GianArbGianluca Arbezzano 14
Role
• Your adapter implement awaitMessage()• || your queue can emulate await
$adapter->awaitMessages(Queue $queue, $callback, ReceiveParameters $params = null)
http://twitter.com/GianArbGianluca Arbezzano 16
Worker
use ZendQueue\Queue;use ZendQueue\QueueEvent;
$queue->getEventManager()->attach(QueueEvent::EVENT_RECEIVE, function (QueueEvent $e) use ($queue) { $message = $e->getMessages()->current(); //do some work $queue->delete($message);});
//Wait for incoming messages$queue->await();
http://twitter.com/GianArbGianluca Arbezzano 17
Use case overview
• Short run jobs• Long run jobs• Schedule jobs (like cron jobs)
• Enterprise service bus
http://twitter.com/GianArbGianluca Arbezzano 18
Links!
• Use casehttps://github.com/ripaclub/ZendQueue/wiki/Example-Usage
• Proposalhttps://github.com/ripaclub/ZendQueue/wiki/ZendQueue-Proposal
http://twitter.com/GianArbGianluca Arbezzano 19
Ripa Club
Powered byhttp://github.com/ripaclub