1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

refactor workers

This commit is contained in:
shimon 2023-06-04 11:19:49 +03:00
parent 6e7c160249
commit 0d2987620c
8 changed files with 47 additions and 5380 deletions

View file

@ -237,7 +237,7 @@ try {
'workerName' => $workerName ?? null,
]);
} catch (\Exception $e) {
Console::error($e->getMessage() . ', File: '.$e->getFile(). ', Line: '.$e->getLine());
Console::error($e->getMessage() . ', File: ' . $e->getFile() . ', Line: ' . $e->getLine());
}
$worker = $platform->getWorker();
@ -254,6 +254,11 @@ $worker
->inject('error')
->inject('logger')
->action(function (Throwable $error, Logger|null $logger) {
if ($logger === null) {
return;
}
$version = App::getEnv('_APP_VERSION', 'UNKNOWN');
if ($error instanceof PDOException) {
@ -289,7 +294,5 @@ $worker
Console::error('[Error] Line: ' . $error->getLine());
});
$worker->workerStart();
$worker->start();

5211
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -52,7 +52,9 @@ class Event
* @param Connection $connection
* @return void
*/
public function __construct(protected Connection $connection){}
public function __construct(protected Connection $connection)
{
}
/**
* Set queue used for this event.

View file

@ -9,11 +9,10 @@ use Utopia\Queue\Connection;
class Mail extends Event
{
protected string $recipient = '';
protected string $url = '';
protected string $type = '';
protected string $from = '';
protected string $name = '';
protected string $locale = '';
protected ?Document $team = null;
protected string $subject = '';
protected string $body = '';
public function __construct(protected Connection $connection)
{
@ -25,14 +24,14 @@ class Mail extends Event
}
/**
* Sets team for the mail event.
* Sets subject for the mail event.
*
* @param Document $team
* @param string $subject
* @return self
*/
public function setTeam(Document $team): self
public function setSubject(string $subject): self
{
$this->team = $team;
$this->subject = $subject;
return $this;
}
@ -40,11 +39,11 @@ class Mail extends Event
/**
* Returns set team for the mail event.
*
* @return null|Document
* @return string
*/
public function getTeam(): ?Document
public function getSubject(): string
{
return $this->team;
return $this->subject;
}
/**
@ -71,49 +70,49 @@ class Mail extends Event
}
/**
* Sets url for the mail event.
* Sets from for the mail event.
*
* @param string $url
* @param string $from
* @return self
*/
public function setUrl(string $url): self
public function setFrom(string $from): self
{
$this->url = $url;
$this->from = $from;
return $this;
}
/**
* Returns set url for the mail event.
* Returns from for mail event.
*
* @return string
*/
public function getURL(): string
public function getFrom(): string
{
return $this->url;
return $this->from;
}
/**
* Sets type for the mail event (use the constants starting with MAIL_TYPE_*).
* Sets body for the mail event.
*
* @param string $type
* @param string $body
* @return self
*/
public function setType(string $type): self
public function setBody(string $body): self
{
$this->type = $type;
$this->body = $body;
return $this;
}
/**
* Returns set type for the mail event.
* Returns body for the mail event.
*
* @return string
*/
public function getType(): string
public function getBody(): string
{
return $this->type;
return $this->body;
}
/**
@ -139,29 +138,6 @@ class Mail extends Event
return $this->name;
}
/**
* Sets locale for the mail event.
*
* @param string $locale
* @return self
*/
public function setLocale(string $locale): self
{
$this->locale = $locale;
return $this;
}
/**
* Returns set locale for the mail event.
*
* @return string
*/
public function getLocale(): string
{
return $this->locale;
}
/**
* Executes the event and sends it to the mails worker.
*
@ -172,18 +148,12 @@ class Mail extends Event
{
$client = new Client($this->queue, $this->connection);
$events = $this->getEvent() ? Event::generateEvents($this->getEvent(), $this->getParams()) : null;
return $client->enqueue([
'project' => $this->project,
'user' => $this->user,
'payload' => $this->payload,
'from' => $this->from,
'recipient' => $this->recipient,
'url' => $this->url,
'locale' => $this->locale,
'type' => $this->type,
'name' => $this->name,
'team' => $this->team,
'subject' => $this->subject,
'body' => $this->body,
'events' => Event::generateEvents($this->getEvent(), $this->getParams())
]);
}

View file

@ -26,8 +26,7 @@ class Audits extends Action
->desc('Audits worker')
->inject('message')
->inject('dbForProject')
->inject('polls')
->callback(fn ($message, $dbForProject, $pools) => $this->action($message, $dbForProject, $pools));
->callback(fn ($message, $dbForProject) => $this->action($message, $dbForProject));
}
@ -38,7 +37,7 @@ class Audits extends Action
{
$payload = $message->getPayload() ?? [];
var_dump('audits worker');
if (empty($payload)) {
throw new Exception('Missing payload');
}
@ -51,13 +50,6 @@ class Audits extends Action
$ip = $payload['ip'] ?? '';
$user = new Document($payload['user'] ?? []);
$this->execute($event, $auditPayload, $mode, $resource, $userAgent, $ip, $user, $dbForProject);
}
private function execute(string $event, array $payload, string $mode, string $resource, string $userAgent, string $ip, Document $user, Database $dbForProject): void
{
$userName = $user->getAttribute('name', '');
$userEmail = $user->getAttribute('email', '');
@ -75,7 +67,7 @@ class Audits extends Action
'userName' => $userName,
'userEmail' => $userEmail,
'mode' => $mode,
'data' => $payload,
'data' => $auditPayload,
]
);
}

View file

@ -39,7 +39,7 @@ class Mails extends Action
{
$payload = $message->getPayload() ?? [];
var_dump('mails worker');
if (empty($payload)) {
throw new Exception('Missing payload');
}
@ -49,75 +49,11 @@ class Mails extends Action
return;
}
$project = new Document($payload['project'] ?? []);
$user = new Document($payload['user'] ?? []);
$team = new Document($payload['team'] ?? []);
$recipient = $payload['recipient'];
$url = $payload['url'];
$subject = $payload['subject'];
$name = $payload['name'];
$type = $payload['type'];
$prefix = match ($type) {
MAIL_TYPE_RECOVERY => 'emails.recovery',
MAIL_TYPE_CERTIFICATE => 'emails.certificate',
MAIL_TYPE_INVITATION => 'emails.invitation',
MAIL_TYPE_VERIFICATION => 'emails.verification',
MAIL_TYPE_MAGIC_SESSION => 'emails.magicSession',
default => throw new Exception('Undefined Mail Type : ' . $type, 500)
};
$locale = new Locale($payload['locale']);
$projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]');
if (!$this->doesLocaleExist($locale, $prefix)) {
$locale->setDefault('en');
}
$from = $project->isEmpty() || $project->getId() === 'console' ? '' : \sprintf($locale->getText('emails.sender'), $projectName);
$body = Template::fromFile(__DIR__ . '/../config/locale/templates/email-base.tpl');
$subject = '';
switch ($type) {
case MAIL_TYPE_CERTIFICATE:
$domain = $payload['domain'];
$error = $payload['error'];
$attempt = $payload['attempt'];
$subject = \sprintf($locale->getText("$prefix.subject"), $domain);
$body->setParam('{{domain}}', $domain);
$body->setParam('{{error}}', $error);
$body->setParam('{{attempt}}', $attempt);
break;
case MAIL_TYPE_INVITATION:
$subject = \sprintf($locale->getText("$prefix.subject"), $team->getAttribute('name'), $projectName);
$body->setParam('{{owner}}', $user->getAttribute('name'));
$body->setParam('{{team}}', $team->getAttribute('name'));
break;
case MAIL_TYPE_RECOVERY:
case MAIL_TYPE_VERIFICATION:
case MAIL_TYPE_MAGIC_SESSION:
$subject = $locale->getText("$prefix.subject");
break;
default:
throw new Exception('Undefined Mail Type : ' . $type, 500);
}
$body
->setParam('{{subject}}', $subject)
->setParam('{{hello}}', $locale->getText("$prefix.hello"))
->setParam('{{name}}', $name)
->setParam('{{body}}', $locale->getText("$prefix.body"))
->setParam('{{redirect}}', $url)
->setParam('{{footer}}', $locale->getText("$prefix.footer"))
->setParam('{{thanks}}', $locale->getText("$prefix.thanks"))
->setParam('{{signature}}', $locale->getText("$prefix.signature"))
->setParam('{{project}}', $projectName)
->setParam('{{direction}}', $locale->getText('settings.direction'))
->setParam('{{bg-body}}', '#f7f7f7')
->setParam('{{bg-content}}', '#ffffff')
->setParam('{{text-content}}', '#000000');
$body = $body->render();
$body = $payload['body'];
$from = $payload['from'];
/** @var PHPMailer $mail */
$mail = $register->get('smtp');
@ -151,23 +87,4 @@ class Mails extends Action
throw new Exception('Error sending mail: ' . $error->getMessage(), 500);
}
}
/**
* Returns true if all the required terms in a locale exist. False otherwise
*
* @param Locale $locale
* @param string $prefix
* @return bool
* @throws Exception
*/
private 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;
}
}

View file

@ -65,12 +65,6 @@ class Messaging extends Action
throw new Exception('Missing message');
}
$this->execute($payload['recipient'], $payload['message']);
}
private function execute(string $recipient, string $message)
{
$sms = match ($this->dsn->getHost()) {
'mock' => new Mock($this->user, $this->secret), // used for tests
'twilio' => new Twilio($this->user, $this->secret),
@ -93,9 +87,10 @@ class Messaging extends Action
return;
}
$message = new SMS(
to: [$recipient],
content: $message,
to: [$payload['recipient']],
content: $payload['message'],
from: $from,
);

View file

@ -31,11 +31,10 @@ class Webhooks extends Action
public function action(Message $message): void
{
$payload = $message->getPayload() ?? [];
var_dump('webhooks action');
if (empty($payload)) {
throw new Exception('Missing payload');
$events = $payload['events'];
$webhookPayload = json_encode($payload['payload']);
$project = new Document($payload['project']);