7
7
builder . Services . AddControllers ( ) ;
8
8
builder . Services . AddOpenApi ( ) ;
9
9
10
- builder . Services . AddTaskProcessor ( new TaskProcessorOptions
10
+ builder . Services . AddTaskProcessor ( ( sp , options ) =>
11
11
{
12
- Redis = "localhost:6379,abortConnect=false" ,
13
- Prefix = "{core}" ,
14
- Queues = new [ ] { "high" , "default" } ,
15
- MaxWorkers = 4 ,
16
- Retries = 3 ,
17
- BaseFrequency = TimeSpan . FromSeconds ( 1 ) ,
18
- Invisibility = TimeSpan . FromMinutes ( 5 ) ,
19
- Retention = TimeSpan . FromDays ( 7 ) ,
20
- UseHostedService = true ,
21
- UseCronSeconds = true
12
+ options . Redis = "localhost:6379,abortConnect=false" ;
13
+ options . Prefix = "{core}" ;
14
+ options . Queues = [ "high" , "default" ] ;
15
+ options . MaxWorkers = 4 ;
16
+ options . Retries = 3 ;
17
+ options . Invisibility = TimeSpan . FromMinutes ( 5 ) ;
18
+ options . BaseFrequency = TimeSpan . FromSeconds ( 5 ) ;
19
+ options . PushbackFrequency = TimeSpan . FromSeconds ( 10 ) ;
20
+ options . CleanUpFrequency = TimeSpan . FromMinutes ( 5 ) ;
21
+ options . Retention = TimeSpan . FromDays ( 7 ) ;
22
+ options . Deadletter = true ;
23
+ options . DeadletterUniqueSchedules = false ;
24
+ options . UseCronSeconds = true ;
25
+ options . OnTaskFailedDelay = ( _ , retry ) =>
26
+ Task . FromResult ( retry switch
27
+ {
28
+ 2 => TimeSpan . FromSeconds ( 5 ) ,
29
+ 1 => TimeSpan . FromSeconds ( 60 ) ,
30
+ _ => ( TimeSpan ? ) null
31
+ } ) ;
32
+ options . OnTaskError = ( _ , exception ) =>
33
+ {
34
+ sp . GetRequiredService < ILogger < ITaskProcessor > > ( )
35
+ . LogError ( exception , "Task Error" ) ;
36
+
37
+ return Task . CompletedTask ;
38
+ } ;
22
39
} ) ;
40
+ builder . Services . AddTaskProcessorExecutor ( ) ;
23
41
24
42
builder . Services . AddScoped < ISomeScopedService , SomeScopedService > ( ) ;
43
+ builder . Services . AddScoped < FaultyService > ( ) ;
25
44
26
45
var app = builder . Build ( ) ;
27
46
37
56
38
57
{
39
58
var proc = app . Services . GetRequiredService < ITaskProcessor > ( ) ;
59
+ await using var scope = app . Services . CreateAsyncScope ( ) ;
60
+ var faulty = scope . ServiceProvider . GetRequiredService < FaultyService > ( ) ;
61
+
62
+ //foreach (var schedule in await proc.GetSchedulesAsync("core", 0, 100))
63
+ // await proc.CancelScheduleAsync(schedule.Id, "core");
64
+
65
+ await proc . ResumeAsync ( ) ;
66
+
67
+ //var discarded = await proc.DiscardDeadTasksAsync("default");
40
68
41
- foreach ( var schedule in await proc . GetSchedulesAsync ( "core" , 0 , 100 ) )
42
- await proc . CancelScheduleAsync ( schedule . Id , "core" ) ;
69
+ await proc . UpsertScheduleAsync ( new ScheduleData
70
+ {
71
+ Id = "refresh" ,
72
+ Tenant = "core" ,
73
+ Queue = "default" ,
74
+ Cron = "0 */2 * * * *" ,
75
+ Timezone = "Etc/UTC" ,
76
+ Unique = true
77
+ } , ( ) => faulty . DoFaultyStuff ( ) ) ;
43
78
}
44
79
45
80
app . Run ( ) ;
0 commit comments