Skip to content

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.

@AlessioGr AlessioGr enabled auto-merge (squash) April 23, 2025 13:26
@jessrynkar jessrynkar self-requested a review April 23, 2025 13:28
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 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
@github-actions
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