-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathDemoAppTickerQModule.cs
More file actions
143 lines (126 loc) · 5.68 KB
/
Copy pathDemoAppTickerQModule.cs
File metadata and controls
143 lines (126 loc) · 5.68 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using TickerQ.Dashboard.DependencyInjection;
using TickerQ.DependencyInjection;
using TickerQ.Utilities;
using TickerQ.Utilities.Enums;
using TickerQ.Utilities.Interfaces.Managers;
using TickerQ.Utilities.Base;
using TickerQ.Utilities.Entities;
using Volo.Abp.AspNetCore;
using Volo.Abp.Autofac;
using Volo.Abp.BackgroundJobs.DemoApp.Shared;
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs;
using Volo.Abp.BackgroundJobs.TickerQ;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.BackgroundWorkers.TickerQ;
using Volo.Abp.Modularity;
using Volo.Abp.TickerQ;
namespace Volo.Abp.BackgroundJobs.DemoApp.TickerQ;
[DependsOn(
typeof(AbpBackgroundJobsTickerQModule),
typeof(AbpBackgroundWorkersTickerQModule),
typeof(DemoAppSharedModule),
typeof(AbpAutofacModule),
typeof(AbpAspNetCoreModule)
)]
public class DemoAppTickerQModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddTickerQ(options =>
{
options.ConfigureScheduler(scheduler =>
{
scheduler.FallbackIntervalChecker = TimeSpan.FromSeconds(30);
});
options.AddDashboard(x =>
{
x.SetBasePath("/tickerq-dashboard");
});
});
Configure<AbpBackgroundJobsTickerQOptions>(options =>
{
options.AddConfiguration<WriteToConsoleGreenJob>(new AbpBackgroundJobsTimeTickerConfiguration()
{
Retries = 3,
RetryIntervals = new[] {30, 60, 120}, // Retry after 30s, 60s, then 2min,
Priority = TickerTaskPriority.High
});
options.AddConfiguration<WriteToConsoleYellowJob>(new AbpBackgroundJobsTimeTickerConfiguration()
{
Retries = 5,
RetryIntervals = new[] {30, 60, 120}, // Retry after 30s, 60s, then 2min
});
});
Configure<AbpBackgroundWorkersTickerQOptions>(options =>
{
options.AddConfiguration<MyBackgroundWorker>(new AbpBackgroundWorkersCronTickerConfiguration()
{
Retries = 3,
RetryIntervals = new[] {30, 60, 120}, // Retry after 30s, 60s, then 2min,
Priority = TickerTaskPriority.High
});
});
}
public override Task OnPreApplicationInitializationAsync(ApplicationInitializationContext context)
{
var abpTickerQFunctionProvider = context.ServiceProvider.GetRequiredService<AbpTickerQFunctionProvider>();
abpTickerQFunctionProvider.AddFunction(nameof(CleanupJobs), async (cancellationToken, serviceProvider, tickerFunctionContext) =>
{
var service = new CleanupJobs();
var request = await TickerRequestProvider.GetRequestAsync<string>(tickerFunctionContext, cancellationToken);
var genericContext = new TickerFunctionContext<string>(tickerFunctionContext, request);
await service.CleanupLogsAsync(genericContext, cancellationToken);
}, TickerTaskPriority.Normal);
abpTickerQFunctionProvider.RequestTypes.TryAdd(nameof(CleanupJobs), (typeof(string).FullName, typeof(string)));
return Task.CompletedTask;
}
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
var backgroundWorkerManager = context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>();
await backgroundWorkerManager.AddAsync(context.ServiceProvider.GetRequiredService<MyBackgroundWorker>());
var app = context.GetApplicationBuilder();
context.GetHost().UseAbpTickerQ();
var timeTickerManager = context.ServiceProvider.GetRequiredService<ITimeTickerManager<TimeTickerEntity>>();
await timeTickerManager.AddAsync(new TimeTickerEntity
{
Function = nameof(CleanupJobs),
ExecutionTime = DateTime.UtcNow.AddSeconds(5),
Request = TickerHelper.CreateTickerRequest<string>("cleanup_example_file.txt"),
Retries = 3,
RetryIntervals = new[] { 30, 60, 120 }, // Retry after 30s, 60s, then 2min
});
var cronTickerManager = context.ServiceProvider.GetRequiredService<ICronTickerManager<CronTickerEntity>>();
await cronTickerManager.AddAsync(new CronTickerEntity
{
Function = nameof(CleanupJobs),
Expression = "* * * * *", // Every minute
Request = TickerHelper.CreateTickerRequest<string>("cleanup_example_file.txt"),
Retries = 2,
RetryIntervals = new[] { 60, 300 }
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async httpContext =>
{
httpContext.Response.Redirect("/tickerq-dashboard", true);
});
});
await CancelableBackgroundJobAsync(context.ServiceProvider);
}
private async Task CancelableBackgroundJobAsync(IServiceProvider serviceProvider)
{
var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>();
var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-cancel-job" });
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-3" });
await Task.Delay(1000);
var timeTickerManager = serviceProvider.GetRequiredService<ITimeTickerManager<TimeTickerEntity>>();
var result = await timeTickerManager.DeleteAsync(Guid.Parse(jobId));
}
}