Skip to content

fix: safely shut down crons using new payload.destroy method, improve jobs autorun docs, adds e2e test #12196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 5, 2025

Conversation

AlessioGr
Copy link
Member

@AlessioGr AlessioGr commented Apr 23, 2025

Safely shut down cronjobs

When Payload is configured to automatically create cron jobs (e.g., via config.jobs.autoRun), our CI began experiencing frequent MongoDB and SQLite connection errors. This stemmed from two core issues:

  • Integration tests are executed in a single process across all test suites. If one suite schedules cron jobs, those jobs continue running even as the next test suite starts - even if that suite has no autoRun jobs configured.
  • When Jest shuts down, it tears down the in-memory database in the same process as Payload. If a cron job runs during this shutdown and performs a DB operation, it can trigger a race condition, leading to flaky test failures.

Fixes:

  • MongoMemoryServer shutdown is now moved to a separate process, ensuring that teardown happens cleanly after the Payload/Jest process exits.
  • Introduces a new payload.destroy() method, which gracefully shuts down all registered cron jobs before calling payload.db.destroy(). This ensures no cron jobs persist across test suites, as payload.destroy() is now invoked in the afterAll() hook of each suite.

Docs improvement

Fixes #12658

Clarifies that jobs.autoRun only runs existing jobs already in the queue - it does not enqueue jobs itself.

Adds e2e test coverage

Adds an e2e test to verify jobs.autoRun functionality, which previously had no test coverage.

Verified

This commit was signed with the committer’s verified signature.
AlessioGr Alessio Gravili

Verified

This commit was signed with the committer’s verified signature.
AlessioGr Alessio Gravili
@AlessioGr AlessioGr enabled auto-merge (squash) April 23, 2025 13:26
@jessrynkar jessrynkar self-requested a review April 23, 2025 13:28

Verified

This commit was signed with the committer’s verified signature.
AlessioGr Alessio Gravili
jessrynkar
jessrynkar previously approved these changes Apr 23, 2025
@AlessioGr AlessioGr disabled auto-merge April 23, 2025 13:30
@AlessioGr AlessioGr enabled auto-merge (squash) April 23, 2025 13:30
AlessioGr added 2 commits May 30, 2025 15:40
AlessioGr added 10 commits June 1, 2025 16:53
fix
@AlessioGr AlessioGr changed the title docs: improve jobs autorun docs, adds e2e test fix: safely shut down crons using new payload.destroy method, improve jobs autorun docs, adds e2e test Jun 5, 2025
@AlessioGr AlessioGr merged commit 7c05c77 into main Jun 5, 2025
81 checks passed
@AlessioGr AlessioGr deleted the docs/autorun branch June 5, 2025 16:19
Copy link
Contributor

github-actions bot commented Jun 9, 2025

🚀 This is included in version v3.42.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Queues of jobs cant be started.
3 participants