From a77a212e2c80dc60db5d0ae3431cfb97d9c65e4d Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Mon, 9 Oct 2023 17:29:26 +0530 Subject: [PATCH] adds scheduling for messaging worker --- app/controllers/api/messaging.php | 5 ++++- src/Appwrite/Event/Messaging.php | 36 +++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index e4a008165b..f4718e55b2 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -21,6 +21,7 @@ use Utopia\Validator\Boolean; use Utopia\Validator\JSON; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\Database\DateTime; App::post('/v1/messaging/providers/mailgun') ->desc('Create Mailgun Provider') @@ -1340,11 +1341,12 @@ App::post('/v1/messaging/messages/email/:messageId') ->param('content', '', new Text(64230), 'Email Content.', true) ->param('status', '', new WhiteList(['draft', 'processing']), 'Message Status.', true) ->param('html', false, new Boolean(), 'Is content of type HTML', true) + ->param('deliveryTime', DateTime::now(), new DatetimeValidator(), 'Delivery time for message.', true) ->inject('dbForProject') ->inject('project') ->inject('messaging') ->inject('response') - ->action(function (string $messageId, array $to, string $subject, string $description, string $content, string $status, bool $html, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { + ->action(function (string $messageId, array $to, string $subject, string $description, string $content, string $status, bool $html, string $deliveryTime, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { $message = $dbForProject->getDocument('messages', $messageId); if ($message->isEmpty()) { @@ -1385,6 +1387,7 @@ App::post('/v1/messaging/messages/email/:messageId') if ($status === 'processing') { $messaging ->setMessageId($message->getId()) + ->setDeliveryTime($deliveryTime) ->setProject($project) ->trigger(); } diff --git a/src/Appwrite/Event/Messaging.php b/src/Appwrite/Event/Messaging.php index 999c9ee0dd..18887ffcd3 100644 --- a/src/Appwrite/Event/Messaging.php +++ b/src/Appwrite/Event/Messaging.php @@ -2,12 +2,13 @@ namespace Appwrite\Event; -use Resque; -use Utopia\Database\Document; +use ResqueScheduler; +use Utopia\Database\DateTime; class Messaging extends Event { protected ?string $messageId = null; + private ?string $deliveryTime = null; public function __construct() { @@ -38,17 +39,38 @@ class Messaging extends Event } /** - * Executes the event and sends it to the messaging worker. + * Sets Delivery time for the messaging event. * - * @return string|bool - * @throws \InvalidArgumentException + * @param string $deliveryTime + * @return self */ - public function trigger(): string|bool + public function setDeliveryTime(string $deliveryTime): self { - return Resque::enqueue($this->queue, $this->class, [ + $this->deliveryTime = $deliveryTime; + + return $this; + } + + /** + * Returns set Delivery Time for the messaging event. + * + * @return string + */ + public function getDeliveryTime(): string + { + return $this->deliveryTime; + } + + /** + * Executes the event and sends it to the messaging worker. + */ + public function trigger(): string | bool + { + ResqueScheduler::enqueueAt(!empty($this->deliveryTime) ? $this->deliveryTime : DateTime::now(), $this->queue, $this->class, [ 'project' => $this->project, 'user' => $this->user, 'messageId' => $this->messageId, ]); + return true; } }