2. About me
•
Jurian Sluiman
•
Founder @ soflomo (Delft, The Netherlands)
•
Web development (eCommerce, health care)
•
Zend Framework 2 contributor
•
Blogger: http://juriansluiman.nl
•
Twitter: @juriansluiman
4. Queue systems
•
Execute tasks in background
•
Return your response fast!
request
response
Server
(Producer)
Queue
•
Producer: piece of code pushing new jobs
•
Worker:
piece of code consuming jobs
Worker
21. Software-as-a-Service
1.Amazon Simple Queue Service (SQS)
•
Useful within EC2 instances
•
http://aws.amazon.com/sqs
2.IronMQ
•
They say they're better than SQS
•
http://iron.io/mq
26. SlmQueue
•
Supports Beanstalkd, SQS and Doctrine
•
Redis and Gearman support coming
•
Integrated with ZF2, but not required
•
Packagist: slm/queue + slm/queue-beanstalkd
•
GitHub: http://github.com/juriansluiman/SlmQueue
30. Workers
Run via ZF2 app
php public/index.php queue beanstalkd default
Configuration
•
Time-out for blocking calls
•
Maximum number of cycles
•
Maximum memory usage
•
Signal handlers for SIGTERM and SIGINT
31. Dependency injection
MyModuleJobEmail
class Email extends AbstractJob
{
protected $transport;
public function __construct(Transport $transport)
{
$this->transport = $transport;
}
public function execute()
{
$payload = $this->getContent();
$message = new Message;
$message->setTo($payload['to']);
$message->setMessage($payload['message']);
}
}
$this->transport->send($message);
32. Dependency injection
MyModuleFactoryEmailJobFactory
use MyModuleJobEmail as EmailJob;
class EmailJobFactory implements FactoryInterface
{
public function createService(ServiceLocator $sl)
{
$transport = $sl->get('MyEmailTransport');
return new EmailJob($transport);
}
}
module.config.php
'slm_queue' => [
'job_manager' => [
'MyEmailJob' => 'MyModuleFactoryEmailJobFactory'
]
]
34. Queue aware jobs
MyModuleJobFoo
class Foo extends AbstractJob implements QueueAwareInterface
{
use QueueAwareTrait;
public function execute()
{
// work here
}
}
$job = new BarJob();
$this->getQueue()->push($job);
37. Jobs in services
MyModuleServiceFoo
class Foo
{
protected $queue;
public function __construct(QueueInterface $queue)
{
$this->queue = $queue;
}
public function doSomething()
{
// work here
$job = new BarJob;
$this->queue->push($job);
}
}
38. Lazy services
class Buzzer
{
public function __construct()
{
sleep(5);
}
}
public function buzz()
{
// do something
}
Lazy services with a Proxy pattern by Marco Pivetta (Ocramius)
39. Lazy services
class BuzzerProxy extends Buzzer
{
private $sl;
private $instance;
public function __construct(ServiceLocator $sl)
{
$this->sl = $sl;
}
private function initialize()
{
$this->initialized = true;
$this->original = $this->sl->get('Buzzer');
}
}
public function buzz()
{
if (!$this->initialized) { $this->initialize(); }
return $this->instance->buzz();
}