利用BackgroundService封装后台服务

在之前的文章 使用 BackgroundService 类在微服务中实现后台任务 中有介绍到如何利用 BackgroundService 来实现后台服务,这里我们依旧利用 BackgroundService 来进行类似 hangfire 的封装。

OdinPlugs.OdinHostedService 使用方法

1 后台任务 - 普通任务,立即执行,只执行一次
1
2
3
4
5
6
7
8
9
services.AddOdinBgServiceNomalJob(opt =>
{
opt.ActionJob = () =>
{
#if DEBUG
Log.Information($"Service:【 BgService - Nomal - Job - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
};
});
2 后台任务 - 延迟调用,只执行一次
1
2
3
4
5
6
7
8
9
10
services.AddOdinBgServiceScheduleJob(opt =>
{
opt.DueTime = 5000;
opt.ActionJob = () =>
{
#if DEBUG
Log.Information($"Service:【 BgService - ScheduleJob - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
};
});
3 后台任务 - 循环任务执行:重复执行的任务,使用常见的时间循环模式
1
2
3
4
5
6
7
8
9
10
services.AddOdinBgServiceScheduleJob(opt =>
{
opt.DueTime = 5000;
opt.ActionJob = () =>
{
#if DEBUG
Log.Information($"Service:【 BgService - ScheduleJob - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
};
});
4 后台任务 - 循环任务执行:重复执行的任务(任务执行完后继续自动执行)
1
2
3
4
5
6
7
8
9
10
services.AddOdinBgServiceLoopJob(opt =>
{
opt.ActionJob = () =>
{
#if DEBUG
Log.Information($"Service:【 BgService - LoopJob - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
Thread.Sleep(1000);
};
});
5 后台任务 - 自定义任务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
services.AddOdinBgServiceJob(opt =>
{
Timer timer = null;
void worker(object state)
{
#if DEBUG
Log.Information($"Service:【 BgService - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
}
opt.StartAsyncAction = () =>
{
timer = new Timer(worker, null, 0, 2000);
};
opt.ExecuteAsyncAction = () =>
{

};
opt.StopAsyncAction = () =>
{
timer?.Change(Timeout.Infinite, 0);
};
opt.DisposeAction = () =>
{
timer?.Dispose();
};
});
6 后台任务 - 多任务执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
services
.AddOdinBgServiceJob(opt =>
{
Timer timer = null;
void worker(object state)
{
#if DEBUG
Log.Information($"Service:【 BgService - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
}
opt.StartAsyncAction = () =>
{
timer = new Timer(worker, null, 0, 2000);
};
opt.ExecuteAsyncAction = () =>
{

};
opt.StopAsyncAction = () =>
{
timer?.Change(Timeout.Infinite, 0);
};
opt.DisposeAction = () =>
{
timer?.Dispose();
};
})
.AddOdinBgServiceLoopJob(opt =>
{
opt.ActionJob = () =>
{
// new ReceiveRabbitMQHelper().ReceiveMQ(_Options);
#if DEBUG
Log.Information($"Service:【 BgService - LoopJob - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
Thread.Sleep(1000);
};
})
.AddOdinBgServiceRecurringJob(opt =>
{
opt.Period = TimeSpan.FromSeconds(1);
opt.ActionJob = () =>
{
// new ReceiveRabbitMQHelper().ReceiveMQ(_Options);
#if DEBUG
Log.Information($"Service:【 BgService - RecurringJob - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
};
})
.AddOdinBgServiceNomalJob(opt =>
{
opt.ActionJob = () =>
{
#if DEBUG
Log.Information($"Service:【 BgService - Nomal- Job - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
};
})
.AddOdinBgServiceScheduleJob(opt =>
{
opt.DueTime = 5000;
opt.ActionJob = () =>
{
#if DEBUG
Log.Information($"Service:【 BgService - ScheduleJob - Running 】\tTime:【 {DateTime.Now.ToString("yyyy-dd-MM hh:mm:ss")} 】");
#endif
};
});

具体的代码在 GitHub nuget