File tree Expand file tree Collapse file tree 5 files changed +73
-5
lines changed
demos/MainDemo/src/Syrna.QuartzAdmin.MainDemo.Jobs Expand file tree Collapse file tree 5 files changed +73
-5
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ private static async Task CanFireIt()
1717 await Task . CompletedTask ;
1818 }
1919
20- public async Task Execute ( IJobExecutionContext context )
20+ private async Task ExecuteJob ( IJobExecutionContext context )
2121 {
2222 context . CancellationToken . ThrowIfCancellationRequested ( ) ;
2323 Console . WriteLine ( $ "Hello from AutoJob { DateTime . Now } ") ;
@@ -31,5 +31,19 @@ public async Task Execute(IJobExecutionContext context)
3131
3232 await Task . CompletedTask ;
3333 }
34+
35+ public async Task Execute ( IJobExecutionContext context )
36+ {
37+ var taskCompletionSource = new TaskCompletionSource ( ) ;
38+ context . CancellationToken . Register ( ( ) =>
39+ {
40+ // We received a cancellation message, cancel the TaskCompletionSource.Task
41+ // ReSharper disable once InvertIf
42+ taskCompletionSource . TrySetCanceled ( ) ;
43+ } ) ;
44+ var completedTask = await Task . WhenAny ( ExecuteJob ( context ) , taskCompletionSource . Task ) ;
45+
46+ await completedTask ;
47+ }
3448 }
3549}
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ namespace Syrna.QuartzAdmin.MainDemo.Jobs
77 [ QuartzTrigger ( 5 , 0 , 0 , Desciption = "Automatic job of welcome information" ) ]
88 public class AutoJob1 : IJob
99 {
10- public Task Execute ( IJobExecutionContext context )
10+ private Task ExecuteJob ( IJobExecutionContext context )
1111 {
1212 context . CancellationToken . ThrowIfCancellationRequested ( ) ;
1313 Console . WriteLine ( $ "Hello from Auto Job1 { DateTime . Now } ") ;
@@ -19,5 +19,19 @@ public Task Execute(IJobExecutionContext context)
1919 context . Result = $ "Hello from Auto Job1 { DateTime . Now } ";
2020 return Task . CompletedTask ;
2121 }
22+
23+ public async Task Execute ( IJobExecutionContext context )
24+ {
25+ var taskCompletionSource = new TaskCompletionSource ( ) ;
26+ context . CancellationToken . Register ( ( ) =>
27+ {
28+ // We received a cancellation message, cancel the TaskCompletionSource.Task
29+ // ReSharper disable once InvertIf
30+ taskCompletionSource . TrySetCanceled ( ) ;
31+ } ) ;
32+ var completedTask = await Task . WhenAny ( ExecuteJob ( context ) , taskCompletionSource . Task ) ;
33+
34+ await completedTask ;
35+ }
2236 }
2337}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ namespace Syrna.QuartzAdmin.MainDemo.Jobs
88 [ QuartzTrigger ( 2 , 0 , "this is an long job test" , "_longjobauto" ) ]
99 public class AutoJob2 ( ILogger < HelloJob > logger ) : IJob
1010 {
11- public async Task Execute ( IJobExecutionContext context )
11+ private async Task ExecuteJob ( IJobExecutionContext context )
1212 {
1313 context . CancellationToken . ThrowIfCancellationRequested ( ) ;
1414 Console . WriteLine ( $ "Hello from AutoJob { DateTime . Now } ") ;
@@ -36,5 +36,18 @@ public async Task Execute(IJobExecutionContext context)
3636
3737 await Task . CompletedTask ;
3838 }
39+ public async Task Execute ( IJobExecutionContext context )
40+ {
41+ var taskCompletionSource = new TaskCompletionSource ( ) ;
42+ context . CancellationToken . Register ( ( ) =>
43+ {
44+ // We received a cancellation message, cancel the TaskCompletionSource.Task
45+ // ReSharper disable once InvertIf
46+ taskCompletionSource . TrySetCanceled ( ) ;
47+ } ) ;
48+ var completedTask = await Task . WhenAny ( ExecuteJob ( context ) , taskCompletionSource . Task ) ;
49+
50+ await completedTask ;
51+ }
3952 }
4053}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ public class HttpJob(IHttpClientFactory httpClientFactory,
1212 ILogger < HttpJob > logger ,
1313 IDataMapValueResolver dmvResolver ) : IJob
1414 {
15- public async Task Execute ( IJobExecutionContext context )
15+ private async Task ExecuteJob ( IJobExecutionContext context )
1616 {
1717 context . CancellationToken . ThrowIfCancellationRequested ( ) ;
1818 try
@@ -127,6 +127,19 @@ public async Task Execute(IJobExecutionContext context)
127127 throw new JobExecutionException ( "Failed to execute http job" , ex ) ;
128128 }
129129 }
130+ public async Task Execute ( IJobExecutionContext context )
131+ {
132+ var taskCompletionSource = new TaskCompletionSource ( ) ;
133+ context . CancellationToken . Register ( ( ) =>
134+ {
135+ // We received a cancellation message, cancel the TaskCompletionSource.Task
136+ // ReSharper disable once InvertIf
137+ taskCompletionSource . TrySetCanceled ( ) ;
138+ } ) ;
139+ var completedTask = await Task . WhenAny ( ExecuteJob ( context ) , taskCompletionSource . Task ) ;
140+
141+ await completedTask ;
142+ }
130143 }
131144}
132145
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ public class LongJob(
1212 private const string PropertyMessage = "message" ;
1313 private const string PropertyDelayInMs = "delay" ;
1414
15- public async Task Execute ( IJobExecutionContext context )
15+ private async Task ExecuteJob ( IJobExecutionContext context )
1616 {
1717 var taskCompletionSource = new TaskCompletionSource ( ) ;
1818 context . CancellationToken . Register ( ( ) =>
@@ -47,5 +47,19 @@ public async Task Execute(IJobExecutionContext context)
4747
4848 await completedTask ;
4949 }
50+
51+ public async Task Execute ( IJobExecutionContext context )
52+ {
53+ var taskCompletionSource = new TaskCompletionSource ( ) ;
54+ context . CancellationToken . Register ( ( ) =>
55+ {
56+ // We received a cancellation message, cancel the TaskCompletionSource.Task
57+ // ReSharper disable once InvertIf
58+ taskCompletionSource . TrySetCanceled ( ) ;
59+ } ) ;
60+ var completedTask = await Task . WhenAny ( ExecuteJob ( context ) , taskCompletionSource . Task ) ;
61+
62+ await completedTask ;
63+ }
5064}
5165
You can’t perform that action at this time.
0 commit comments