项目作者: tomahawkphp

项目描述 :
Tomahawk Worker Queue
高级语言: PHP
项目地址: git://github.com/tomahawkphp/queue.git
创建时间: 2017-01-27T13:11:34Z
项目社区:https://github.com/tomahawkphp/queue

开源协议:

下载


Tomahawk Queue

A nice and simple PHP Worker Queue library

Requirements

  • PHP 7.0 +
  • pcntl extension.
  • posix extension.

Installation

You can install Tomahawk Queue using composer:

composer require tomahawk/queue

1. Setup configuration

First you need to create a new file called tomahawk.xml

You will need to configure the following things:

  • Storage directory - for logs and pid files
  • Bootstrap file (optional) - Allows you to add event listeners and extend storage
  • Your workers

Below is an example:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <tomahawk
  3. storage="./storage"
  4. bootstrap="./queue-bootstrap.php">
  5. <workers>
  6. <worker pidkey="emails" name="Emails" queues="emails" ></worker>
  7. </workers>
  8. </tomahawk>

2. Create Bootstrap file

Bootstrap example file

  1. <?php
  2. use Tomahawk\Queue\Application;
  3. use Tomahawk\Queue\Storage\StorageInterface;
  4. use Tomahawk\Queue\Storage\RedisStorage;
  5. use Predis\Client;
  6. use Pimple\Container;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. /**
  9. * Get the Autoloader
  10. */
  11. require_once(__DIR__.'/vendor/autoload.php');
  12. /**
  13. * Set Default Timezone
  14. */
  15. date_default_timezone_set('Europe/London');
  16. // Get the container
  17. $container = Application::getContainer();
  18. // Set storage for jobs
  19. $container[StorageInterface::class] = function(Container $c) {
  20. $client = new Client([
  21. 'scheme' => 'tcp',
  22. 'host' => '10.0.0.1',
  23. 'port' => 6379,
  24. ]);
  25. return new RedisStorage($client);
  26. };
  27. $eventDispatcher = $container[EventDispatcherInterface::class];
  28. // Add events
  29. $eventDispatcher->addListener(\Tomahawk\Queue\JobEvents::PROCESSED, function(\Tomahawk\Queue\Event\PreProcessEvent $event) {
  30. // Log to a file
  31. });
  32. $container[EventDispatcherInterface::class];

Using the CLI

Create a new worker

./bin/tomahawk-queue work emails emails --daemon

Queue a new job to worker

./bin/tomahawk-queue queue emails JobClass {"id":"1"}

List running workers

./bin/tomahawk-queue list

Stop a running worker

./bin/tomahawk-queue stop emails

Load and run all workers defined in configuration file

./bin/tomahawk-queue load

Using the Queue Manager

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

  1. <?php
  2. use Predis\Client;
  3. use Tomahawk\Queue\Manager;
  4. use Tomahawk\Queue\Storage\RedisStorage;
  5. $client = new Client([
  6. 'scheme' => 'tcp',
  7. 'host' => '10.0.0.1',
  8. 'port' => 6379,
  9. ]);
  10. $storage = new RedisStorage($client);
  11. $manager = new Manager($storage);
  12. $arguments = [
  13. 'email' => '...',
  14. 'subject' => '...',
  15. ];
  16. $manager->queue('queue_email', 'JobClass', $arguments);

License

Tomahawk Queue is open-sourced software licensed under the MIT license