-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: recurring jobs #12
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| --- | ||
| outline: deep | ||
| title: Recurring Jobs | ||
| description: How to schedule recurring jobs with Sidequest.js using cron expressions. | ||
| --- | ||
|
|
||
| # Recurring Jobs (Scheduling with Cron) | ||
|
|
||
| Sidequest supports scheduling jobs to run automatically at recurring intervals using cron expressions. This allows you to trigger background jobs on a fixed schedule—without manual intervention—directly from your code, with just one line. | ||
|
|
||
| ## Quick Example | ||
|
|
||
| ```ts | ||
| import { Sidequest } from "sidequest"; | ||
| import { MyJob } from "./jobs/my-job"; | ||
|
|
||
| // Schedule MyJob to run every 10 seconds | ||
| Sidequest.build(MyJob).schedule("*/10 * * * * *"); | ||
| ``` | ||
|
|
||
| This schedules a new instance of `MyJob` to be enqueued every 10 seconds. | ||
|
|
||
| ## How Scheduling Works | ||
|
|
||
| - Scheduling is **in-memory** only: schedules are not persisted to the database. | ||
| - Schedules must be registered each time your application starts. Typically, you should place schedule calls in your application bootstrap/startup logic. | ||
| - Each time the cron expression matches, a new job is enqueued with the arguments you specify (if any). | ||
| - By default, jobs are enqueued in the "default" queue unless configured otherwise. | ||
| - Sidequest uses [`node-cron`](https://www.npmjs.com/package/node-cron) for robust cron parsing and execution. | ||
|
|
||
| ## When Should I Use Scheduling? | ||
|
|
||
| Use recurring jobs for tasks like: | ||
|
|
||
| - Periodic data synchronization | ||
| - Sending notifications or reminders | ||
| - Cleanup and maintenance routines | ||
| - Any background process that needs to run on a schedule | ||
|
|
||
| ## Arguments and Examples | ||
|
|
||
| ### `schedule(cronExpression: string, ...args: any[]): void` | ||
|
|
||
| Schedules the job to be enqueued automatically according to a cron expression. | ||
|
|
||
| #### Parameters | ||
|
|
||
| - `cronExpression` (`string`): A valid cron expression (see [node-cron docs](https://www.npmjs.com/package/node-cron#cron-syntax)), e.g. `'0 * * * *'` for every hour or `'*/10 * * * * *'` for every 10 seconds. | ||
| - `...args`: Arguments passed to the job's `run` method each time it is enqueued. | ||
|
|
||
| #### Example | ||
|
|
||
| ```ts | ||
| Sidequest.build(MyJob).schedule("0 * * * *"); // Every hour | ||
| Sidequest.build(MyJob).schedule("*/5 * * * * *", "foo"); // Every 5 seconds with argument | ||
| ``` | ||
|
|
||
| #### Throws | ||
|
|
||
| - Throws an error if the cron expression is invalid. | ||
|
|
||
| #### Notes | ||
|
|
||
| - **In-memory only:** Scheduled tasks are NOT persisted in the database. You must re-register schedules on every app startup. | ||
| - **No overlap:** Sidequest uses `noOverlap: true` by default—if a previous run is still in progress, a new job will not be enqueued for that tick. | ||
|
|
||
| ## Limitations and Recommendations | ||
|
|
||
| - **Persistence:** If your application restarts, any scheduled jobs must be re-scheduled via code. (This is by design and similar to other popular job libraries.) | ||
| - **Clustering:** In a multi-instance environment, each instance will create its own scheduled jobs unless you coordinate or restrict scheduling to a single node. | ||
| To avoid duplicate executions, we recommend enabling job uniqueness with a period window (e.g., “unique per hour” or “unique per minute”). | ||
| This ensures that even if multiple nodes schedule the same job, only one will actually run for each interval. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.