Tomahawk Worker Queue
A nice and simple PHP Worker Queue library
You can install Tomahawk Queue using composer:
composer require tomahawk/queue
First you need to create a new file called tomahawk.xml
You will need to configure the following things:
Below is an example:
<?xml version="1.0" encoding="UTF-8"?>
<tomahawk
storage="./storage"
bootstrap="./queue-bootstrap.php">
<workers>
<worker pidkey="emails" name="Emails" queues="emails" ></worker>
</workers>
</tomahawk>
Bootstrap example file
<?php
use Tomahawk\Queue\Application;
use Tomahawk\Queue\Storage\StorageInterface;
use Tomahawk\Queue\Storage\RedisStorage;
use Predis\Client;
use Pimple\Container;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Get the Autoloader
*/
require_once(__DIR__.'/vendor/autoload.php');
/**
* Set Default Timezone
*/
date_default_timezone_set('Europe/London');
// Get the container
$container = Application::getContainer();
// Set storage for jobs
$container[StorageInterface::class] = function(Container $c) {
$client = new Client([
'scheme' => 'tcp',
'host' => '10.0.0.1',
'port' => 6379,
]);
return new RedisStorage($client);
};
$eventDispatcher = $container[EventDispatcherInterface::class];
// Add events
$eventDispatcher->addListener(\Tomahawk\Queue\JobEvents::PROCESSED, function(\Tomahawk\Queue\Event\PreProcessEvent $event) {
// Log to a file
});
$container[EventDispatcherInterface::class];
./bin/tomahawk-queue work emails emails --daemon
./bin/tomahawk-queue queue emails JobClass {"id":"1"}
./bin/tomahawk-queue list
./bin/tomahawk-queue stop emails
./bin/tomahawk-queue load
If you have your works setup on a different VM or server you can still push jobs onto the queue using the Queue Manager.
Below is an example of how to do this
<?php
use Predis\Client;
use Tomahawk\Queue\Manager;
use Tomahawk\Queue\Storage\RedisStorage;
$client = new Client([
'scheme' => 'tcp',
'host' => '10.0.0.1',
'port' => 6379,
]);
$storage = new RedisStorage($client);
$manager = new Manager($storage);
$arguments = [
'email' => '...',
'subject' => '...',
];
$manager->queue('queue_email', 'JobClass', $arguments);
Tomahawk Queue is open-sourced software licensed under the MIT license