Skip to content

Instantly share code, notes, and snippets.

@sergiors
Last active January 13, 2016 01:37
Show Gist options
  • Save sergiors/370cc931485d101ed84a to your computer and use it in GitHub Desktop.
Save sergiors/370cc931485d101ed84a to your computer and use it in GitHub Desktop.

sergiors/worker

composer require sergiors/worker "dev-master"
use Inbep\Inbep\Worker\Command\MessageCommand;
use Inbep\Inbep\Provider\WorkerServiceProvider;

//...

$app->register(new WorkerServiceProvider());

$command = new MessageCommand();
$command->setFrom('[email protected]', 'Bugao amigo');
$command->setTo('[email protected]', 'Sérgio');
$command->setSubject('Run to the hills');
$command->setBody('huehuea');

$app['worker']->put($command);
<?php
namespace Inbep\Inbep\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Sergiors\Worker\Command\CommandInterface;
use Sergiors\Worker\Command\Invoker;
class DaemoniseCommand extends Command
{
protected function configure()
{
$this
->setName('daemon:daemonise')
->setDescription('Starts the daemon to run commands.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getHelper('container')->getContainer();
/** @var \Sergiors\Worker\WorkerInterface $worker */
$worker = $container['worker'];
$invoker = new Invoker($container);
$worker->run(function (CommandInterface $command) use ($invoker) {
try {
$invoker->setCommand($command);
$invoker->run();
} catch (\Exception $e) {
}
});
}
}
<?php
namespace Inbep\Inbep\Worker\Command;
use Sergiors\Worker\Command\MessageCommandInterface;
/**
* @author Sérgio Rafael Siqueira <[email protected]>
*/
class MessageCommand implements MessageCommandInterface
{
/**
* @var \Pimple
*/
private $container;
/**
* @var array
*/
private $from = [];
/**
* @var array
*/
private $to = [];
/**
* @var string
*/
private $subject;
/**
* @var string
*/
private $body;
/**
* {@inheritdoc}
*/
public function setContainer(\Pimple $container)
{
$this->container = $container;
}
/**
* {@inheritdoc}
*/
public function setFrom($address, $name = null)
{
$this->from = [
'address' => $address,
'name' => $name
];
}
/**
* {@inheritdoc}
*/
public function setTo($address, $name = null)
{
$this->to = [
'address' => $address,
'name' => $name
];
}
/**
* {@inheritdoc}
*/
public function setSubject($subject)
{
$this->subject = $subject;
}
/**
* {@inheritdoc}
*/
public function setBody($body)
{
$this->body = $body;
}
public function execute()
{
if (!isset($this->from['address']) || !isset($this->to['address'])) {
throw new \RuntimeException('You must add address for "from" and "to".');
}
$message = [
'text' => $this->body,
'subject' => $this->subject,
'from_email' => $this->from['address'],
'from_name' => $this->from['name'],
'to' => [
[
'email' => $this->to['address'],
'name' => $this->to['name'],
'type' => 'to',
]
]
];
/** @var \Mandrill_Messages */
return $this->container['mandrill.messages']->send($message);
}
}
<?php
namespace Inbep\Inbep\Worker;
use Pheanstalk\PheanstalkInterface;
use Sergiors\Worker\WorkerInterface;
use Sergiors\Worker\Command\CommandInterface;
class PheanstalkWorker implements WorkerInterface
{
private $pheanstalk;
public function __construct(PheanstalkInterface $pheanstalk)
{
$this->pheanstalk = $pheanstalk;
}
public function put(CommandInterface $command)
{
$this->pheanstalk->put(serialize($command));
}
public function run(\Closure $callable)
{
$queue = $this->pheanstalk;
while ($job = $queue->reserve()) {
$command = unserialize($job->getData());
if (false === $command instanceof CommandInterface) {
continue;
}
$queue->delete($job);
call_user_func($callable, $command);
}
}
}
<?php
namespace Inbep\Inbep\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Inbep\Inbep\Worker\PheanstalkWorker;
class WorkerServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['worker'] = $app->share(function (Application $app) {
return new PheanstalkWorker($app['pheanstalk']);
});
}
public function boot(Application $app)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment