You've already forked Php-Template
feat: implement queue system with consumer and message handling (#14)
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 3m1s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 3m16s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m13s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 3m5s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 3m11s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m51s
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 3m1s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 3m16s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m13s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 3m5s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 3m11s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m51s
Reviewed-on: #14 Co-authored-by: Ron Rise <ron@siteworxpro.com> Co-committed-by: Ron Rise <ron@siteworxpro.com>
This commit was merged in pull request #14.
This commit is contained in:
@@ -17,7 +17,7 @@ FROM php:8.4.14-alpine AS php
|
||||
# Move the production PHP configuration file to the default location
|
||||
RUN mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
|
||||
&& apk add libpq-dev linux-headers --no-cache \
|
||||
&& docker-php-ext-install pdo_pgsql sockets \
|
||||
&& docker-php-ext-install pdo_pgsql sockets pcntl \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
# Set the working directory to /app
|
||||
|
||||
@@ -19,7 +19,9 @@
|
||||
"predis/predis": "^v3.2.0",
|
||||
"siteworxpro/http-status": "0.0.2",
|
||||
"lcobucci/jwt": "^5.6",
|
||||
"adhocore/cli": "^1.9"
|
||||
"adhocore/cli": "^1.9",
|
||||
"robinvdvleuten/ulid": "^5.0",
|
||||
"monolog/monolog": "^3.9"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^12.4",
|
||||
|
||||
151
composer.lock
generated
151
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "f7dc2e6131715ed6eec2d9f851949b80",
|
||||
"content-hash": "f920b7224ee908f6a4270f200dbbca3a",
|
||||
"packages": [
|
||||
{
|
||||
"name": "adhocore/cli",
|
||||
@@ -976,6 +976,109 @@
|
||||
],
|
||||
"time": "2024-11-25T08:10:15+00:00"
|
||||
},
|
||||
{
|
||||
"name": "monolog/monolog",
|
||||
"version": "3.9.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Seldaek/monolog.git",
|
||||
"reference": "10d85740180ecba7896c87e06a166e0c95a0e3b6"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/10d85740180ecba7896c87e06a166e0c95a0e3b6",
|
||||
"reference": "10d85740180ecba7896c87e06a166e0c95a0e3b6",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1",
|
||||
"psr/log": "^2.0 || ^3.0"
|
||||
},
|
||||
"provide": {
|
||||
"psr/log-implementation": "3.0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"aws/aws-sdk-php": "^3.0",
|
||||
"doctrine/couchdb": "~1.0@dev",
|
||||
"elasticsearch/elasticsearch": "^7 || ^8",
|
||||
"ext-json": "*",
|
||||
"graylog2/gelf-php": "^1.4.2 || ^2.0",
|
||||
"guzzlehttp/guzzle": "^7.4.5",
|
||||
"guzzlehttp/psr7": "^2.2",
|
||||
"mongodb/mongodb": "^1.8",
|
||||
"php-amqplib/php-amqplib": "~2.4 || ^3",
|
||||
"php-console/php-console": "^3.1.8",
|
||||
"phpstan/phpstan": "^2",
|
||||
"phpstan/phpstan-deprecation-rules": "^2",
|
||||
"phpstan/phpstan-strict-rules": "^2",
|
||||
"phpunit/phpunit": "^10.5.17 || ^11.0.7",
|
||||
"predis/predis": "^1.1 || ^2",
|
||||
"rollbar/rollbar": "^4.0",
|
||||
"ruflin/elastica": "^7 || ^8",
|
||||
"symfony/mailer": "^5.4 || ^6",
|
||||
"symfony/mime": "^5.4 || ^6"
|
||||
},
|
||||
"suggest": {
|
||||
"aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
|
||||
"doctrine/couchdb": "Allow sending log messages to a CouchDB server",
|
||||
"elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client",
|
||||
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
|
||||
"ext-curl": "Required to send log messages using the IFTTTHandler, the LogglyHandler, the SendGridHandler, the SlackWebhookHandler or the TelegramBotHandler",
|
||||
"ext-mbstring": "Allow to work properly with unicode symbols",
|
||||
"ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)",
|
||||
"ext-openssl": "Required to send log messages using SSL",
|
||||
"ext-sockets": "Allow sending log messages to a Syslog server (via UDP driver)",
|
||||
"graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
|
||||
"mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)",
|
||||
"php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
|
||||
"rollbar/rollbar": "Allow sending log messages to Rollbar",
|
||||
"ruflin/elastica": "Allow sending log messages to an Elastic Search server"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "3.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Monolog\\": "src/Monolog"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jordi Boggiano",
|
||||
"email": "j.boggiano@seld.be",
|
||||
"homepage": "https://seld.be"
|
||||
}
|
||||
],
|
||||
"description": "Sends your logs to files, sockets, inboxes, databases and various web services",
|
||||
"homepage": "https://github.com/Seldaek/monolog",
|
||||
"keywords": [
|
||||
"log",
|
||||
"logging",
|
||||
"psr-3"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/Seldaek/monolog/issues",
|
||||
"source": "https://github.com/Seldaek/monolog/tree/3.9.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/Seldaek",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/monolog/monolog",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2025-03-24T10:02:05+00:00"
|
||||
},
|
||||
{
|
||||
"name": "nesbot/carbon",
|
||||
"version": "3.10.3",
|
||||
@@ -1819,6 +1922,52 @@
|
||||
],
|
||||
"time": "2025-08-12T14:04:38+00:00"
|
||||
},
|
||||
{
|
||||
"name": "robinvdvleuten/ulid",
|
||||
"version": "v5.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/robinvdvleuten/php-ulid.git",
|
||||
"reference": "5389c9a2ff020815cc1f2b840334fdcb84ae3f35"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/robinvdvleuten/php-ulid/zipball/5389c9a2ff020815cc1f2b840334fdcb84ae3f35",
|
||||
"reference": "5389c9a2ff020815cc1f2b840334fdcb84ae3f35",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^7.2|^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpbench/phpbench": "^1.0.0-alpha3",
|
||||
"phpunit/phpunit": "^8.5",
|
||||
"symfony/phpunit-bridge": "^5.1"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Ulid\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Robin van der Vleuten",
|
||||
"email": "robin@webstronauts.co"
|
||||
}
|
||||
],
|
||||
"description": "Universally Unique Lexicographically Sortable Identifier (ULID) implementation for PHP.",
|
||||
"homepage": "https://github.com/robinvdvleuten/php-ulid",
|
||||
"support": {
|
||||
"issues": "https://github.com/robinvdvleuten/php-ulid/issues",
|
||||
"source": "https://github.com/robinvdvleuten/php-ulid/tree/v5.0.0"
|
||||
},
|
||||
"time": "2020-12-06T19:13:21+00:00"
|
||||
},
|
||||
{
|
||||
"name": "siteworxpro/config",
|
||||
"version": "1.1.1",
|
||||
|
||||
32
config.php
32
config.php
@@ -48,5 +48,37 @@ return [
|
||||
'audience' => Env::get('JWT_AUDIENCE', 'my_audience'),
|
||||
'issuer' => Env::get('JWT_ISSUER', 'my_issuer'),
|
||||
'strict_validation' => Env::get('JWT_STRICT_VALIDATION', true, 'bool'),
|
||||
],
|
||||
|
||||
'queue' => [
|
||||
'broker' => Env::get('QUEUE_BROKER', 'redis'),
|
||||
|
||||
'broker_config' => [
|
||||
|
||||
'redis' => [
|
||||
'consumerGroup' => Env::get('QUEUE_REDIS_CONSUMER_GROUP', ''),
|
||||
],
|
||||
|
||||
'kafka' => [
|
||||
'brokers' => Env::get('QUEUE_KAFKA_BROKERS', 'localhost:9092'),
|
||||
'topic' => Env::get('QUEUE_KAFKA_TOPIC', 'my_topic'),
|
||||
],
|
||||
|
||||
'rabbitmq' => [
|
||||
'host' => Env::get('QUEUE_RABBITMQ_HOST', 'localhost'),
|
||||
'port' => Env::get('QUEUE_RABBITMQ_PORT', 5672, 'int'),
|
||||
'username' => Env::get('QUEUE_RABBITMQ_USERNAME', 'guest'),
|
||||
'password' => Env::get('QUEUE_RABBITMQ_PASSWORD', 'guest'),
|
||||
'vhost' => Env::get('QUEUE_RABBITMQ_VHOST', '/'),
|
||||
],
|
||||
|
||||
'sqs' => [
|
||||
'key' => Env::get('QUEUE_SQS_KEY', ''),
|
||||
'secret' => Env::get('QUEUE_SQS_SECRET', ''),
|
||||
'region' => Env::get('QUEUE_SQS_REGION', 'us-east-1'),
|
||||
'version' => Env::get('QUEUE_SQS_VERSION', 'latest'),
|
||||
'queue_url' => Env::get('QUEUE_SQS_QUEUE_URL', ''),
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
@@ -72,6 +72,8 @@ services:
|
||||
dockerfile: Dockerfile
|
||||
entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
||||
depends_on:
|
||||
migration-container:
|
||||
condition: service_completed_successfully
|
||||
traefik:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
|
||||
24
src/Annotations/Async/HandlesMessage.php
Normal file
24
src/Annotations/Async/HandlesMessage.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Annotations\Async;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
||||
readonly class HandlesMessage
|
||||
{
|
||||
public function __construct(
|
||||
public string $messageClass,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageClass(): string
|
||||
{
|
||||
return $this->messageClass;
|
||||
}
|
||||
}
|
||||
19
src/Async/Brokers/Broker.php
Normal file
19
src/Async/Brokers/Broker.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Brokers;
|
||||
|
||||
abstract class Broker implements BrokerInterface
|
||||
{
|
||||
public const array BROKER_TYPES = [
|
||||
'redis' => Redis::class,
|
||||
'rabbitmq' => RabbitMQ::class,
|
||||
'kafka' => Kafka::class,
|
||||
'sqs' => Sqs::class,
|
||||
];
|
||||
|
||||
public function __construct(protected $config = [])
|
||||
{
|
||||
}
|
||||
}
|
||||
21
src/Async/Brokers/BrokerInterface.php
Normal file
21
src/Async/Brokers/BrokerInterface.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Brokers;
|
||||
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
|
||||
interface BrokerInterface
|
||||
{
|
||||
public function publish(Queue $queue, Message $message, ?int $delay = null): void;
|
||||
|
||||
public function consume(Queue $queue): Message | null;
|
||||
|
||||
public function acknowledge(Queue $queue, Message $message): void;
|
||||
|
||||
public function reject(Queue $queue, Message $message, bool $requeue = false): void;
|
||||
|
||||
public function purge(Queue $queue): void;
|
||||
}
|
||||
36
src/Async/Brokers/Kafka.php
Normal file
36
src/Async/Brokers/Kafka.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Brokers;
|
||||
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
|
||||
class Kafka extends Broker
|
||||
{
|
||||
public function publish(Queue $queue, Message $message, ?int $delay = null): void
|
||||
{
|
||||
// TODO: Implement publish() method.
|
||||
}
|
||||
|
||||
public function consume(Queue $queue): Message | null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function acknowledge(Queue $queue, Message $message): void
|
||||
{
|
||||
// TODO: Implement acknowledge() method.
|
||||
}
|
||||
|
||||
public function reject(Queue $queue, Message $message, bool $requeue = false): void
|
||||
{
|
||||
// TODO: Implement reject() method.
|
||||
}
|
||||
|
||||
public function purge(Queue $queue): void
|
||||
{
|
||||
// TODO: Implement purge() method.
|
||||
}
|
||||
}
|
||||
36
src/Async/Brokers/RabbitMQ.php
Normal file
36
src/Async/Brokers/RabbitMQ.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Brokers;
|
||||
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
|
||||
class RabbitMQ extends Broker
|
||||
{
|
||||
public function publish(Queue $queue, Message $message, ?int $delay = null): void
|
||||
{
|
||||
// TODO: Implement publish() method.
|
||||
}
|
||||
|
||||
public function consume(Queue $queue): Message | null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function acknowledge(Queue $queue, Message $message): void
|
||||
{
|
||||
// TODO: Implement acknowledge() method.
|
||||
}
|
||||
|
||||
public function reject(Queue $queue, Message $message, bool $requeue = false): void
|
||||
{
|
||||
// TODO: Implement reject() method.
|
||||
}
|
||||
|
||||
public function purge(Queue $queue): void
|
||||
{
|
||||
// TODO: Implement purge() method.
|
||||
}
|
||||
}
|
||||
190
src/Async/Brokers/Redis.php
Normal file
190
src/Async/Brokers/Redis.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Brokers;
|
||||
|
||||
use Predis\Client;
|
||||
use Predis\Command\RawCommand;
|
||||
use Siteworxpro\App\Async\Messages\SayHelloMessage;
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
use Siteworxpro\App\Helpers\Ulid;
|
||||
|
||||
class Redis extends Broker
|
||||
{
|
||||
private Client $client;
|
||||
|
||||
private string $consumerId;
|
||||
|
||||
private string $consumerGroup;
|
||||
|
||||
private const string CONSUMER_ID_PREFIX = 'consumer-group:';
|
||||
private const string QUEUE_PREFIX = 'queue:';
|
||||
|
||||
private array $queueNames = [];
|
||||
|
||||
public function __construct($config = [])
|
||||
{
|
||||
parent::__construct($config);
|
||||
|
||||
$this->client = \Siteworxpro\App\Services\Facades\Redis::getFacadeRoot();
|
||||
$this->consumerId = php_uname('n') . ':' . getmypid();
|
||||
$this->consumerGroup = $config['consumerGroup'] ?? 'default';
|
||||
}
|
||||
|
||||
private function ensureQueue(string $queueName): void
|
||||
{
|
||||
if (in_array($queueName, $this->queueNames, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->client->executeCommand(
|
||||
new RawCommand(
|
||||
'XGROUP',
|
||||
[
|
||||
'CREATE',
|
||||
self::QUEUE_PREFIX . $queueName,
|
||||
self::CONSUMER_ID_PREFIX . $this->consumerGroup,
|
||||
'$',
|
||||
'MKSTREAM'
|
||||
]
|
||||
)
|
||||
);
|
||||
} catch (\Exception) {
|
||||
// If the group already exists, we catch the exception and ignore it
|
||||
// This is because Redis will throw an error if the group already exists
|
||||
// We can safely ignore this error as it means the group is already set up
|
||||
}
|
||||
|
||||
$this->client->executeCommand(
|
||||
new RawCommand(
|
||||
'XGROUP',
|
||||
[
|
||||
'CREATECONSUMER',
|
||||
self::QUEUE_PREFIX . $queueName,
|
||||
self::CONSUMER_ID_PREFIX . $this->consumerGroup,
|
||||
$this->consumerId
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$this->queueNames[] = $queueName;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
foreach ($this->queueNames as $queueName) {
|
||||
try {
|
||||
$this->client->executeCommand(
|
||||
new RawCommand(
|
||||
'XGROUP',
|
||||
[
|
||||
'DELCONSUMER',
|
||||
self::QUEUE_PREFIX . $queueName,
|
||||
self::CONSUMER_ID_PREFIX . $this->consumerGroup,
|
||||
$this->consumerId
|
||||
]
|
||||
)
|
||||
);
|
||||
} catch (\Exception) {
|
||||
// Ignore exceptions during cleanup
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function publish(Queue $queue, Message $message, ?int $delay = null): void
|
||||
{
|
||||
$command = '%s * data %s';
|
||||
$command = sprintf(
|
||||
$command,
|
||||
self::QUEUE_PREFIX .
|
||||
$queue->queueName(),
|
||||
base64_encode($message->serialize())
|
||||
);
|
||||
|
||||
/** @var string $result */
|
||||
$result = $this
|
||||
->client
|
||||
->executeCommand(
|
||||
new RawCommand('XADD', explode(' ', $command)),
|
||||
);
|
||||
|
||||
$message->setId($result);
|
||||
}
|
||||
|
||||
public function consume(Queue $queue): Message|null
|
||||
{
|
||||
$this->ensureQueue($queue->queueName());
|
||||
|
||||
$command = 'GROUP %s %s COUNT 1 STREAMS %s >';
|
||||
$command = sprintf(
|
||||
$command,
|
||||
self::CONSUMER_ID_PREFIX . $this->consumerGroup,
|
||||
$this->consumerId,
|
||||
self::QUEUE_PREFIX . $queue->queueName(),
|
||||
);
|
||||
|
||||
/** @var array | null $response */
|
||||
$response = $this
|
||||
->client
|
||||
->executeCommand(
|
||||
new RawCommand(
|
||||
'XREADGROUP',
|
||||
explode(' ', $command)
|
||||
)
|
||||
);
|
||||
|
||||
if ($response === null || !isset($response[0][1][0][1][1])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$messageData = base64_decode($response[0][1][0][1][1]);
|
||||
$messageId = $response[0][1][0][0];
|
||||
|
||||
if ($messageData === 'NOOP') {
|
||||
// If the message is a NOOP, we return null to indicate no actual message
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = unserialize($messageData, ['allowed_classes' => true]);
|
||||
if (!$value instanceof Message) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$value->setId($messageId);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function acknowledge(Queue $queue, Message $message): void
|
||||
{
|
||||
$response = $this
|
||||
->client
|
||||
->executeCommand(
|
||||
new RawCommand(
|
||||
'XACK',
|
||||
[
|
||||
self::QUEUE_PREFIX . $queue->queueName(),
|
||||
self::CONSUMER_ID_PREFIX . $this->consumerGroup,
|
||||
$message->getId()
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function reject(Queue $queue, Message $message, bool $requeue = false): void
|
||||
{
|
||||
// TODO: Implement reject() method.
|
||||
}
|
||||
|
||||
public function purge(Queue $queue): void
|
||||
{
|
||||
|
||||
// TODO: Implement purge() method.
|
||||
}
|
||||
}
|
||||
36
src/Async/Brokers/Sqs.php
Normal file
36
src/Async/Brokers/Sqs.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Brokers;
|
||||
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
|
||||
class Sqs extends Broker
|
||||
{
|
||||
public function publish(Queue $queue, Message $message, ?int $delay = null): void
|
||||
{
|
||||
// TODO: Implement publish() method.
|
||||
}
|
||||
|
||||
public function consume(Queue $queue): Message | null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function acknowledge(Queue $queue, Message $message): void
|
||||
{
|
||||
// TODO: Implement acknowledge() method.
|
||||
}
|
||||
|
||||
public function reject(Queue $queue, Message $message, bool $requeue = false): void
|
||||
{
|
||||
// TODO: Implement reject() method.
|
||||
}
|
||||
|
||||
public function purge(Queue $queue): void
|
||||
{
|
||||
// TODO: Implement purge() method.
|
||||
}
|
||||
}
|
||||
142
src/Async/Consumer.php
Normal file
142
src/Async/Consumer.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
declare(ticks=1);
|
||||
|
||||
namespace Siteworxpro\App\Async;
|
||||
|
||||
use Siteworxpro\App\Annotations\Async\HandlesMessage;
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Services\Facades\Logger;
|
||||
|
||||
class Consumer
|
||||
{
|
||||
private static bool $shutDown = false;
|
||||
|
||||
private const array QUEUES = [
|
||||
Queues\DefaultQueue::class,
|
||||
];
|
||||
|
||||
private array $queues = [];
|
||||
|
||||
private array $handlers = [];
|
||||
|
||||
private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\Async\\Handlers\\';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
foreach (self::QUEUES as $queueClass) {
|
||||
$this->queues[] = new $queueClass();
|
||||
}
|
||||
|
||||
$this->registerHandlers();
|
||||
}
|
||||
|
||||
private function registerHandlers(): void
|
||||
{
|
||||
$recursiveIterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator(__DIR__ . '/Handlers/')
|
||||
);
|
||||
|
||||
foreach ($recursiveIterator as $file) {
|
||||
if ($file->isFile() && $file->getExtension() === 'php') {
|
||||
$relativePath = str_replace(__DIR__ . '/Handlers/', '', $file->getPathname());
|
||||
$className = self::HANDLER_NAMESPACE . str_replace('/', '\\', substr($relativePath, 0, -4));
|
||||
|
||||
|
||||
if (class_exists($className)) {
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$attributes = $reflection->getAttributes(HandlesMessage::class);
|
||||
foreach ($attributes as $attribute) {
|
||||
$instance = $attribute->newInstance();
|
||||
$messageClass = $instance->getMessageClass();
|
||||
$this->handlers[$messageClass][] = $className;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $signal
|
||||
*/
|
||||
public static function handleSignal($signal): void
|
||||
{
|
||||
switch ($signal) {
|
||||
// Graceful
|
||||
case SIGINT:
|
||||
case SIGTERM:
|
||||
case SIGHUP:
|
||||
self::$shutDown = true;
|
||||
|
||||
break;
|
||||
|
||||
// Not Graceful
|
||||
case SIGKILL:
|
||||
exit(9);
|
||||
}
|
||||
}
|
||||
|
||||
private function shouldShutDown(): bool
|
||||
{
|
||||
return self::$shutDown;
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
if (!\function_exists('pcntl_signal')) {
|
||||
throw new \RuntimeException('The pcntl extension is required to handle signals.');
|
||||
}
|
||||
|
||||
Logger::info('Starting queue consumer...');
|
||||
|
||||
\pcntl_signal(SIGINT, [self::class, 'handleSignal']);
|
||||
\pcntl_signal(SIGTERM, [self::class, 'handleSignal']);
|
||||
\pcntl_signal(SIGHUP, [self::class, 'handleSignal']);
|
||||
|
||||
while (true) {
|
||||
if ($this->shouldShutDown()) {
|
||||
Logger::info('Shutting down queue consumer...');
|
||||
break;
|
||||
}
|
||||
|
||||
/** @var Queue $queue */
|
||||
foreach ($this->queues as $queue) {
|
||||
Logger::info('Listening to queue: ' . $queue->queueName());
|
||||
$message = $queue->pop();
|
||||
if ($message) {
|
||||
Logger::info('Processing message of type: ' . get_class($message));
|
||||
|
||||
$handlers = $this->getHandlerForMessage($message);
|
||||
|
||||
foreach ($handlers as $handler) {
|
||||
$handler($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
private function getHandlerForMessage($message): array
|
||||
{
|
||||
$callables = [];
|
||||
|
||||
$messageClass = get_class($message);
|
||||
if (isset($this->handlers[$messageClass])) {
|
||||
$handlerClasses = $this->handlers[$messageClass];
|
||||
|
||||
foreach ($handlerClasses as $handlerClass) {
|
||||
if (class_exists($handlerClass)) {
|
||||
$handlerInstance = new $handlerClass();
|
||||
|
||||
$callables[] = $handlerInstance;
|
||||
}
|
||||
}
|
||||
|
||||
return $callables;
|
||||
}
|
||||
|
||||
throw new \RuntimeException("No handler found for message class: $messageClass");
|
||||
}
|
||||
}
|
||||
12
src/Async/Handlers/HandlerInterface.php
Normal file
12
src/Async/Handlers/HandlerInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Handlers;
|
||||
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
|
||||
interface HandlerInterface
|
||||
{
|
||||
public function __invoke(Message $message): void;
|
||||
}
|
||||
21
src/Async/Handlers/SayHelloHandler.php
Normal file
21
src/Async/Handlers/SayHelloHandler.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Handlers;
|
||||
|
||||
use Siteworxpro\App\Annotations\Async\HandlesMessage;
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
use Siteworxpro\App\Async\Messages\SayHelloMessage;
|
||||
use Siteworxpro\App\Services\Facades\Logger;
|
||||
|
||||
#[HandlesMessage(SayHelloMessage::class)]
|
||||
class SayHelloHandler implements HandlerInterface
|
||||
{
|
||||
public function __invoke(Message | SayHelloMessage $message): void
|
||||
{
|
||||
$name = $message->getPayload()['name'] ?? 'Guest';
|
||||
|
||||
Logger::info(sprintf("Hello, %s!", $name));
|
||||
}
|
||||
}
|
||||
102
src/Async/Messages/Message.php
Normal file
102
src/Async/Messages/Message.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Messages;
|
||||
|
||||
use Siteworxpro\App\Async\Queues\DefaultQueue;
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Helpers\Ulid;
|
||||
|
||||
abstract class Message implements \Serializable
|
||||
{
|
||||
protected string $id = '';
|
||||
|
||||
protected string $uniqueId;
|
||||
|
||||
protected array $payload;
|
||||
|
||||
protected int $timestamp;
|
||||
|
||||
protected string $queue = '';
|
||||
|
||||
protected const string DEFAULT_QUEUE = DefaultQueue::class;
|
||||
|
||||
abstract public static function dispatch(...$args): void;
|
||||
|
||||
abstract public static function dispatchLater(int $delay, ...$args): void;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->uniqueId = Ulid::generate();
|
||||
$this->timestamp = time();
|
||||
}
|
||||
|
||||
protected function getQueue(): Queue
|
||||
{
|
||||
if ($this->queue === '') {
|
||||
$this->queue = static::DEFAULT_QUEUE;
|
||||
}
|
||||
|
||||
return new $this->queue();
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*/
|
||||
public function setId(string $id): void
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getPayload(): array
|
||||
{
|
||||
return $this->payload;
|
||||
}
|
||||
|
||||
public function getTimestamp(): int
|
||||
{
|
||||
return $this->timestamp;
|
||||
}
|
||||
|
||||
public function __serialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'payload' => $this->payload,
|
||||
'timestamp' => $this->timestamp,
|
||||
'queue' => $this->queue,
|
||||
];
|
||||
}
|
||||
|
||||
public function __unserialize(array $data): void
|
||||
{
|
||||
$this->id = $data['id'];
|
||||
$this->payload = $data['payload'];
|
||||
$this->timestamp = $data['timestamp'];
|
||||
$this->queue = $data['queue'];
|
||||
}
|
||||
|
||||
public function serialize(): string
|
||||
{
|
||||
return serialize($this);
|
||||
}
|
||||
|
||||
public function unserialize(string $data): Message
|
||||
{
|
||||
$unserializedData = unserialize($data, ['allowed_classes' => [Message::class]]);
|
||||
|
||||
$this->id = $unserializedData['id'];
|
||||
$this->uniqueId = $unserializedData['uniqueId'];
|
||||
$this->payload = $unserializedData['payload'];
|
||||
$this->timestamp = $unserializedData['timestamp'];
|
||||
$this->queue = $unserializedData['queue'];
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
41
src/Async/Messages/SayHelloMessage.php
Normal file
41
src/Async/Messages/SayHelloMessage.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Messages;
|
||||
|
||||
use Siteworxpro\App\Services\Facades\Broker;
|
||||
|
||||
class SayHelloMessage extends Message
|
||||
{
|
||||
public static function dispatch(...$args): void
|
||||
{
|
||||
$name = $args[0] ?? 'World';
|
||||
$message = new self($name);
|
||||
Broker::publish(
|
||||
$message->getQueue(),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
public static function dispatchLater(int $delay, ...$args): void
|
||||
{
|
||||
$name = $args[0] ?? 'World';
|
||||
$message = new self($name);
|
||||
Broker::publishLater(
|
||||
$message->getQueue(),
|
||||
$message,
|
||||
$delay
|
||||
);
|
||||
}
|
||||
|
||||
private function __construct(
|
||||
private readonly string $name
|
||||
) {
|
||||
parent::__construct();
|
||||
|
||||
$this->payload = [
|
||||
'name' => $this->name,
|
||||
];
|
||||
}
|
||||
}
|
||||
13
src/Async/Queues/DefaultQueue.php
Normal file
13
src/Async/Queues/DefaultQueue.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Queues;
|
||||
|
||||
readonly class DefaultQueue extends Queue
|
||||
{
|
||||
public function queueName(): string
|
||||
{
|
||||
return 'default';
|
||||
}
|
||||
}
|
||||
28
src/Async/Queues/Queue.php
Normal file
28
src/Async/Queues/Queue.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Async\Queues;
|
||||
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
use Siteworxpro\App\Services\Facades\Broker;
|
||||
|
||||
readonly abstract class Queue
|
||||
{
|
||||
abstract public function queueName(): string;
|
||||
|
||||
public function push(Message $message): void
|
||||
{
|
||||
Broker::publish($this, $message);
|
||||
}
|
||||
|
||||
public function later(int $delay, Message $message): void
|
||||
{
|
||||
Broker::publish($this, $message, $delay);
|
||||
}
|
||||
|
||||
public function pop(): Message | null
|
||||
{
|
||||
return Broker::consume($this);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ namespace Siteworxpro\App\Cli;
|
||||
|
||||
use Ahc\Cli\Application;
|
||||
use Siteworxpro\App\Cli\Commands\DemoCommand;
|
||||
use Siteworxpro\App\Cli\Commands\Queue\Start;
|
||||
use Siteworxpro\App\Kernel;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
|
||||
@@ -22,6 +23,7 @@ class App
|
||||
$this->app = new Application('Php-Template', Config::get('app.version') ?? 'dev-master');
|
||||
|
||||
$this->app->add(new DemoCommand());
|
||||
$this->app->add(new Start());
|
||||
}
|
||||
|
||||
public function run(): int
|
||||
|
||||
28
src/Cli/Commands/Queue/Start.php
Normal file
28
src/Cli/Commands/Queue/Start.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Cli\Commands\Queue;
|
||||
|
||||
use Ahc\Cli\Input\Command;
|
||||
use Siteworxpro\App\Async\Consumer;
|
||||
use Siteworxpro\App\Cli\Commands\CommandInterface;
|
||||
|
||||
class Start extends Command implements CommandInterface
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('queue:start', 'Start the queue consumer to process messages.');
|
||||
|
||||
$this->argument('[name]', 'Your name')
|
||||
->option('-g, --greet', 'Include a greeting message');
|
||||
}
|
||||
|
||||
public function execute(): int
|
||||
{
|
||||
$consumer = new Consumer();
|
||||
$consumer->start();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
13
src/Helpers/Ulid.php
Normal file
13
src/Helpers/Ulid.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Helpers;
|
||||
|
||||
class Ulid
|
||||
{
|
||||
public static function generate(): string
|
||||
{
|
||||
return \Ulid\Ulid::generate()->getRandomness();
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use Siteworx\Config\Config as SWConfig;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
use Siteworxpro\App\Services\Facades\Dispatcher;
|
||||
use Siteworxpro\App\Services\ServiceProviders\BrokerServiceProvider;
|
||||
use Siteworxpro\App\Services\ServiceProviders\DispatcherServiceProvider;
|
||||
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
||||
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
|
||||
@@ -19,6 +20,7 @@ class Kernel
|
||||
LoggerServiceProvider::class,
|
||||
RedisServiceProvider::class,
|
||||
DispatcherServiceProvider::class,
|
||||
BrokerServiceProvider::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
117
src/Log/Logger.php
Normal file
117
src/Log/Logger.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Log;
|
||||
|
||||
use Monolog\Formatter\JsonFormatter;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use RoadRunner\Logger\Logger as RRLogger;
|
||||
use Spiral\Goridge\RPC\RPC;
|
||||
|
||||
class Logger implements LoggerInterface
|
||||
{
|
||||
private ?RRLogger $rpcLogger = null;
|
||||
|
||||
private \Monolog\Logger $monologLogger;
|
||||
|
||||
private array $levels = [
|
||||
LogLevel::EMERGENCY => 0,
|
||||
LogLevel::ALERT => 1,
|
||||
LogLevel::CRITICAL => 2,
|
||||
LogLevel::ERROR => 3,
|
||||
LogLevel::WARNING => 4,
|
||||
LogLevel::NOTICE => 5,
|
||||
LogLevel::INFO => 6,
|
||||
LogLevel::DEBUG => 7,
|
||||
];
|
||||
|
||||
public function __construct(private readonly string $level = LogLevel::DEBUG)
|
||||
{
|
||||
if (isset($_SERVER['RR_RPC'])) {
|
||||
$rpc = RPC::create('tcp://127.0.0.1:6001');
|
||||
$this->rpcLogger = new RRLogger($rpc);
|
||||
}
|
||||
|
||||
$this->monologLogger = new \Monolog\Logger('app_logger');
|
||||
$formatter = new JsonFormatter();
|
||||
$this->monologLogger->pushHandler(new StreamHandler('php://stdout')->setFormatter($formatter));
|
||||
}
|
||||
|
||||
public function emergency(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::EMERGENCY, $message, $context);
|
||||
}
|
||||
|
||||
public function alert(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::ALERT, $message, $context);
|
||||
}
|
||||
|
||||
public function critical(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::CRITICAL, $message, $context);
|
||||
}
|
||||
|
||||
public function error(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::ERROR, $message, $context);
|
||||
}
|
||||
|
||||
public function warning(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::WARNING, $message, $context);
|
||||
}
|
||||
|
||||
public function notice(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::NOTICE, $message, $context);
|
||||
}
|
||||
|
||||
public function info(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::INFO, $message, $context);
|
||||
}
|
||||
|
||||
public function debug(\Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$this->log(LogLevel::DEBUG, $message, $context);
|
||||
}
|
||||
|
||||
public function log($level, \Stringable|string $message, array $context = []): void
|
||||
{
|
||||
if ($this->levels[$level] > $this->levels[$this->level]) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->rpcLogger) {
|
||||
switch ($level) {
|
||||
case LogLevel::DEBUG:
|
||||
$this->rpcLogger->debug((string)$message, $context);
|
||||
break;
|
||||
case LogLevel::NOTICE:
|
||||
case LogLevel::INFO:
|
||||
$this->rpcLogger->info((string)$message, $context);
|
||||
break;
|
||||
case LogLevel::WARNING:
|
||||
$this->rpcLogger->warning((string)$message, $context);
|
||||
break;
|
||||
case LogLevel::CRITICAL:
|
||||
case LogLevel::ERROR:
|
||||
case LogLevel::ALERT:
|
||||
case LogLevel::EMERGENCY:
|
||||
$this->rpcLogger->error((string)$message, $context);
|
||||
break;
|
||||
default:
|
||||
$this->rpcLogger->log((string)$message, $context);
|
||||
break;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->monologLogger->log($this->levels[$level], (string)$message, $context);
|
||||
}
|
||||
}
|
||||
29
src/Services/Facades/Broker.php
Normal file
29
src/Services/Facades/Broker.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Services\Facades;
|
||||
|
||||
use Siteworxpro\App\Async\Messages\Message;
|
||||
use Siteworxpro\App\Async\Queues\Queue;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
|
||||
/**
|
||||
* Broker Facade
|
||||
*
|
||||
* @method static void publish(Queue $queue, Message $message, int $delay = 0)
|
||||
* @method static void publishLater(Queue $queue, Message $message, int $delay)
|
||||
* @method static Message|null consume(Queue $queue)
|
||||
*/
|
||||
class Broker extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string The name of the component.
|
||||
*/
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return \Siteworxpro\App\Async\Brokers\Broker::class;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Services\Facades;
|
||||
|
||||
use RoadRunner\Logger\Logger as RRLogger;
|
||||
use Siteworxpro\App\Services\Facade;
|
||||
|
||||
/**
|
||||
@@ -13,10 +12,13 @@ use Siteworxpro\App\Services\Facade;
|
||||
* This class serves as a facade for the Monolog logger.
|
||||
* It extends the Facade class from the Illuminate\Support\Facades namespace.
|
||||
*
|
||||
* @method static debug(string $message, array $context = []) Log an informational message.
|
||||
* @method static info(string $message, array $context = []) Log an informational message.
|
||||
* @method static error(string $message, array $context = []) Log an error message.
|
||||
* @method static warning(string $message, array $context = []) Log a warning message.
|
||||
* @method static debug(\Stringable|string $message, array $context = []) Log an informational message.
|
||||
* @method static info(\Stringable|string $message, array $context = []) Log an informational message.
|
||||
* @method static error(\Stringable|string $message, array $context = []) Log an error message.
|
||||
* @method static warning(\Stringable|string $message, array $context = []) Log a warning message.
|
||||
* @method static critical(\Stringable|string $message, array $context = []) Log a critical error message.
|
||||
* @method static alert(\Stringable|string $message, array $context = []) Log an alert message.
|
||||
* @method static emergency(\Stringable|string $message, array $context = []) Log an emergency message.
|
||||
*
|
||||
* @package Siteworxpro\App\Facades
|
||||
*/
|
||||
@@ -29,6 +31,6 @@ class Logger extends Facade
|
||||
*/
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return RRLogger::class;
|
||||
return \Siteworxpro\App\Log\Logger::class;
|
||||
}
|
||||
}
|
||||
|
||||
28
src/Services/ServiceProviders/BrokerServiceProvider.php
Normal file
28
src/Services/ServiceProviders/BrokerServiceProvider.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Services\ServiceProviders;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Siteworxpro\App\Async\Brokers\Broker;
|
||||
use Siteworxpro\App\Services\Facades\Config;
|
||||
|
||||
class BrokerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(Broker::class, function (): Broker {
|
||||
$configName = Config::get('queue.broker');
|
||||
$brokerConfig = Config::get('queue.' . $configName) ?? [];
|
||||
|
||||
$brokerClass = Broker::BROKER_TYPES[$configName] ?? null;
|
||||
|
||||
if ($brokerClass && class_exists($brokerClass)) {
|
||||
return new $brokerClass($brokerConfig);
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Broker class $brokerClass does not exist.");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,16 +5,14 @@ declare(strict_types=1);
|
||||
namespace Siteworxpro\App\Services\ServiceProviders;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use RoadRunner\Logger\Logger as RRLogger;
|
||||
use Spiral\Goridge\RPC\RPC;
|
||||
use Siteworxpro\App\Log\Logger;
|
||||
|
||||
class LoggerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(RRLogger::class, function () {
|
||||
$rpc = RPC::create('tcp://127.0.0.1:6001');
|
||||
return new RRLogger($rpc);
|
||||
$this->app->singleton(Logger::class, function () {
|
||||
return new Logger();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user