1
0
Fork 0
mirror of synced 2024-07-06 23:21:05 +12:00

adds scheduling for messaging worker

This commit is contained in:
Prateek Banga 2023-10-09 17:29:26 +05:30
parent a92390dda2
commit a77a212e2c
2 changed files with 33 additions and 8 deletions

View file

@ -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();
}

View file

@ -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;
}
}