Merge pull request #12552 from appwrite/feat/worker-webhooks-coroutines

Make webhooks worker coroutine-safe
This commit is contained in:
Luke B. Silver authored and GitHub committed 2026-06-09 16:47:21 +01:00
commit b812be3d50
2 files changed
+17 -14

No files matched your search

+1 -1
View File
@@ -386,7 +386,7 @@ services:
environment:
- _APP_ENV
- _APP_WORKERS_NUM=1
- _APP_WORKER_MAX_COROUTINES=1
- _APP_WORKER_MAX_COROUTINES=8
- _APP_WORKER_PER_CORE
- _APP_POOL_ADAPTER
- _APP_OPENSSL_KEY_V1
+16 -13
View File
@@ -20,7 +20,6 @@ use Utopia\System\System;
class Webhooks extends Action
{
private array $errors = [];
private const MAX_FILE_SIZE = 5242880; // 5 MB
public static function getName(): string
@@ -58,11 +57,8 @@ class Webhooks extends Action
*/
public function action(Message $message, Document $project, Database $dbForPlatform, MailPublisher $publisherForMails, UsagePublisher $publisherForUsage, Log $log, array $plan): void
{
$this->errors = [];
$payload = $message->getPayload();
if (empty($payload)) {
throw new Exception('Missing payload');
}
@@ -73,14 +69,18 @@ class Webhooks extends Action
$log->addTag('projectId', $project->getId());
$errors = [];
foreach ($project->getAttribute('webhooks', []) as $webhook) {
if (array_intersect($webhook->getAttribute('events', []), $events)) {
$this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $publisherForMails, $publisherForUsage, $plan);
$error = $this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $publisherForMails, $publisherForUsage, $plan);
if ($error !== null) {
$errors[] = $error;
}
}
}
if (!empty($this->errors)) {
throw new Exception(\implode(" / \n\n", $this->errors));
if (!empty($errors)) {
throw new Exception(\implode(" / \n\n", $errors));
}
}
@@ -93,12 +93,12 @@ class Webhooks extends Action
* @param Database $dbForPlatform
* @param MailPublisher $publisherForMails
* @param array $plan
* @return void
* @return string|null The error log if the delivery failed, otherwise null
*/
private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, MailPublisher $publisherForMails, UsagePublisher $publisherForUsage, array $plan): void
private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, MailPublisher $publisherForMails, UsagePublisher $publisherForUsage, array $plan): ?string
{
if ($webhook->getAttribute('enabled') !== true) {
return;
return null;
}
$url = \rawurldecode($webhook->getAttribute('url'));
@@ -107,8 +107,7 @@ class Webhooks extends Action
$host = \parse_url($url, PHP_URL_HOST) ?? '';
$hostnameValidator = new PublicHostname();
if (!$hostnameValidator->isValid($host)) {
$this->errors[] = 'Webhook target ' . $host . ' rejected: ' . $hostnameValidator->getDescription();
return;
return 'Webhook target ' . $host . ' rejected: ' . $hostnameValidator->getDescription();
}
}
@@ -159,6 +158,8 @@ class Webhooks extends Action
$curlError = \curl_error($ch);
$statusCode = \curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$error = null;
if (!empty($curlError) || $statusCode >= 400) {
$dbForPlatform->increaseDocumentAttribute('webhooks', $webhook->getId(), 'attempts', 1);
$webhook = $dbForPlatform->getDocument('webhooks', $webhook->getId());
@@ -189,7 +190,7 @@ class Webhooks extends Action
$dbForPlatform->updateDocument('webhooks', $webhook->getId(), new Document($updatePayload));
$dbForPlatform->purgeCachedDocument('projects', $project->getId());
$this->errors[] = $logs;
$error = $logs;
$usage = (new UsageContext())
->addMetric(METRIC_WEBHOOKS_FAILED, 1)
->addMetric(str_replace('{webhookInternalId}', $webhook->getSequence(), METRIC_WEBHOOK_ID_FAILED), 1);
@@ -207,6 +208,8 @@ class Webhooks extends Action
project: $project,
metrics: $usage->getMetrics(),
));
return $error;
}
/**