Saturday, 11 May 2024

Sync & Queued Message Handling in Eccube

1. PHP test connect to Redis

need install "predis/predis"

use Predis\Client;
// Redis server configuration
$host = 'redis';
$port = 6379;
$password = 'secret_redis'; // Replace 'your_password' with your actual password

// Connect to Redis server
try {
$redis = new Client([
'scheme' => 'tcp',
'host' => $host,
'port' => $port,
'password'=> $password
]);
$redis->ping();
echo "Connected to Redis server successfully.";
} catch (Exception $e) {
echo "Failed to connect to Redis server: " . $e->getMessage();
}
 }

2. File /app/config/eccube/packages/messenger.yaml

framework:
messenger:
# reset services after consuming messages
reset_on_message: true

# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
# failure_transport: failed

transports:
# https://symfony.com/doc/current/messenger.html#transport-configuration
async:
dsn: 'redis://:secret_redis@redis:6379/messages'

routing:
# async is whatever name you gave your transport above
'Customize\Message\SmsNotification': async

# php bin/console debug:messenger
# php bin/console messenger:consume async

And in .env add:

MESSENGER_TRANSPORT_DSN='redis://:{redis_password}@redis:6379/messages'

MESSENGER_TRANSPORT_DSN='redis://:secret_redis@redis:6379/messages'

3. /app/Customize/Message/SmsNotification.php 

<?php

namespace Customize\Message;

class SmsNotification
{
private $content;

public function __construct(string $content)
{
$this->content = $content;
}

public function getContent(): string
{
return $this->content;
}
}

4. /app/Customize/MessageHandler/SmsNotificationHandler.php

<?php
namespace Customize\MessageHandler;

use Customize\Message\SmsNotification;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
class SmsNotificationHandler
{
public function __invoke(SmsNotification $message)
{
log_error('HELLO SmsNotificationHandler');
dump($message);
}
}

5. DemoController.php

<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\Controller;

use Customize\Message\SmsNotification;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;

class TopController extends AbstractController
{
/**
* @Route("/demo", name="demo", methods={"GET"})
*/
public function demo(MessageBusInterface $bus)
{
// will cause the SmsNotificationHandler to be called
$bus->dispatch(new SmsNotification('Look! I created a message!'));
        echo 'DEMO CALL SmsNotification';
 
return new Response(
'',
Response::HTTP_OK,
array('Content-Type' => 'text/plain; charset=utf-8')
);
}
}

* use command

# use to list all jobs
php bin/console debug:messenger
 
# use to run job; add -vv to see details 
php bin/console messenger:consume async
 

Reference:

https://symfony.com/doc/current/messenger.html

Thank you

No comments:

Post a Comment

Golang Advanced Interview Q&A