One of the topics discussed in the community is the benefit of a unified, officially supported, CakePHP Queue plugin. Queues are used in most of the projects nowadays and are a central utility plugin.
During the CakeFest 2020 event, there were also a couple direct references from speakers: (https://speakerdeck.com/josegonzalez/building-and-releasing-a-cakephp-plugin/?slide=15c) and this comment from Mark Story: https://cakesf.slack.com/archives/C172CS4TE/p1602257791377500.
This motivated me to take a deeper look at the cakephp/queue plugin and write this blog post.
Here at CakeDC we've been using queues for a looooong time. Initially in CakePHP 2, we've used plugins like CakeResque with redis or custom workers tied to Amazon SQS queues. Then in CakePHP 3 & 4 we've been using mostly https://github.com/josegonzalez/cakephp-queuesadilla with redis or mongodb backends.
https://github.com/cakephp/queue
First thing would be setting up the plugin in your project, we are going to use the example project we used in CakeFest 2020: https://github.com/cakephp/cakefest2020/#using-docker
So after setting up the project and running it via docker compose, we can proceed to setup the plugin via composer. We will need to add it as a repository and set
Install via composer
After the first release it'll be much easier, but for now you'll need to add the package to your composer.json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/cakephp/queue.git"
}
]
Then do
composer require cakephp/queue -W
And install some transport as stated in the official documentation https://book.cakephp.org/queue/1/en/index.html#installation
composer require enqueue/redis:^0.9
composer require predis/predis:^1
Ensure your redis server is up and running, you can check the commands sent to your local redis server using redis-cli monitor
Now we are ready to configure the queue, we'll create 1 default queue adding this to the config/app.php file
'Queue' => [
'default' => [
'url' => 'redis:',
],
],
Add a Job using `bin/cake bake job Example` and add some code to the execute method
public function execute(Message $message): string
{
$data = $message->getArgument('data');
// do some long operation with data
Debugger::log($data);
sleep(2);
return Processor::ACK;
}
I've added a command utility to enqueue a test message bin/cake bake command addJob
public function execute(Arguments $args, ConsoleIo $io)
{
$callable = [ExampleJob::class, 'execute'];
$arguments = ['id' => 1, 'data' => ['some' => 'data']];
QueueManager::push($callable, $arguments);
}
And finally we can start our worker using bin/cake worker
to pull jobs from Redis and process them using the ExampleJob::execute method
Here's all the example code created: https://github.com/cakephp/cakefest2020/tree/feature/cakephp-queue - for your reference.
Please note the plugin is still a work in progress and there is no stable release as of now.
It's looking great so far and we plan to include it in our next projects!