Port Mail Worker from resque to Utopia Queue
This commit is contained in:
parent
072d896bb9
commit
2e49666ee6
3 changed files with 79 additions and 74 deletions
|
@ -850,7 +850,9 @@ App::setResource('locale', fn() => new Locale(App::getEnv('_APP_LOCALE', 'en')))
|
||||||
// Queues
|
// Queues
|
||||||
App::setResource('events', fn() => new Event('', ''));
|
App::setResource('events', fn() => new Event('', ''));
|
||||||
App::setResource('audits', fn() => new Audit());
|
App::setResource('audits', fn() => new Audit());
|
||||||
App::setResource('mails', fn() => new Mail());
|
App::setResource('mails', function (Connection $queue) {
|
||||||
|
return new Mail($queue);
|
||||||
|
}, ['queue']);
|
||||||
App::setResource('deletes', fn() => new Delete());
|
App::setResource('deletes', fn() => new Delete());
|
||||||
App::setResource('database', fn() => new EventDatabase());
|
App::setResource('database', fn() => new EventDatabase());
|
||||||
App::setResource('messaging', fn() => new Phone());
|
App::setResource('messaging', fn() => new Phone());
|
||||||
|
|
|
@ -5,26 +5,69 @@ use Appwrite\Template\Template;
|
||||||
use Utopia\App;
|
use Utopia\App;
|
||||||
use Utopia\CLI\Console;
|
use Utopia\CLI\Console;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
|
use Utopia\Database\Validator\Authorization;
|
||||||
use Utopia\Locale\Locale;
|
use Utopia\Locale\Locale;
|
||||||
|
use Utopia\Queue\Message;
|
||||||
|
use Utopia\Queue\Server;
|
||||||
|
|
||||||
require_once __DIR__ . '/../init.php';
|
require_once __DIR__ . '/../worker.php';
|
||||||
|
|
||||||
Console::title('Mails V1 Worker');
|
|
||||||
Console::success(APP_NAME . ' mails worker v1 has started' . "\n");
|
|
||||||
|
|
||||||
class MailsV1 extends Worker
|
Authorization::disable();
|
||||||
|
Authorization::setDefaultStatus(false);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns a prefix from a mail type
|
||||||
|
*
|
||||||
|
* @param $type
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function getPrefix(string $type): string
|
||||||
{
|
{
|
||||||
public function getName(): string
|
switch ($type) {
|
||||||
{
|
case MAIL_TYPE_RECOVERY:
|
||||||
return "mails";
|
return 'emails.recovery';
|
||||||
|
case MAIL_TYPE_CERTIFICATE:
|
||||||
|
return 'emails.certificate';
|
||||||
|
case MAIL_TYPE_INVITATION:
|
||||||
|
return 'emails.invitation';
|
||||||
|
case MAIL_TYPE_VERIFICATION:
|
||||||
|
return 'emails.verification';
|
||||||
|
case MAIL_TYPE_MAGIC_SESSION:
|
||||||
|
return 'emails.magicSession';
|
||||||
|
default:
|
||||||
|
throw new Exception('Undefined Mail Type : ' . $type, 500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init(): void
|
/**
|
||||||
|
* Returns true if all the required terms in a locale exist. False otherwise
|
||||||
|
*
|
||||||
|
* @param $locale
|
||||||
|
* @param $prefix
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function doesLocaleExist(Locale $locale, string $prefix): bool
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if (!$locale->getText('emails.sender') || !$locale->getText("$prefix.hello") || !$locale->getText("$prefix.subject") || !$locale->getText("$prefix.body") || !$locale->getText("$prefix.footer") || !$locale->getText("$prefix.thanks") || !$locale->getText("$prefix.signature")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$server->job()
|
||||||
|
->inject('message')
|
||||||
|
->action(function (Message $message) {
|
||||||
|
$payload = $message->getPayload() ?? [];
|
||||||
|
|
||||||
|
if (empty($payload)) {
|
||||||
|
throw new Exception('Missing payload');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(): void
|
|
||||||
{
|
|
||||||
global $register;
|
global $register;
|
||||||
|
|
||||||
if (empty(App::getEnv('_APP_SMTP_HOST'))) {
|
if (empty(App::getEnv('_APP_SMTP_HOST'))) {
|
||||||
|
@ -32,20 +75,19 @@ class MailsV1 extends Worker
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$project = new Document($this->args['project'] ?? []);
|
$project = new Document($payload['project'] ?? []);
|
||||||
$user = new Document($this->args['user'] ?? []);
|
$user = new Document($payload['user'] ?? []);
|
||||||
$team = new Document($this->args['team'] ?? []);
|
$team = new Document($payload['team'] ?? []);
|
||||||
$payload = $this->args['payload'] ?? [];
|
|
||||||
|
|
||||||
$recipient = $this->args['recipient'];
|
$recipient = $payload['recipient'];
|
||||||
$url = $this->args['url'];
|
$url = $payload['url'];
|
||||||
$name = $this->args['name'];
|
$name = $payload['name'];
|
||||||
$type = $this->args['type'];
|
$type = $payload['type'];
|
||||||
$prefix = $this->getPrefix($type);
|
$prefix = getPrefix($type);
|
||||||
$locale = new Locale($this->args['locale']);
|
$locale = new Locale($payload['locale']);
|
||||||
$projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]');
|
$projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]');
|
||||||
|
|
||||||
if (!$this->doesLocaleExist($locale, $prefix)) {
|
if (!doesLocaleExist($locale, $prefix)) {
|
||||||
$locale->setDefault('en');
|
$locale->setDefault('en');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,52 +167,7 @@ class MailsV1 extends Worker
|
||||||
} catch (\Exception $error) {
|
} catch (\Exception $error) {
|
||||||
throw new Exception('Error sending mail: ' . $error->getMessage(), 500);
|
throw new Exception('Error sending mail: ' . $error->getMessage(), 500);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
public function shutdown(): void
|
$server->workerStart();
|
||||||
{
|
$server->start();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a prefix from a mail type
|
|
||||||
*
|
|
||||||
* @param $type
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function getPrefix(string $type): string
|
|
||||||
{
|
|
||||||
switch ($type) {
|
|
||||||
case MAIL_TYPE_RECOVERY:
|
|
||||||
return 'emails.recovery';
|
|
||||||
case MAIL_TYPE_CERTIFICATE:
|
|
||||||
return 'emails.certificate';
|
|
||||||
case MAIL_TYPE_INVITATION:
|
|
||||||
return 'emails.invitation';
|
|
||||||
case MAIL_TYPE_VERIFICATION:
|
|
||||||
return 'emails.verification';
|
|
||||||
case MAIL_TYPE_MAGIC_SESSION:
|
|
||||||
return 'emails.magicSession';
|
|
||||||
default:
|
|
||||||
throw new Exception('Undefined Mail Type : ' . $type, 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if all the required terms in a locale exist. False otherwise
|
|
||||||
*
|
|
||||||
* @param $locale
|
|
||||||
* @param $prefix
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function doesLocaleExist(Locale $locale, string $prefix): bool
|
|
||||||
{
|
|
||||||
|
|
||||||
if (!$locale->getText('emails.sender') || !$locale->getText("$prefix.hello") || !$locale->getText("$prefix.subject") || !$locale->getText("$prefix.body") || !$locale->getText("$prefix.footer") || !$locale->getText("$prefix.thanks") || !$locale->getText("$prefix.signature")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,8 @@ namespace Appwrite\Event;
|
||||||
|
|
||||||
use Resque;
|
use Resque;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
|
use Utopia\Queue\Client;
|
||||||
|
use Utopia\Queue\Connection;
|
||||||
|
|
||||||
class Mail extends Event
|
class Mail extends Event
|
||||||
{
|
{
|
||||||
|
@ -14,7 +16,7 @@ class Mail extends Event
|
||||||
protected string $locale = '';
|
protected string $locale = '';
|
||||||
protected ?Document $team = null;
|
protected ?Document $team = null;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct(protected Connection $connection)
|
||||||
{
|
{
|
||||||
parent::__construct(Event::MAILS_QUEUE_NAME, Event::MAILS_CLASS_NAME);
|
parent::__construct(Event::MAILS_QUEUE_NAME, Event::MAILS_CLASS_NAME);
|
||||||
}
|
}
|
||||||
|
@ -165,7 +167,11 @@ class Mail extends Event
|
||||||
*/
|
*/
|
||||||
public function trigger(): string|bool
|
public function trigger(): string|bool
|
||||||
{
|
{
|
||||||
return Resque::enqueue($this->queue, $this->class, [
|
$client = new Client($this->queue, $this->connection);
|
||||||
|
|
||||||
|
$events = $this->getEvent() ? Event::generateEvents($this->getEvent(), $this->getParams()) : null;
|
||||||
|
|
||||||
|
return $client->enqueue([
|
||||||
'project' => $this->project,
|
'project' => $this->project,
|
||||||
'user' => $this->user,
|
'user' => $this->user,
|
||||||
'payload' => $this->payload,
|
'payload' => $this->payload,
|
||||||
|
|
Loading…
Reference in a new issue