Skip to content

Tested automation poll enqueueing - #29026

Merged
EvanHahn merged 5 commits into
mainfrom
test-automation-service-enqueuepollat
Jul 6, 2026
Merged

Tested automation poll enqueueing#29026
EvanHahn merged 5 commits into
mainfrom
test-automation-service-enqueuepollat

Conversation

@EvanHahn

@EvanHahn EvanHahn commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

towards https://linear.app/ghost/issue/NY-1396

I recommend reviewing this commit-by-commit.

This change should have no user impact.

As part of our work to make automation polling idempotent, I'd like to test a previously-untested part of the code.

@coderabbitai

coderabbitai Bot commented Jul 1, 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: ed711a41-b61e-4f7c-aef3-1ca27bff3360

📥 Commits

Reviewing files that changed from the base of the PR and between 654a178 and 205a9ef.

📒 Files selected for processing (1)
  • ghost/core/test/unit/server/services/automations/service.test.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • ghost/core/test/unit/server/services/automations/service.test.js

Walkthrough

The AutomationsService poll re-arming mechanism changes from a synchronous enqueue-now callback to an async enqueue-at callback. init() now starts scheduling through the async callback, rescheduleAll() returns a Promise, and a test-only helper exposes enqueue-at behavior for specific dates. Unit tests now await event-loop flushing and cover past, now, and future enqueue paths.

Changes

Suggested reviewers: sam-lord

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant AutomationsService
  participant SchedulerAdapter

  Caller->>AutomationsService: rescheduleAll()
  AutomationsService->>AutomationsService: await `#enqueuePollAt`(date)
  AutomationsService->>SchedulerAdapter: schedule(date)
  SchedulerAdapter-->>AutomationsService: scheduled
  AutomationsService-->>Caller: Promise resolved
Loading

Possibly related PRs

  • TryGhost/Ghost#29023: Both PRs modify the same #enqueuePollNow/rescheduleAll logic in AutomationsService.
  • TryGhost/Ghost#29024: Both PRs rework init() and rescheduleAll() around the internal poll enqueue callback in the same service file.
  • TryGhost/Ghost#29025: Both PRs modify the enqueuePollAt re-arming behavior and flushEventLoop usage in the same test suite.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is clear, concise, and matches the main change: added tests around automation poll enqueueing.
Description check ✅ Passed The description is directly related to the changeset and correctly frames it as testing a previously untested automation polling flow.
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.
✨ 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 test-automation-service-enqueuepollat

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

@EvanHahn
EvanHahn force-pushed the test-automation-service-enqueuepollat branch from 8856a82 to fbd3897 Compare July 1, 2026 22:36
@EvanHahn
EvanHahn changed the base branch from main to cleanup-automations-enqueuepoll July 1, 2026 22:36
@nx-cloud

nx-cloud Bot commented Jul 1, 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 205a9ef

Command Status Duration Result
nx run ghost:test:ci:integration ✅ Succeeded 2m 49s View ↗
nx run ghost:test:legacy ✅ Succeeded 3m 3s View ↗
nx run ghost:test:integration ✅ Succeeded 2m 33s View ↗
nx run-many --target=build --projects=tag:publi... ✅ Succeeded <1s View ↗
nx run-many -t test:unit -p ghost ✅ Succeeded 30s View ↗
nx run ghost:test:e2e ✅ Succeeded 2m 15s View ↗
nx run @tryghost/admin:build ✅ Succeeded 11s View ↗
nx run ghost:build:assets ✅ Succeeded 2s View ↗
Additional runs (2) ✅ Succeeded ... View ↗

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


☁️ Nx Cloud last updated this comment at 2026-07-06 15:39:10 UTC

@EvanHahn
EvanHahn marked this pull request as ready for review July 1, 2026 23:13
@EvanHahn
EvanHahn requested a review from Copilot July 1, 2026 23:13

Copilot AI 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.

Pull request overview

Adds unit coverage for the automations poll enqueueing path (past/now vs. future scheduling), and adjusts the automations rescheduler behavior to be awaitable so tests can reliably observe event dispatch timing. This supports the broader effort to make automation polling idempotent (NY-1396) by exercising previously untested scheduling logic.

Changes:

  • Use node:timers/promises setImmediate() in unit tests to reliably flush the macrotask queue.
  • Add tests covering enqueueing behavior for past/now (dispatch in-process poll) vs. future (schedule via scheduler adapter).
  • Update AutomationsService.rescheduleAll() to be async and add a test-only enqueue helper.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ghost/core/test/unit/server/services/automations/service.test.js Adds unit tests for poll enqueueing behavior and updates existing tests to await macrotask flushing.
ghost/core/core/server/services/automations/service.ts Makes rescheduling awaitable and exposes a test-only hook to exercise enqueue logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ghost/core/core/server/services/automations/service.ts
Comment thread ghost/core/core/server/services/automations/service.ts
@EvanHahn
EvanHahn requested a review from troyciesco July 2, 2026 16:26
Base automatically changed from cleanup-automations-enqueuepoll to main July 6, 2026 11:44
EvanHahn added 2 commits July 6, 2026 08:11
Notably, `rescheduleAll` now returns a promise. That's okay--we `await`
the result anyway. See `SchedulingBase.prototype.rescheduleAll`.
@EvanHahn
EvanHahn force-pushed the test-automation-service-enqueuepollat branch from fbd3897 to 654a178 Compare July 6, 2026 13:12
@cursor

cursor Bot commented Jul 6, 2026

Copy link
Copy Markdown

Bugbot is not enabled for this team, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@EvanHahn
EvanHahn enabled auto-merge (squash) July 6, 2026 13:12

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
ghost/core/test/unit/server/services/automations/service.test.js (1)

78-87: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

"date is now" test relies on real wall-clock timing.

Using new Date() directly ties the "now" boundary test to actual elapsed time between construction and the internal now-vs-future comparison inside __testOnlyEnqueuePollAt. This is inherently racy at the exact millisecond boundary; the "past" test already exercises the dispatch-not-schedule path, so this test's specific value (verifying the boundary itself) is undermined by not controlling time deterministically (e.g., via sinon.useFakeTimers).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ghost/core/test/unit/server/services/automations/service.test.js` around
lines 78 - 87, The StartAutomationsPollEvent boundary test in automations
service is relying on the real clock via new Date(), which makes it racy. Update
the test around __testOnlyEnqueuePollAt to control time deterministically,
preferably with sinon.useFakeTimers or a fixed timestamp, so the “date is now”
case is evaluated against a stable mocked now. Keep the existing assertions on
domainEvents.dispatch and schedulerAdapter.schedule, but ensure the test setup
explicitly freezes time before invoking __testOnlyEnqueuePollAt.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@ghost/core/test/unit/server/services/automations/service.test.js`:
- Around line 78-87: The StartAutomationsPollEvent boundary test in automations
service is relying on the real clock via new Date(), which makes it racy. Update
the test around __testOnlyEnqueuePollAt to control time deterministically,
preferably with sinon.useFakeTimers or a fixed timestamp, so the “date is now”
case is evaluated against a stable mocked now. Keep the existing assertions on
domainEvents.dispatch and schedulerAdapter.schedule, but ensure the test setup
explicitly freezes time before invoking __testOnlyEnqueuePollAt.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: db28f623-d169-4aec-a4a6-6dc5756dad86

📥 Commits

Reviewing files that changed from the base of the PR and between 29ebce9 and 654a178.

📒 Files selected for processing (2)
  • ghost/core/core/server/services/automations/service.ts
  • ghost/core/test/unit/server/services/automations/service.test.js

@EvanHahn
EvanHahn disabled auto-merge July 6, 2026 14:07
@cursor

cursor Bot commented Jul 6, 2026

Copy link
Copy Markdown

Bugbot is not enabled for this team, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@EvanHahn
EvanHahn enabled auto-merge (squash) July 6, 2026 14:12
@cursor

cursor Bot commented Jul 6, 2026

Copy link
Copy Markdown

Bugbot is not enabled for this team, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@EvanHahn
EvanHahn merged commit a240e61 into main Jul 6, 2026
43 checks passed
@EvanHahn
EvanHahn deleted the test-automation-service-enqueuepollat branch July 6, 2026 15:49
cmraible pushed a commit that referenced this pull request Jul 8, 2026
closes https://linear.app/ghost/issue/NY-1405

**Depends on #29026.**

## What

In addition to telling the scheduler when to do an automation poll, also
use an in-memory timer.

This change should have no user impact.

## Why

1. [Soon][NY-1396], we plan to reduce the precision of requests to the
scheduler. Instead of telling the scheduler *exactly* when to poll,
we'll round to the nearest 30 minutes (with some per-site jitter). This
will reduce load on the scheduler and will boot Pro servers less.
1. If the scheduler service is down, polls will still happen at the
right time if the server is up.

[NY-1396]:
https://linear.app/ghost/issue/NY-1396/make-automations-poll-arming-idempotent-in-scheduler
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.

3 participants