Skip to content

Commit 09787bb

Browse files
committed
more docs
1 parent c7d2bb4 commit 09787bb

File tree

1 file changed

+73
-9
lines changed

1 file changed

+73
-9
lines changed

MyApp/_pages/releases/v8_04.md

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ the following options:
196196
- `ReplyTo` - Optional field for capturing where to send notification for completion of a Job
197197
- `Args` - Optional String Dictionary of Arguments that can be attached to a Job
198198

199-
### Schedule Recurring Tasks
199+
## Schedule Recurring Tasks
200200

201201
In addition to queueing jobs to run in the background, it also supports scheduling recurring tasks
202202
to execute APIs or Commands at fixed intervals.
@@ -208,15 +208,73 @@ Schedule your Reoccurring Tasks with Background Jobs!
208208
APIs and Commands can be scheduled to run at either a `TimeSpan` or
209209
[CRON Expression](https://github.com/HangfireIO/Cronos?tab=readme-ov-file#cron-format) interval, e.g:
210210

211+
212+
### CRON Expression Examples
213+
211214
```csharp
215+
// Every Minute Expression
212216
jobs.RecurringCommand<CheckUrlsCommand>(Schedule.Cron("* * * * *"));
213-
jobs.RecurringCommand<CheckUrlsCommand>(
214-
Schedule.Interval(TimeSpan.FromMinutes(1)));
215217

218+
// Every Minute Constant
216219
jobs.RecurringCommand<CheckUrlsCommand>(Schedule.EveryMinute, new CheckUrls {
217220
Urls = urls
218221
});
222+
```
223+
224+
### CRON Format
225+
226+
You can use any **unix-cron format** expression supported by the [HangfireIO/Cronos](https://github.com/HangfireIO/Cronos) library:
227+
228+
```txt
229+
|------------------------------- Minute (0-59)
230+
| |------------------------- Hour (0-23)
231+
| | |------------------- Day of the month (1-31)
232+
| | | |------------- Month (1-12; or JAN to DEC)
233+
| | | | |------- Day of the week (0-6; or SUN to SAT; or 7 for Sunday)
234+
| | | | |
235+
| | | | |
236+
* * * * *
237+
```
238+
239+
The allowed formats for each field include:
240+
241+
| Field | Format of valid values |
242+
|------------------|--------------------------------------------|
243+
| Minute | 0-59 |
244+
| Hour | 0-23 |
245+
| Day of the month | 1-31 |
246+
| Month | 1-12 (or JAN to DEC) |
247+
| Day of the week | 0-6 (or SUN to SAT; or 7 for Sunday) |
248+
249+
#### Matching all values
250+
251+
To match all values for a field, use the asterisk: `*`, e.g here are two examples in which the minute field is left unrestricted:
252+
253+
- `* 0 1 1 1` - the job runs every minute of the midnight hour on January 1st and Mondays.
254+
- `* * * * *` - the job runs every minute (of every hour, of every day of the month, of every month, every day of the week, because each of these fields is unrestricted too).
255+
256+
#### Matching a range
257+
258+
To match a range of values, specify your start and stop values, separated by a hyphen (-). Do not include spaces in the range. Ranges are inclusive. The first value must be less than the second.
259+
260+
The following equivalent examples run at midnight on Mondays, Tuesdays, Wednesdays, Thursdays, and Fridays (for all months):
219261

262+
- `0 0 * * 1-5`
263+
- `0 0 * * MON-FRI`
264+
265+
#### Matching a list
266+
267+
Lists can contain any valid value for the field, including ranges. Specify your values, separated by a comma (,). Do not include spaces in the list, e.g:
268+
269+
- `0 0,12 * * *` - the job runs at midnight and noon.
270+
- `0-5,30-35 * * * *` - the job runs in each of the first five minutes of every half hour (at the top of the hour and at half past the hour).
271+
272+
### TimeSpan Interval Examples
273+
274+
```csharp
275+
jobs.RecurringCommand<CheckUrlsCommand>(Schedule.Interval(TimeSpan.FromMinutes(1)));
276+
277+
// With Example
220278
jobs.RecurringApi(Schedule.Interval(TimeSpan.FromMinutes(1)), new CheckUrls {
221279
Urls = urls
222280
});
@@ -235,13 +293,12 @@ jobs.RecurringCommand<CheckUrlsCommand>("Check URLs", Schedule.EveryMinute,
235293
If no name is provided, the Command's Name or APIs Request DTO will be used
236294
:::
237295

296+
### Idempotent Registration
297+
238298
Scheduled Tasks are idempotent where the same registration with the same name will
239299
either create or update the scheduled task registration without losing track of the
240-
last time the Recurring Task was run which is also viewable in the Jobs Admin UI:
241-
242-
![](/img/pages/jobs/jobs-scheduled-tasks-last-job.webp)
243-
244-
As such it's recommended to always define your App's Scheduled Tasks on Startup:
300+
last time the Recurring Task, as such it's recommended to always define your App's
301+
Scheduled Tasks on Startup:
245302

246303
```csharp
247304
public class ConfigureBackgroundJobs : IHostingStartup
@@ -255,12 +312,19 @@ public class ConfigureBackgroundJobs : IHostingStartup
255312
var services = appHost.GetApplicationServices();
256313

257314
var jobs = services.GetRequiredService<IBackgroundJobs>();
258-
// Register Scheduled Tasks
315+
316+
// App's Scheduled Tasks Registrations:
259317
jobs.RecurringCommand<MyCommand>(Schedule.Hourly);
260318
});
261319
}
262320
```
263321

322+
### Background Jobs Admin UI
323+
324+
The last job the Recurring Task ran is also viewable in the Jobs Admin UI:
325+
326+
![](/img/pages/jobs/jobs-scheduled-tasks-last-job.webp)
327+
264328
### Executing non-durable jobs
265329

266330
`IBackgroundJobs` also supports `RunCommand` methods to be able to execute jobs transiently

0 commit comments

Comments
 (0)