Skip to content

Canceled active automation runs when an automation is disabled#28818

Open
EvanHahn wants to merge 1 commit into
mainfrom
ny1358-cancel-active-automation-runs
Open

Canceled active automation runs when an automation is disabled#28818
EvanHahn wants to merge 1 commit into
mainfrom
ny1358-cancel-active-automation-runs

Conversation

@EvanHahn

Copy link
Copy Markdown
Contributor

closes https://linear.app/ghost/issue/NY-1358

When an automation is marked inactive, we now cancel any active runs.

How it works

To do this, we:

  • Mark all pending steps canceled.
  • If an automation is deactivated while a step is running:
    • Don't enqueue subsequent steps.
    • Don't retry failed steps.

Note that, if a step is already locked and running, we may not be able to stop it.

Manual test

In addition to automated tests, I manually tested this by:

  1. Temporarily set automations.fakeWaitHoursMultiplier to 2500 (one day becomes one minute).
  2. Created a "send email, wait 2 days, send email" automation.
  3. Signed up a member and got the first email.
  4. Deactivated then reactivated the automation.
  5. Verified that I never received the second email.

I also ran through this without deactivating the automation and made sure I got both emails.

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 999fa315-03ca-4e85-9d4a-aceec7d8c1c2

📥 Commits

Reviewing files that changed from the base of the PR and between b8df697 and cc1cc41.

📒 Files selected for processing (4)
  • ghost/core/core/server/services/automations/database-automations-repository.ts
  • ghost/core/core/server/services/automations/stale-lock-cutoff.ts
  • ghost/core/test/unit/server/services/automations/automations-repository.test.ts
  • ghost/core/test/unit/server/services/automations/stale-lock-cutoff.test.ts
🚧 Files skipped from review as they are similar to previous changes (4)
  • ghost/core/test/unit/server/services/automations/stale-lock-cutoff.test.ts
  • ghost/core/core/server/services/automations/stale-lock-cutoff.ts
  • ghost/core/test/unit/server/services/automations/automations-repository.test.ts
  • ghost/core/core/server/services/automations/database-automations-repository.ts

Walkthrough

A new getStaleLockCutoff(now) utility is extracted into its own file, computing a stale-lock cutoff by subtracting LOCK_TIMEOUT_MS from a given timestamp. The repository replaces its inline cutoff calculation with this helper in fetchAndLockSteps. The edit transaction now cancels pending (unlocked or expired-lock) automation run steps by marking them 'automation disabled' when an automation's status transitions to 'inactive', using a new cancelCancelablePendingStepsForAutomation bulk-update helper. New isStepRunAutomationActive / isRunAutomationActive / selectExists helpers are added, and early guards using them are inserted into finishStepAndEnqueueNext (returns null if inactive) and retryStep (marks step terminal as 'automation disabled' and returns false if inactive). Unit tests cover all new behaviors.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: canceling active automation runs when an automation is disabled.
Description check ✅ Passed The description clearly explains the purpose, implementation approach, and manual testing methodology, all directly related to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ny1358-cancel-active-automation-runs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nx-cloud

nx-cloud Bot commented Jun 23, 2026

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix

Ensure the fix-ci command is configured to always run in your CI pipeline to get automatic fixes in future runs. For more information, please see https://nx.dev/ci/features/self-healing-ci


View your CI Pipeline Execution ↗ for commit b8df697

Command Status Duration Result
nx run ghost:test:ci:integration ✅ Succeeded 2m 17s View ↗
nx run ghost:test:ci:integration:no-coverage ✅ Succeeded 1m 27s View ↗
nx build @tryghost/activitypub ✅ Succeeded 1s View ↗
nx build @tryghost/admin-toolbar ✅ Succeeded <1s View ↗
nx build @tryghost/sodo-search ✅ Succeeded <1s View ↗
nx build @tryghost/comments-ui ✅ Succeeded <1s View ↗
nx build @tryghost/announcement-bar ✅ Succeeded <1s View ↗
nx build @tryghost/signup-form ✅ Succeeded <1s View ↗
Additional runs (10) ✅ Succeeded ... View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-06-23 00:53:51 UTC

closes https://linear.app/ghost/issue/NY-1358

When an automation is marked inactive, we now cancel any active runs.

To do this, we:

- Mark all pending steps canceled.
- If an automation is deactivated while a step is running:
    - Don't enqueue subsequent steps.
    - Don't retry failed steps.

Note that, if a step is already locked and running, we may not be able
to stop it.

In addition to automated tests, I manually tested this by:

1. Temporarily set `automations.fakeWaitHoursMultiplier` to 2500 (one
   day becomes one minute).
2. Created a "send email, wait 2 days, send email" automation.
3. Signed up a member and got the first email.
4. Deactivated then reactivated the automation.
5. Verified that I never received the second email.

I also ran through this *without* deactivating the automation and made
sure I got both emails.
@EvanHahn EvanHahn force-pushed the ny1358-cancel-active-automation-runs branch from b8df697 to cc1cc41 Compare June 23, 2026 00:46
@EvanHahn EvanHahn requested a review from cmraible June 23, 2026 01:06
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.

1 participant