-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulkExampleService.cs
More file actions
102 lines (68 loc) · 2.22 KB
/
Copy pathBulkExampleService.cs
File metadata and controls
102 lines (68 loc) · 2.22 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
using Hangfire;
using HotChocolate.Subscriptions;
namespace HangfireDemo;
public class BulkExampleService(ITopicEventSender eventSender)
{
public void UploadBulk(int count)
{
for (int i = 0; i < count; i++)
{
BackgroundJob.Enqueue(() => Upload(i.ToString()));
}
}
//public void UploadBulk(int count)
//{
// for (int i = 0; i < count; i++)
// {
// //Once every 2 seconds
// var when = TimeSpan.FromSeconds(i * 2);
// BackgroundJob.Schedule(Constants.FastQueue, () => Upload(i.ToString()), when);
// }
//}
//public void UploadBulk(int count)
//{
// string jobId = string.Empty;
// for(int i = 0; i < count; i++)
// {
// // 5 per second
// var when = TimeSpan.FromSeconds(i / 5);
// jobId = BackgroundJob.Schedule(Constants.FastQueue, () => Upload(i.ToString()), when);
// }
// BackgroundJob.ContinueJobWith(jobId, Constants.DefaultQueue, () => Upload("Completed"));
//}
[AutomaticRetry(Attempts = 3, DelaysInSeconds = [15], OnAttemptsExceeded = AttemptsExceededAction.Fail)]
//[DisableConcurrentExecution(30)]
public async Task Upload(string text)
{
await Task.Delay(5000);
await eventSender.SendAsync("jobResult", new JobResult() { Text = text, Random = new Random() });
/*
* Exception Handling
*/
//Just throw
//Will automatically retry
//if (text == "99")
// throw new Exception("We are at 99");
//In a try/catch
//Hangfire does not know anything went wrong.
try
{
if (text == "99")
throw new Exception("We are at 99");
}
catch (Exception ex)
{
Console.WriteLine($"ERROR: {ex.Message}");
}
}
public void UploadWithFilterA(string text)
{
BackgroundJob.Enqueue(() => UploadWithFilterB(text));
}
[ExampleFilter]
public async Task UploadWithFilterB(string text)
{
await Task.Delay(5000);
await eventSender.SendAsync("jobResult", new JobResult() { Text = text, Random = new Random() });
}
}