Skip to content

Moved automations service to separate file#28343

Merged
EvanHahn merged 1 commit into
mainfrom
move-automationsservice-to-service
Jun 3, 2026
Merged

Moved automations service to separate file#28343
EvanHahn merged 1 commit into
mainfrom
move-automationsservice-to-service

Conversation

@EvanHahn
Copy link
Copy Markdown
Contributor

@EvanHahn EvanHahn commented Jun 3, 2026

towards https://linear.app/ghost/issue/NY-1286
ref #28120

This change should have no impact on functionality.

We have a lint rule that limits the length of index.js files. In an upcoming change, the automations service will cross that limit.

This patch moves it into a separate file. Doing so also makes it a little easier to test.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

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: f7e2262d-b7e5-4703-b3c2-345c21129f88

📥 Commits

Reviewing files that changed from the base of the PR and between fa3995c and 2eb8a65.

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

Walkthrough

This PR extracts the AutomationsService implementation from ghost/core/core/server/services/automations/index.js into a new dedicated service.js module. The entry point is updated to import and export the service as a singleton instance. Test setup is refactored to instantiate the service class directly per test run instead of manipulating module cache. The observable behavior—domain event subscription, poll enqueueing, scheduler registration, and token-signed scheduling—remains unchanged across the refactoring.

Possibly related PRs

  • TryGhost/Ghost#28170: Modifies StartAutomationsPollEvent subscription to call the new poll skeleton, directly connected to this PR's event-wiring refactor.
  • TryGhost/Ghost#27909: Refactors automations polling to consolidate "poll now vs poll later" into enqueuePollAt(date) with follow-up scheduling through enqueueAnotherPollAt.
  • TryGhost/Ghost#28263: Adds new member_sign_up automation triggering that dispatches StartAutomationsPollEvent, coupled to this PR's poll-start event flow refactor.

Suggested labels

ok to merge for me

Suggested reviewers

  • troyciesco
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: moving the AutomationsService class from index.js to a separate service.js file.
Description check ✅ Passed The description clearly relates to the changeset, explaining the motivation (lint rule compliance) and confirming no functional impact is intended.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 move-automationsservice-to-service

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 and usage tips.

@EvanHahn EvanHahn force-pushed the move-automationsservice-to-service branch from fa3995c to 0a6768c Compare June 3, 2026 17:39
towards https://linear.app/ghost/issue/NY-1286
ref #28120

This change should have no impact on functionality.

We have [a lint rule that limits the length of `index.js` files][0]. In
[an upcoming change][1], the automations service will cross that limit.

This patch moves it into a separate file. Doing so also makes it a
little easier to test.

[0]: https://github.com/TryGhost/eslint-plugin-ghost/blob/22d154a8e3807e4e30219fb8449e7aa9b90a110b/lib/config/node.js#L32-L37
[1]: #28120
@EvanHahn EvanHahn force-pushed the move-automationsservice-to-service branch from 0a6768c to 2eb8a65 Compare June 3, 2026 17:40
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Git makes this diff look huge! Here's what actually changed in this file (it's just one line!):

-module.exports = new AutomationsService();
+module.exports = AutomationsService;

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

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

48-54: ⚡ Quick win

Cover all init() side effects in the idempotence test.

This currently proves only that subscriptions stay at two. A regression that re-dispatches the initial poll or re-registers with schedulerAdapter on later init() calls would still pass, even though both are part of the extracted service contract.

💡 Suggested test expansion
         it('subscribes each poller only once when init is called multiple times', function () {
             automations.init(initOptions);
             automations.init(initOptions);
             automations.init(initOptions);
             automations.init(initOptions);
+
             sinon.assert.calledTwice(domainEvents.subscribe);
+            sinon.assert.calledOnce(domainEvents.dispatch);
+            sinon.assert.calledOnceWithExactly(schedulerAdapter.register, automations);
         });
🤖 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 48 - 54, The test should assert all idempotent side effects of
automations.init: after calling automations.init(initOptions) multiple times,
verify domainEvents.subscribe is still called twice, schedulerAdapter.register
is called only once, and the pollers' startup is not re-dispatched by spying on
the poller start method (e.g., Poller.prototype.start or the concrete poller
start function used) and asserting it was called only once; add these additional
assertions to the existing test so it fails if init() re-registers with
schedulerAdapter or re-dispatches the initial poll on subsequent calls.

4-4: ⚡ Quick win

Drop the import change; focus on strengthening init() idempotence assertions

This suite already imports ghost/core/server/services/automations/service directly, so the .constructor/wrapper coupling concern (and related diff) isn’t applicable. Strengthen the init() idempotence test to also assert that observable side effects like schedulerAdapter.register and the initial poll/dispatch happen only on the first init() call.

🤖 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` at line 4,
The test incorrectly changed the import; revert any import modifications and
keep requiring the concrete service module (restore original require), then
strengthen the init() idempotence test on AutomationsService by asserting
observable side effects only occur once: call service.init() twice and verify
schedulerAdapter.register was called exactly once and that the initial
poll/dispatch (the code path that triggers scheduling/polling) ran only on the
first call (use spies/mocks on schedulerAdapter.register and the poll/dispatch
entry point used by AutomationsService to assert single invocation). Ensure you
reference AutomationsService and its init() method along with
schedulerAdapter.register and the initial poll/dispatch function when locating
where to add the assertions.
🤖 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 48-54: The test should assert all idempotent side effects of
automations.init: after calling automations.init(initOptions) multiple times,
verify domainEvents.subscribe is still called twice, schedulerAdapter.register
is called only once, and the pollers' startup is not re-dispatched by spying on
the poller start method (e.g., Poller.prototype.start or the concrete poller
start function used) and asserting it was called only once; add these additional
assertions to the existing test so it fails if init() re-registers with
schedulerAdapter or re-dispatches the initial poll on subsequent calls.
- Line 4: The test incorrectly changed the import; revert any import
modifications and keep requiring the concrete service module (restore original
require), then strengthen the init() idempotence test on AutomationsService by
asserting observable side effects only occur once: call service.init() twice and
verify schedulerAdapter.register was called exactly once and that the initial
poll/dispatch (the code path that triggers scheduling/polling) ran only on the
first call (use spies/mocks on schedulerAdapter.register and the poll/dispatch
entry point used by AutomationsService to assert single invocation). Ensure you
reference AutomationsService and its init() method along with
schedulerAdapter.register and the initial poll/dispatch function when locating
where to add the assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 46ea6ca6-99d6-4f54-985e-fb79ce2c23f9

📥 Commits

Reviewing files that changed from the base of the PR and between 87afad8 and fa3995c.

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

@EvanHahn EvanHahn enabled auto-merge (squash) June 3, 2026 17:43
@EvanHahn EvanHahn merged commit 87b99d0 into main Jun 3, 2026
50 checks passed
@EvanHahn EvanHahn deleted the move-automationsservice-to-service branch June 3, 2026 18:01
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