Skip to content

fix: publish worker event payloads through the response model - #14316

Merged
ChiragAgg5k merged 1 commit into
mainfrom
fix/execution-event-datetime
Oct 9, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
fix/execution-event-datetime

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #14295. That PR made Response::output() render every datetime field in ISO 8601 with an offset, so event payloads copied from $response->getPayload() got it for free. Payloads that workers build themselves never pass through the response, so the functions.*.executions.*.update event for async, scheduled and event-triggered executions still went out without the response model's formatting, and in fact without $createdAt/$updatedAt at all.

Root cause

The functions worker creates the execution as a fresh in-memory Document with no timestamps (the execution store adds them only when it persists the row) and publishes getArrayCopy(array_keys($model->getRules())) of it. So the webhook/realtime/function-event payload had no $createdAt or $updatedAt. Captured on origin/main:

'has_createdAt' => false, 'createdAt' => NULL, 'updatedAt' => NULL, 'status' => 'failed'

Because the store fills in a missing $createdAt with the time it persists the row, an async execution's stored $createdAt also moved from the time the API queued it to the time the worker finished it.

The same hand-rolled getArrayCopy(array_keys($model->getRules())) payload was also built in Jobs::dispatchUpdate (deployments update), the certificates worker (rules.*.update) and Functions/Create (rules.*.create). Those documents come back from the database, so their datetimes were already ISO 8601, but they skipped the shared formatting as well.

Fix

  • Model::payload(Document) shapes a document as the model for an event published outside a response: the model's filter(), only the model's keys, and datetimes through Model::formatDatetime().
  • formatDatetime() moved from a private method on Response to a public static on Model, so Response::output() and worker payloads use the same normaliser and the formatting stays in one place.
  • The functions worker stamps the execution with $createdAt/$updatedAt and keeps the $createdAt of an execution the API queued (async HTTP). $updatedAt is refreshed when the run finishes.
  • The four ad-hoc payload builders now call (new Model())->payload($document).

Tests

E2E only. I extended existing tests and added no new flows:

  • ProjectWebhooks/WebhooksCustomServerTest::testExecutions: the executions.*.update webhook now asserts that $createdAt and $updatedAt match Y-m-d\TH:i:s.uP (the strict DateTime::createFromFormat check used in the account tests), and that $createdAt equals the one returned when the async execution was created.
  • ProjectWebhooks/WebhooksCustomServerTest::testCreateDeployment: after the build completes, the deployments update webhook published by the worker asserts the same datetime format.

Failing before the fix (origin/main source, tests from this branch):

1) Tests\E2E\Services\ProjectWebhooks\WebhooksCustomServerTest::testExecutions
Failed asserting that false is not false.
tests/e2e/Services/ProjectWebhooks/WebhooksCustomServerTest.php:900
Tests: 2, Assertions: 367, Failures: 1

The deployment assertion already passes on origin/main, because that document is read back from the database. It is there as regression coverage for the worker-built deployment payload.

After the fix the captured payload is 'createdAt' => '2026-10-09T13:45:36.926+00:00', 'updatedAt' => '2026-10-09T13:45:46.921+00:00'.

Verification

Run locally on a docker stack built from this branch:

  • ProjectWebhooks: OK (82 tests, 7358 assertions)
  • Functions: OK (88 tests, 2606 assertions)
  • Webhooks: OK (135 tests, 1032 assertions)
  • FunctionsSchedule: OK (15 tests, 392 assertions)
  • Pint (composer lint): pass on the changed files
  • PHPStan on the changed files: no errors
  • Rector dry run on the changed test: clean

Closes #14317

The functions worker built the executions update payload from a fresh
in-memory document, so webhook, realtime and function-event payloads for
async, scheduled and event-triggered executions had no $createdAt or
$updatedAt, and #14295's response formatting never reached them.

Model::payload() now shapes worker-built payloads the way the response
does, including ISO 8601 datetimes, and the worker stamps the execution
with its timestamps, keeping the creation time of executions queued by
the API.
@luxor-code-review

luxor-code-review Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Schedule trigger drops propagated execution createdAt · Functions.php:317–338 · unchanged line

// src/Appwrite/Platform/Workers/Functions.php
317	                $this->execute(
318	                    dbForProject: $dbForProject,
319	                    queueForWebhooks: $queueForWebhooks,
320	                    publisherForFunctions: $publisherForFunctions,
321	                    queueForRealtime: $queueForRealtime,
322	                    queueForEvents: $queueForEvents,
323	                    bus: $bus,
324	                    project: $project,
325	                    function: $function,
326	                    executor:  $executor,
327	                    trigger: 'schedule',
328	                    path: $path,

The PR propagates the API-created execution's creation time to the functions worker for the http trigger (src/Appwrite/Platform/Workers/Functions.php:302), but the schedule branch at lines 317-338 builds its execution from the same API snapshot (pre-created with trigger => 'schedule' in Executions/Create.php) and does not pass createdAt, so execute() resets $createdAt to the worker's dispatch time (line 637). Scheduled executions — and redeliveries of their messages, which re-run under the same executionId — get their creation time overwritten in the persisted record and the update event payload (webhooks, realtime), diverging from the value the API returned at schedule time and breaking the $createdAt preservation this PR establishes for the HTTP path.


This change centralizes event-payload shaping: Response::formatDatetime() moves to a public static Model::formatDatetime(), and a new Model::payload(Document) applies the model filter(), restricts output to declared rules, and renders datetimes as ISO 8601 T-format for webhook/realtime/event consumers. Four publish sites (Functions/Http/Functions/Create.php, Functions/Workers/Jobs.php, Workers/Certificates.php, Workers/Functions.php) switch from raw getArrayCopy(getRules()) copies to the new helper, aligning event payloads with the HTTP API response contract. The Functions worker additionally threads the API-set $createdAt through the http trigger path and refreshes $updatedAt in the execution finally block. One issue remains: the schedule trigger branch does not pass the propagated createdAt, so scheduled executions reset their creation time to worker processing time.

Files Change
src/Appwrite/Utopia/Response/Model.php, src/Appwrite/Utopia/Response.php Extract shared Model::payload() and Model::formatDatetime(); Response::output() now delegates datetime formatting to the shared helper
src/Appwrite/Platform/Workers/Functions.php, src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php, src/Appwrite/Platform/Workers/Certificates.php Worker event publishes now emit model-filtered payloads with ISO 8601 datetimes; execution documents preserve API-set createdAt on the http path and set updatedAt on completion
src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php Execution creation action publishes its event through (new Execution())->payload($execution)
tests/e2e/Services/ProjectWebhooks/WebhooksCustomServerTest.php E2E assertions updated for the new ISO 8601 datetime format in webhook payloads

Reviewed commit: f0f789f

@ghost

ghost commented Oct 9, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The payload formatting and timestamp propagation are consistent with the existing response model contract.

This change adds a shared model payload formatter for worker-published events and reuses the response datetime normalization. It also stamps function execution timestamps, preserving the API creation time for queued async HTTP executions, and updates event payload builders and webhook E2E assertions.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 7
File Change
src/Appwrite/Utopia/Response/Model.php Adds model-shaped event payload generation and shared datetime normalization.
src/Appwrite/Utopia/Response.php Uses the shared Model datetime formatter when rendering responses.
src/Appwrite/Platform/Workers/Functions.php Adds execution timestamps to worker-created documents and uses model payload shaping for updates.
src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php Shapes rule-create event payloads through the response model.
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php Shapes deployment-update event payloads through the response model.
src/Appwrite/Platform/Workers/Certificates.php Shapes rule-update event payloads through the response model.
tests/e2e/Services/ProjectWebhooks/WebhooksCustomServerTest.php Checks datetime formatting in deployment and async execution update webhook payloads.

Reviewed f0f789f · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@ChiragAgg5k
ChiragAgg5k merged commit eb0235f into main Oct 9, 2026
64 checks passed
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/execution-event-datetime (after).

Metric Before After Change
🚀 Requests/sec 215.8 209.27 ⚪ -3%
⏱️ Latency P50 79.3 ms 81.79 ms ⚪ +3.1%
⏱️ Latency P95 189.11 ms 197.47 ms ⚪ +4.4%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 81.79 197.47 13,395 209.27 +8.36
Account 163.44 290.75 705 11.67 -4.84
TablesDB 77.94 137.94 7,285 116.77 -5.81
Storage 78.48 163.53 3,525 57.54 +0.7
Functions 124.96 251.41 1,880 31.2 +33.68

Top API waits (after)

API request Max wait (ms)
account.prefs.update 508.42
functions.variables.update 450.67
account.name.update 436.92
functions.delete 390.1
functions.create 364.45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Publish worker event payloads through the response model

1 participant