From 3e74e39d1dd74d5b7c48923bcce5bcc9fdf8d259 Mon Sep 17 00:00:00 2001 From: Khushboo Verma Date: Mon, 28 Jul 2025 10:26:20 +0530 Subject: [PATCH] Add headers for event triggered executions --- app/controllers/shared/api.php | 6 +++--- app/init/constants.php | 2 +- src/Appwrite/Platform/Workers/Functions.php | 3 ++- .../e2e/Services/Functions/FunctionsCustomServerTest.php | 8 ++++++++ 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 04e5c41ba5..5f88440c2c 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -70,8 +70,11 @@ $eventDatabaseListener = function (Document $project, Document $document, Respon ->setParam('userId', $document->getId()) ->setPayload($response->output($document, Response::MODEL_USER)); + $scheduledAt = new \DateTime(); + // Trigger functions, webhooks, and realtime events $queueForFunctions + ->setHeaders(['x-appwrite-scheduled-at' => $scheduledAt->format('Y-m-d\TH:i:s.v\Z')]) ->from($queueForEvents) ->trigger(); @@ -713,10 +716,7 @@ App::shutdown() $queueForEvents->setPayload($responsePayload); } - $scheduledAt = new \DateTime(); - $queueForFunctions - ->setHeaders(['x-appwrite-scheduled-at' => $scheduledAt->format('Y-m-d\TH:i:s.v\Z')]) ->from($queueForEvents) ->trigger(); diff --git a/app/init/constants.php b/app/init/constants.php index d58cd60560..7678f5e0b6 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -142,7 +142,7 @@ const APP_AUTH_TYPE_ADMIN = 'Admin'; // Response related const MAX_OUTPUT_CHUNK_SIZE = 10 * 1024 * 1024; // 10MB // Function headers -const FUNCTION_ALLOWLIST_HEADERS_REQUEST = ['content-type', 'agent', 'content-length', 'host', 'x-appwrite-scheduled-at']; +const FUNCTION_ALLOWLIST_HEADERS_REQUEST = ['content-type', 'agent', 'content-length', 'host', 'x-appwrite-scheduled-at', 'x-appwrite-execution-delay', 'x-appwrite-executed-at']; const FUNCTION_ALLOWLIST_HEADERS_RESPONSE = ['content-type', 'content-length']; // Message types const MESSAGE_TYPE_EMAIL = 'email'; diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index 1335d03aea..bf80f94942 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -164,7 +164,8 @@ class Functions extends Action method: 'POST', headers: [ 'user-agent' => 'Appwrite/' . APP_VERSION_STABLE, - 'content-type' => 'application/json' + 'content-type' => 'application/json', + 'x-appwrite-scheduled-at' => $headers['x-appwrite-scheduled-at'] ?? '', ], data: null, user: $user, diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 8ba4d39a74..3acbcae0ee 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1508,6 +1508,14 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(204, $lastExecution['responseStatusCode']); $this->assertStringContainsString($userId, $lastExecution['logs']); $this->assertStringContainsString('Event User', $lastExecution['logs']); + + $requestHeaders = array_column($lastExecution['requestHeaders'], 'value', 'name'); + $this->assertArrayHasKey('x-appwrite-scheduled-at', $requestHeaders); + $this->assertArrayHasKey('x-appwrite-executed-at', $requestHeaders); + $this->assertArrayHasKey('x-appwrite-execution-delay', $requestHeaders); + $this->assertIsNumeric($requestHeaders['x-appwrite-execution-delay']); + $this->assertGreaterThan($requestHeaders['x-appwrite-scheduled-at'], $requestHeaders['x-appwrite-executed-at']); + }, 10000, 500); $this->cleanupFunction($functionId);