CakePHP/Queue WorkerListener Example Class

Written by James McDonald

May 31, 2021

This is an example of implementing a WorkerListener class for the new CakePHP/Queue plugin as mentioned here => https://book.cakephp.org/queue/1/en/index.html#worker-events

Config array

// ...
'Queue' => [
    'default' => [
          // A DSN for your configured backend. default: null
          'url' => 'redis:',

          // The queue that will be used for sending messages. default: default
          // This can be overriden when queuing or processing messages
          'queue' => 'default',

          // The name of a configured logger, default: null
          'logger' => 'stdout',

          // The name of an event listener class to associate with the worker
          'listener' => \App\Listener\WorkerListener::class,
    ]
],
// ...

Put it in src/Listener

// src/Listener/WorkerListener.php

<?php
namespace App\Listener;

use Cake\Event\EventListenerInterface;
use Cake\Log\LogTrait;

class WorkerListener implements EventListenerInterface
{
    use LogTrait;
    
    public function implementedEvents(): array
    {
        return [
            'Processor.message.exception' => 'processorMessageException',
            'Processor.message.invalid' => 'processorMessageInvalid',
            'Processor.message.reject' => 'processorMessageReject',
            'Processor.message.success' => 'processorMessageSuccess',
            'Processor.maxIterations' => 'processorMaxIterations',
            'Processor.maxRuntime' => "processorMaxRuntime",
            'Processor.message.failure' => "processorMessageFailure",
            'Processor.message.seen' => 'processorMessageSeen',
            'Processor.message.start' => 'processorMessageStart',
        ];
    }

    public function processorMessageException($message, $exception)
    {
        $this->log(__METHOD__);
        $this->log(print_r($message, true));
        $this->log($exception);
    }

    public function processorMessageInvalid($message)
    {
        $this->log(__METHOD__);
    }

    public function processorMessageReject($message)
    {
        $this->log(__METHOD__);
    }

    public function processorMessageSuccess($message)
    {
        $this->log(__METHOD__);
    }
    public function processorMaxIterations()
    {
        $this->log(__METHOD__);
    }
    public function processorMaxRuntime()
    {
        $this->log(__METHOD__);
    }
    public function processorMessageFailure($message)
    {
        $this->log(__METHOD__);
    }
    public function processorMessageSeen($message)
    {
        $this->log(__METHOD__);
    }
    public function processorMessageStart($message)
    {
        $this->log(__METHOD__);
    }
}

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...