tomahawk / queue
Tomahawk Queue
0.2.0
2017-05-09 19:12 UTC
Requires
- php: >=7.0.0
- ext-pcntl: *
- ext-posix: *
- monolog/monolog: ~1.22
- pimple/pimple: ~3.0
- predis/predis: ~1.0
- psr/log: ~1.0.0
- symfony/console: ~3.1.0
- symfony/event-dispatcher: ~3.1.0
- symfony/process: ~3.1.0
Requires (Dev)
- phpunit/phpunit: ~5.7.0
README
一个简单优雅的 PHP 工作队列库
要求
- PHP 7.0+
- pcntl 扩展。
- posix 扩展。
安装
您可以使用 composer 安装 Tomahawk Queue
composer require tomahawk/queue
1. 设置配置
首先,您需要创建一个名为 tomahawk.xml
的新文件
您需要配置以下内容
- 存储目录 - 用于日志和 pid 文件
- 引导文件(可选)- 允许您添加事件监听器和扩展存储
- 您的工人
以下是一个示例
<?xml version="1.0" encoding="UTF-8"?> <tomahawk storage="./storage" bootstrap="./queue-bootstrap.php"> <workers> <worker pidkey="emails" name="Emails" queues="emails" /> </workers> </tomahawk>
2. 创建引导文件
引导示例文件
<?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];
使用 CLI
创建一个新工人
./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
使用队列管理器
如果您的工作设置在不同的 VM 或服务器上,您仍然可以使用队列管理器将工作推送到队列。
以下是如何做到这一点的示例
<?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 是开源软件,许可协议为 MIT 协议