Emit events for deployment create, update, and delete operations#21139
Merged
desertaxle merged 3 commits intomainfrom Mar 17, 2026
Merged
Emit events for deployment create, update, and delete operations#21139desertaxle merged 3 commits intomainfrom
desertaxle merged 3 commits intomainfrom
Conversation
- Add _deployment_related_resources helper to build related resources for deployment events (flow, work-queue, work-pool) - Add deployment_created_event, deployment_updated_event, and deployment_deleted_event factory functions in server/models/events.py - Refactor deployment_status_event to use shared helper - Add emit wrapper functions in server/models/deployments.py - Integrate event emission into create_deployment (upsert), update_deployment, delete_deployment, and delete_deployments (bulk) - Detect changed fields for update events using DEPLOYMENT_EVENT_FIELDS set - Add integration tests for all CRUD event scenarios Closes #20176 Co-authored-by: alex.s <[email protected]> Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
- Use session.expire() instead of session.expunge() to keep ORM objects in the identity map while forcing re-fetch on next access - Update existing test assertions to expect new deployment.created events alongside deployment.ready events Co-authored-by: alex.s <[email protected]> Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9de89417cb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…and expand tracked fields - Use result_deployment.created >= upsert_start to reliably determine whether the upsert was a genuine insert or an ON CONFLICT update, fixing a race condition under concurrent POSTs for the same deployment. - Add version, concurrency_limit_id, and concurrency_options to DEPLOYMENT_EVENT_FIELDS so concurrency limit changes and version updates emit prefect.deployment.updated events. Co-authored-by: alex.s <[email protected]> Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds event emission for deployment CRUD operations (
prefect.deployment.created,prefect.deployment.updated,prefect.deployment.deleted), following the same patterns used for work pool and work queue events.Closes #20176
Changes
src/prefect/server/models/events.py_deployment_related_resourceshelper fromdeployment_status_eventto build related resources (flow, work-queue, work-pool) shared across all deployment eventsdeployment_created_event,deployment_updated_event,deployment_deleted_eventfactory functionssrc/prefect/server/models/deployments.pycreate_deployment: Captures aupsert_starttimestamp before the upsert, then comparesresult_deployment.created >= upsert_startto reliably distinguish create vs update (same pattern used by the API layer for HTTP status codes). Snapshots field values into a plain dict before the upsert for change detection on the update path. Callssession.expire()on the pre-loaded ORM object to prevent stale state after the upsert.update_deployment: Snapshots deployment field values before and after update; emitsupdatedevent with changed field details (from/tovalues). Callssession.expire()after snapshotting.delete_deployment: Reads deployment and builds the delete event before deletion (while the object is still in session); emitsdeletedevent after successful delete.delete_deployments: Loads full deployment objects (instead of just IDs) so delete events can be built before the bulk delete, then emits them after.DEPLOYMENT_EVENT_FIELDSset (includesversion,concurrency_limit_id,concurrency_optionsalongside the original fields),_detect_deployment_changed_fieldshelper, andemit_*wrapper functionstests/server/orchestration/api/test_deployments.pyTestGetScheduledFlowRunsassertions to expect newprefect.deployment.createdevents alongsideprefect.deployment.readyeventsUpdates since last revision
Addressed two review comments:
Race condition in concurrent upserts (P1): Replaced the pre-upsert existence check (
existing_snapshot is None) with a timestamp comparison (result_deployment.created >= upsert_start) to determine create-vs-update. Under concurrent POSTs for the same(flow_id, name), both transactions could previously observe "not found" and both emitcreatedevents. Now the second request correctly detects the row already existed and emits anupdatedevent instead.Expanded tracked fields (P2): Added
version,concurrency_limit_id, andconcurrency_optionstoDEPLOYMENT_EVENT_FIELDSso that concurrency limit changes and version updates emitprefect.deployment.updatedevents.Key review areas
create_deploymentusesresult_deployment.created >= upsert_start(mirroring the API layer's HTTP 201 logic). Relies on timestamp precision not being truncated by the DB below the resolution ofnow("UTC").concurrency_limit_idchange detection: Concurrency limit changes are applied via_create_or_update_deployment_concurrency_limit, which modifies the ORM relationship. The post-upsert re-read (viapopulate_existing=Truein create, orread_deploymentin update) should pick up the newconcurrency_limit_idvalue, but worth verifying this path works end-to-end.create_deploymentadds one pre-upsert query;update_deploymentadds a pre-update read and a post-update read. Verify this performance trade-off is acceptable.session.expire()after snapshotting: Increate_deploymentandupdate_deployment, the existing ORM object is expired after its field values are copied into a plain dict. This prevents stale state when downstream code (e.g._create_or_update_deployment_concurrency_limit) later accesses the object viasession.get(). Verify this doesn't cause unexpected lazy-load behavior.delete_deploymentsnow loads full objects instead of just IDs. The.unique()call was added. Confirm this doesn't change existing return behavior or cause issues at scale.Checklist
<link to issue>"mint.json.Link to Devin session: https://app.devin.ai/sessions/6e878425faa64628980a64c78a432911
Requested by: @desertaxle