Skip to content

Commit 4ba9918

Browse files
committed
add detail desc.
1 parent 007ce22 commit 4ba9918

File tree

1 file changed

+114
-2
lines changed

1 file changed

+114
-2
lines changed

README.md

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55
[![Build status](https://ci.appveyor.com/api/projects/status/i02yxvu0mvhyv5nk?svg=true)](https://ci.appveyor.com/project/icsharp/hangfire-recurringjobextensions)
66
[![License MIT](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT)
77

8-
This repo is the extension for [Hangfire](https://github.com/HangfireIO/Hangfire) to build `RecurringJob` automatically. We can use the attribute `RecurringJobAttribute` to assign the interface/instance/static method.
8+
This repo is the extension for [Hangfire](https://github.com/HangfireIO/Hangfire) to build `RecurringJob` automatically.
99
When app start, `RecurringJob` will be added/updated automatically.
10+
There is two ways to build `RecurringJob`.
11+
12+
- `RecurringJobAttribute` attribute
13+
- Json Configuration
14+
15+
## Using RecurringJobAttribute
16+
17+
We can use the attribute `RecurringJobAttribute` to assign the interface/instance/static method.
18+
1019

1120
```csharp
1221
public class RecurringJobService
@@ -29,4 +38,107 @@ public class RecurringJobService
2938
}
3039
```
3140

32-
More details [here](https://github.com/icsharp/Hangfire.Topshelf).
41+
## Json Configuration
42+
43+
It is similar to [quartz.net](http://www.quartz-scheduler.net/), We also define the unified interface `IRecurringJob`.
44+
Recurring jobs must impl the specified interface like this.
45+
46+
```csharp
47+
[AutomaticRetry(Attempts = 0)]
48+
[DisableConcurrentExecution(90)]
49+
public class LongRunningJob : IRecurringJob
50+
{
51+
public void Execute(PerformContext context)
52+
{
53+
context.WriteLine($"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} LongRunningJob Running ...");
54+
55+
var runningTimes = context.GetJobData<int>("RunningTimes");
56+
57+
context.WriteLine($"get job data parameter-> RunningTimes: {runningTimes}");
58+
59+
var progressBar = context.WriteProgressBar();
60+
61+
foreach (var i in Enumerable.Range(1, runningTimes).ToList().WithProgress(progressBar))
62+
{
63+
Thread.Sleep(1000);
64+
}
65+
}
66+
}
67+
```
68+
69+
Now we need to provider the json config file to assign the implemented recurring job, the json configuration samples as below.
70+
71+
```json
72+
[{
73+
"job-name": "My Job1",
74+
"job-type": "Hangfire.Samples.MyJob1, Hangfire.Samples",
75+
"cron-expression": "*/1 * * * *",
76+
"timezone": "China Standard Time",
77+
"queue": "jobs"
78+
},
79+
{
80+
"job-name": "My Job2",
81+
"job-type": "Hangfire.Samples.MyJob2, Hangfire.Samples",
82+
"cron-expression": "*/5 * * * *",
83+
"job-data": {
84+
"IntVal": 1,
85+
"StringVal": "abcdef",
86+
"BooleanVal": true,
87+
"SimpleObject": {
88+
"Name": "Foo",
89+
"Age": 100
90+
}
91+
}
92+
},
93+
{
94+
"job-name": "Long Running Job",
95+
"job-type": "Hangfire.Samples.LongRunningJob, Hangfire.Samples",
96+
"cron-expression": "*/2 * * * *",
97+
"job-data": {
98+
"RunningTimes": 300
99+
}
100+
}]
101+
```
102+
103+
The json token description to the configuration is here.
104+
105+
JSON Token | Description
106+
---|---
107+
**job-name** | *[required]* The job name to `RecurringJob`.
108+
**job-type** | *[required]* The job type while impl the interface `IRecurringJob`.
109+
**cron-expression** | *[required]* Cron expressions.
110+
timezone | *[optional]* Default value is `TimeZoneInfo.Local`.
111+
queue | *[optional]* The specified queue name , default value is `default`.
112+
job-data | *[optional]* Likely to the [quartz.net](http://www.quartz-scheduler.net/) `JobDataMap`, it is can be deserialized to the type `Dictionary<string,object>`.
113+
enable | *[optional]* Whether the `RecurringJob` can be added/updated, default value is true, if false `RecurringJob` will be deleted automatically.
114+
115+
*To the json token `job-data`, we can use extension method to get data with specified key from `PerformContext` when recurring job running.*
116+
117+
```csharp
118+
var runningTimes = context.GetJobData<int>("RunningTimes");
119+
```
120+
121+
## Building RecurringJob
122+
123+
Finally, we can use extension method `UseRecurringJob` to build `RecurringJob`. In .NET Core's Startup.cs.
124+
125+
``` csharp
126+
public void ConfigureServices(IServiceCollection services)
127+
{
128+
services.AddHangfire(x =>
129+
{
130+
x.UseSqlServerStorage(_config.GetConnectionString("Hangfire"));
131+
132+
x.UseConsole();
133+
134+
//using json config file to build RecurringJob automatically.
135+
x.UseRecurringJob("recurringjob.json");
136+
//using RecurringJobAttribute to build RecurringJob automatically.
137+
x.UseRecurringJob(typeof(RecurringJobService));
138+
139+
x.UseDefaultActivator();
140+
});
141+
}
142+
```
143+
144+
*For the json configuration file, we can monitor the file change and reload `RecurringJob` dynamically by passing the parameter `reloadOnChange = true`.*

0 commit comments

Comments
 (0)