Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Latest commit

 

History

History
132 lines (104 loc) · 4.07 KB

2.Configuration.md

File metadata and controls

132 lines (104 loc) · 4.07 KB

Documentation - configuration

For queues and jobs, SlmQueue uses a plugin structure. Both the queue and a job are a plugin in the queue or job manager. You can pull the queue or job from this manager and base your logic around this structure. In essence, the plugin structure is similar to Laminas controllers or view helpers.

Queues

SlmQueue supports at this moment three types of queues: Beanstalkd, Doctrine and Amazon SQS. You can specify this type by using a factory from one of these systems. A queue is defined in the queue_manager key. In the following example, a queue named "default" is defined.

'slm_queue' => [
    'queue_manager' => [
        'factories' => [
            'default' => 'SlmQueueBeanstalkd\Factory\BeanstalkdQueueFactory'
        ],
    ],
],

If you get this queue from the queue manager, SlmQueue will configure the instance completely for you and you are ready to push jobs into the queue. In the following example, access to the queue in your own controller factory is demonstrated.

namespace MyModule\Factory;

use MyModule\Controller\MyController;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;

class MyControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $queueManager = $container->get('SlmQueue\Queue\QueuePluginManager');
        $queue = $queueManager->get('default');

        $controller = new MyController($queue);
        return $controller;
    }
}

With a plugin structure, it allows you to create multiple queues. For larger applications, it might be useful to define different types of queues for different types of tasks. Simple jobs can be pushed into a default queue, but difficult jobs can be pushed into another queue. The workers of that second queue will run on a larger separate machine, so they are processed more efficiently.

Jobs

Jobs are defined in a similar fashion. The job_manager key is available for this configuration.

'slm_queue' => [
    'job_manager' => [
        'factories' => [
            'MyModule\Job\SendEmailJob' => 'MyModule\Factory\SendEmailJobFactory',
        ],
        'invokables' => [
            'MyModule\Job\PrintHelloWorldJob' => 'MyModule\Job\PrintHelloWorldJob',
        ],
    ],
],

It is not required to use factories for all jobs. If a job does not need any dependency, you can define the job as an invokable. You can get the job via the job plugin manager, as shown below.

namespace MyModule\Controller;

use SlmQueue\Queue\QueueInterface;
use SlmQueue\Job\JobPluginManager;
use Laminas\Mvc\Controller\AbstractActionController;

class MyController extends AbstractActionController
{
    protected $queue;
    protected $jobManager;

    public function __construct(QueueInterface $queue, JobPluginManager $jobManager)
    {
        $this->queue      = $queue;
        $this->jobManager = $jobManager;
    }

    public function fooAction()
    {
        // Do some work

        $job = $this->jobManager->get('MyModule\Job\PrintHelloWorldJob');
        $this->queue->push($job);
    }
}

Jobs without job plugin manager

There is no need to define all the jobs you use in the job plugin manager. If the job can be invoked (no factory is defined) you can use the FQCN for the job in the job plugin manager to access it as well.

$job = $jobPluginManager->get('MyModule\Job\PrintHelloWorldJob');

Alternatively, in many cases the job manager is not even required to instantiate the job. You can push jobs into queues where you get the job by using the new keyword:

$job = new PrintHelloWorldJob;
$queue->push($job);

Navigation

Previous page: Introduction Next page: Jobs

  1. Introduction
  2. Configuration
  3. Jobs
  4. QueueAware
  5. Workers
  6. Events
  7. Worker management