:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): delete schedules when the live trigger leaves schedule (#127358)
Harley Alexander0e8fe16026bb
diff --git a/products/workflows/backend/services/hog_flow_writes.py b/products/workflows/backend/services/hog_flow_writes.pyindex ea0f31a83317..9dca739130c0 100644--- a/products/workflows/backend/services/hog_flow_writes.py+++ b/products/workflows/backend/services/hog_flow_writes.py@@ -27,6 +27,7 @@ from products.workflows.backend.models.hog_flow.hog_flow import (exit_condition_that_runs,)from products.workflows.backend.models.hog_flow_revision import HogFlowRevision+from products.workflows.backend.models.hog_flow_schedule import SCHEDULED_TRIGGER_TYPESfrom products.workflows.backend.services.action_redirects import compute_action_redirectsfrom products.workflows.backend.services.email_utm_defaults import (flow_has_active_schedule,@@ -350,6 +351,28 @@ def _pause_schedules_on_audience_change(before: HogFlow, after: HogFlow) -> int:)+def _delete_schedules_on_unscheduled_trigger(before: HogFlow, after: HogFlow) -> None:+ """Delete every schedule when the live trigger stops being a scheduled type.++ The scheduler keeps a schedule row through a switch to another trigger and arms it again when+ the trigger switches back. By then the author has usually added a new schedule, so each+ occurrence fires twice. Deleting the row on the switch leaves one schedule per cadence.++ Called wherever the LIVE trigger changes (direct save, graph edit, publish), never for draft+ writes, because a staged trigger change must not stop a schedule that still fires.++ It runs inside the write transaction, while the workflow row is locked, so a concurrent switch+ back cannot add a schedule between the trigger write and the delete. It also requires a+ scheduled trigger before the write, so a save that does not touch the trigger keeps a schedule+ that a staged draft depends on.+ """+ if (before.trigger or {}).get("type") not in SCHEDULED_TRIGGER_TYPES:+ return+ if (after.trigger or {}).get("type") in SCHEDULED_TRIGGER_TYPES:+ return+ after.schedules.all().delete()++def update_workflow(*,team_id: int,@@ -441,6 +464,7 @@ def update_workflow(_save_live(instance, validated_data)if bump:_append_revisions(team_id, user_id, instance, before_update)+ _delete_schedules_on_unscheduled_trigger(before_update, instance)if tags_changed:replace_tags_on_object(tags, instance)@@ -504,6 +528,8 @@ def edit_workflow_content(_save_live(locked, validated_data)if bump:_append_revisions(team_id, user_id, locked, before_update)+ if changes_graph:+ _delete_schedules_on_unscheduled_trigger(before_update, locked)paused = 0if changes_graph and not route_to_draft:@@ -591,6 +617,7 @@ def publish_draft(locked.draft_encrypted_inputs = Nonelocked.save(update_fields=["draft", "draft_updated_at", "draft_encrypted_inputs"])apply_approved_proposals_for_flow(locked)+ _delete_schedules_on_unscheduled_trigger(before_update, locked)_reschedule_timing_edits(team_id, before_update, locked)paused = _pause_schedules_on_audience_change(before_update, locked)diff --git a/products/workflows/backend/tests/api/test_hog_flow.py b/products/workflows/backend/tests/api/test_hog_flow.pyindex 4c190eba2efc..506a53e2a039 100644--- a/products/workflows/backend/tests/api/test_hog_flow.py+++ b/products/workflows/backend/tests/api/test_hog_flow.py@@ -75,6 +75,10 @@ from products.workflows.backend.tests.api.test_workflow_data_suggestions import_AUDIENCE_CONDITION = {"key": "email", "type": "person", "value": "x", "operator": "icontains"}_WIDER_AUDIENCE_CONDITION = {"key": "email", "type": "person", "value": "@", "operator": "icontains"}+_EVENT_TRIGGER_CONFIG = {+ "type": "event",+ "filters": {"events": [{"id": "$pageview", "name": "$pageview", "type": "events", "order": 0}]},+}webhook_template = MOCK_NODE_TEMPLATES[0]@@ -5737,12 +5741,16 @@ class TestHogFlowAPI(APIBaseTest):{"type": "batch", "filters": {"properties": [_AUDIENCE_CONDITION]}},HogFlowSchedule.Status.ACTIVE,),+ ("schedule_trigger_unchanged", {"type": "schedule"}, {"type": "schedule"}, HogFlowSchedule.Status.ACTIVE),+ ("trigger_switched_off_schedule", {"type": "schedule"}, _EVENT_TRIGGER_CONFIG, None),])- def test_live_audience_change_pauses_schedules(self, _name, before_config, after_config, expected_status):+ def test_live_trigger_change_updates_schedules(self, _name, before_config, after_config, expected_status):# Each firing broadcasts to whatever the trigger says at fire time, not to what was confirmed# when the schedule was created, so an edit that moves the audience stops the cadence. A save# that leaves the audience alone must not, or every resave would silently kill the schedule.+ # A trigger that is no longer a scheduled type deletes the schedule, so a switch back cannot+ # arm it next to a new one.hog_flow, _ = self._create_hog_flow_with_action({"template_id": "template-webhook", "inputs": {"url": {"value": "https://example.com"}}})@@ -5764,11 +5772,52 @@ class TestHogFlowAPI(APIBaseTest):)assert edited.status_code == 200, edited.json()- schedule = HogFlowSchedule.objects.get(id=scheduled.json()["id"])+ schedule = HogFlowSchedule.objects.filter(id=scheduled.json()["id"]).first()+ if expected_status is None:+ assert schedule is None+ return+ assert schedule is not Noneassert schedule.status == expected_statusif expected_status == HogFlowSchedule.Status.PAUSED:assert schedule.next_run_at is None+ def test_staged_trigger_change_keeps_schedule_until_publish(self):+ hog_flow, _ = self._create_hog_flow_with_action(+ {"template_id": "template-webhook", "inputs": {"url": {"value": "https://example.com"}}}+ )+ hog_flow["actions"][0]["config"] = {"type": "schedule"}+ hog_flow["status"] = "active"+ created = self.client.post(f"/api/projects/{self.team.id}/hog_flows", hog_flow)+ assert created.status_code == 201, created.json()+ flow_id = created.json()["id"]+ scheduled = self.client.post(+ f"/api/projects/{self.team.id}/hog_flows/{flow_id}/schedules",+ {"rrule": "FREQ=WEEKLY;INTERVAL=1;BYDAY=MO", "starts_at": "2026-08-03T09:00:00Z"},+ )+ assert scheduled.status_code == 201, scheduled.json()++ hog_flow["actions"][0]["config"] = _EVENT_TRIGGER_CONFIG+ staged = self.client.patch(+ f"/api/projects/{self.team.id}/hog_flows/{flow_id}",+ {"actions": hog_flow["actions"], "stage_draft": True},+ )+ assert staged.status_code == 200, staged.json()+ assert HogFlowSchedule.objects.filter(id=scheduled.json()["id"]).exists()++ with patch(+ "products.workflows.backend.presentation.views.hog_flow.get_hog_flow_in_flight_count",+ side_effect=Exception("down"),+ ):+ token = self.client.post(f"/api/projects/{self.team.id}/hog_flows/{flow_id}/publish", {}).json()[+ "confirm_token"+ ]+ published = self.client.post(+ f"/api/projects/{self.team.id}/hog_flows/{flow_id}/publish",+ {"confirm": True, "confirm_token": token},+ )+ assert published.status_code == 200, published.json()+ assert not HogFlowSchedule.objects.filter(id=scheduled.json()["id"]).exists()+@patch("products.workflows.backend.models.hog_flow_batch_job.hog_flow_batch_job.create_batch_hog_flow_job_invocation")