Skip to content

Commit

Permalink
feat(slack): fixed and tested with queue work
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Dec 9, 2024
1 parent 5a888b4 commit c58dd48
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 134 deletions.
10 changes: 9 additions & 1 deletion services/Commands/Hm_CheckMailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Services\Jobs\Hm_ProcessNewEmail;
use Services\Core\Commands\Hm_BaseCommand;
use Services\Notifications\Hm_NewMailNotification;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -32,7 +33,14 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->info("Checking for new mail...");
Hm_ProcessNewEmail::dispatch(email: '[email protected]');
$message = "Hello! You have a new mail on test@entreprise server.";

// $notification = (new Hm_NewMailNotification())
// ->greeting('New Mail Alert')
// ->line($message);
// $notification->sendNow();
Hm_NewMailNotification::dispatch($message);
// Hm_ProcessNewEmail::dispatch(email: '[email protected]');
return Command::SUCCESS;
}
}
33 changes: 14 additions & 19 deletions services/Core/Notifications/Channels/Hm_SlackChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Services\Core\Notifications\Channels;

use Services\Core\Hm_Container;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackContextBlock;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;

/**
* Class Hm_SlackChannel
Expand All @@ -16,11 +15,11 @@
class Hm_SlackChannel extends Hm_NotificationChannel
{
/**
* The Notifier instance.
* The Chatter instance.
*
* @var Notifier
* @var Chatter
*/
private $notifier;
private $chatter;

/**
* Constructor.
Expand All @@ -31,9 +30,12 @@ public function __construct()
$config = Hm_Container::getContainer()->get('config');
$slackConfig = $config->get('slack');
$slackToken = $slackConfig['token'];
$slackChannel = $slackConfig['channel'];
$slackTransport = new SlackTransport($slackToken, $slackChannel);
$this->notifier = new Notifier([$slackTransport]);
$slackChannel = $slackConfig['channel'];
$dsnString = sprintf('slack://%s@default?channel=%s', $slackToken, $slackChannel);
$dsn = new Dsn($dsnString);
$factory = new SlackTransportFactory();
$transport = $factory->create($dsn);
$this->chatter = new Chatter($transport);
}

/**
Expand All @@ -43,15 +45,8 @@ public function __construct()
*/
public function send($notification): void
{
$slackMessage = new ChatMessage($notification->getTitle());
$contextBlock = (new SlackContextBlock())
->text($notification->getMessageText());
// Optionally, configure options (e.g., set icon, etc.)
$slackMessage->options((new SlackOptions())->block($contextBlock));

// Send the message to Slack
$this->notifier->send($slackMessage->getNotification());

$chatMessage = new ChatMessage($notification->getContent());
$this->chatter->send($chatMessage);
echo "Message sent to Slack!";
}
}
101 changes: 19 additions & 82 deletions services/Core/Notifications/Hm_Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace Services\Core\Notifications;

use Symfony\Component\Notifier\Notifier;
use Services\Traits\Hm_Dispatchable;
use Services\Core\Queue\Hm_Queueable;
use Services\Contracts\Notifications\Hm_Dispatcher;
use Symfony\Component\Notifier\Recipient\Recipient;

/**
* Notification class
Expand All @@ -15,65 +13,22 @@
class Hm_Notification extends Hm_Queueable implements Hm_Dispatcher
{
use Hm_Dispatchable;
/**
* The notification driver.
*
* @var string
*/
public string $driver;

/**
* The notification title.
* The notification content(message).
*
* @var string
*/
public string $title;

/**
* The notification lines.
*
* @var array
*/
public array $lines = [];
public string $content = '';

/**
* The recipient of the notification.
* Constructor.
*
* @var Recipient
*/
protected Recipient $recipient;

/**
* Set the title of the notification.
*
* @param string $title
* @return self
*/
public function greeting(string $title): self
{
$this->title = $title;
return $this;
}

/**
* Add a line to the message of the notification.
*
* @param string $line
* @return self
*/
public function line(string $line): self
{
$this->lines[] = $line;
return $this;
}

/**
* Get the full message text, combining all lines.
*
* @return string
* @param string $content The notification content.
*/
public function getMessageText(): string
public function __construct($content = '')
{
return implode("\n", $this->lines);
$this->content = $content;
}
/**
* Notifcations can be sent through multiple channels.
Expand All @@ -85,38 +40,15 @@ public function via(): array
return [];
}

/**
* Get the recipient of the notification.
*
* @return Recipient
*/
public function getRecipient(): Recipient
public function handle(): void
{
return $this->recipient;
dump("Processing ".self::class);

$this->sendNow();
}

/**
* Get the title of the notification.
*
* @return string
*/
public function getTitle(): string
public function failed(): void
{
return $this->title;
}

/**
* Set the recipient for the notification.
*
* @param mixed $recipient
* @return self
*/
static public function to(mixed $recipient): string
{
self::$recipient = new Recipient(
is_array($recipient) ? implode(',', $recipient) : $recipient
);
return static::class;
echo "Notification failed to send!";
}

public function send(): void
Expand All @@ -136,7 +68,7 @@ public function sendNow(): void
if (class_exists($channelClass)) {
$channelInstance = new $channelClass();
if (method_exists($channelInstance, 'send')) {
$channelInstance->send($this->recipient, $this->title, $this->getMessageText());
$channelInstance->send($this);
} else {
throw new \Exception("The channel {$channel} does not have a send method.");
}
Expand All @@ -145,5 +77,10 @@ public function sendNow(): void
}
}
}

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

13 changes: 3 additions & 10 deletions services/Core/Queue/Drivers/Hm_AmazonSQSQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Aws\Sqs\SqsClient;
use Services\Contracts\Queue\Hm_Queueable;
use Services\Contracts\Queue\Hm_ShouldQueue;
use Services\Core\Notifications\Hm_Notification;
use Services\Core\Queue\Hm_Queueable as Hm_QueueableClass;

/**
Expand Down Expand Up @@ -72,19 +71,13 @@ public function pop(): ?Hm_QueueableClass
$item = unserialize($body);

try {
// Check if the item is a notification, if so send it
if($item instanceof Hm_Notification) {
$item->send();
}else {
// Otherwise handle the job
$item->handle();
}
$item->handle();
} catch (\Exception $e) {
$this->fail($item, $e); // Log the failure
$this->fail($item, $e);
throw new \Exception("Failed to process job: " . $e->getMessage());
}

return $item; // Return the job if it was processed successfully
return $item;
}

return null;
Expand Down
8 changes: 1 addition & 7 deletions services/Core/Queue/Drivers/Hm_DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,7 @@ public function release(Hm_QueueableClass $item, int $delay = 0): void {
public function process(Hm_QueueableClass $item): void
{
try {
// Check if the item is a notification, if so send it
if($item instanceof Hm_Notification) {
$item->send();
}else {
// Otherwise handle the job
$item->handle();
}
$item->handle();
} catch (\Exception $e) {
$item->incrementAttempts();
if ($item->getAttempts() >= $item->tries) {
Expand Down
9 changes: 1 addition & 8 deletions services/Core/Queue/Drivers/Hm_RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Exception;
use Services\Contracts\Queue\Hm_Queueable;
use Services\Contracts\Queue\Hm_ShouldQueue;
use Services\Core\Notifications\Hm_Notification;
use Services\Core\Queue\Hm_Queueable as Hm_QueueableClass;

/**
Expand Down Expand Up @@ -106,13 +105,7 @@ public function release(Hm_QueueableClass $item, int $delay = 0): void {
*/
public function process(Hm_QueueableClass $item): void {
try {
// Check if the item is a notification, if so send it
if($item instanceof Hm_Notification) {
$item->send();
}else {
// Otherwise handle the job
$item->handle();
}
$item->handle();
} catch (Exception $e) {
$item->incrementAttempts();
if ($item->getAttempts() >= $item->tries) {
Expand Down
1 change: 0 additions & 1 deletion services/Core/Queue/Hm_QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function addDriver(string $name, Hm_ShouldQueue $driver): void

public function getDriver(string $name): Hm_ShouldQueue
{
dump("Getting driver $name");
return $this->drivers[$name];
}
}
3 changes: 2 additions & 1 deletion services/Core/Queue/Hm_QueueWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public function work(): void {
while ($item = $this->queue->pop())
{
try {
// exit(var_dump($this->queue));
$this->queue->process($item);
} catch (\Exception $e) {
// $job->failed();
$item->failed();
// // Optionally release the job back to the queue with a delay
// $this->queue->release($job, 30);
}
Expand Down
27 changes: 27 additions & 0 deletions services/Hm_Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Services;

use Services\Core\Scheduling\Hm_Scheduler;

class Hm_Kernel
{
protected $scheduler;

public function __construct(Hm_Scheduler $scheduler)
{
$this->scheduler = $scheduler;

}

/**
* Define the application's command schedule.
*/
public function schedule()
{
// Register tasks with the scheduler
$this->scheduler->command('check:mail')
->everyMinute()
->withoutOverlapping();
}
}
12 changes: 7 additions & 5 deletions services/Notifications/Hm_NewMailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class Hm_NewMailNotification extends Hm_Notification implements Hm_ShouldQueue
{
use Hm_Dispatchable, Hm_InteractsWithQueue;

public function __construct(array $config = [])
{
parent::__construct($config);
}
public string $driver = 'redis';

// public function __construct(array $config = [])
// {
// parent::__construct($config);
// }

public function via(): array
{
return ['slack', 'telegram','broadcast'];
return ['slack'];//, 'telegram','broadcast'
}
}

0 comments on commit c58dd48

Please sign in to comment.