-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathJobProcessorCore.cs
More file actions
119 lines (94 loc) · 3.32 KB
/
JobProcessorCore.cs
File metadata and controls
119 lines (94 loc) · 3.32 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
using System.Threading;
using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
namespace JobProcessor
{
class JobProcessorCore
{
private bool running = false;
private Task task = null;
public void Start()
{
running = true;
task = Task.Run(() => Loop());
}
public void Stop()
{
running = false;
if (task != null)
task.Wait();
}
private void Loop()
{
#if DEBUG
Thread.Sleep(TimeSpan.FromMinutes(1));
#endif
int count = 0;
while(running)
{
try
{
var logic = new JobManagement.JobsCore(ConfigurationManager.AppSettings["database"]);
var jobs = logic.GetJobs();
// Process up to 100 items in one cycle
jobs = jobs.GetRange(0, Math.Min(jobs.Count, 100));
foreach (var job in jobs)
{
string type;
IDictionary<string, string> parameters;
logic.GetJobDetails(job, out type, out parameters);
ProcessJob(type, parameters);
count++;
// Mark this job as complete asynchronously
logic.FinishJob(job);
if (!running)
break;
}
if (jobs.Count == 0)
Thread.Sleep(500);
// Delete old jobs periodically
if (count > 100)
{
count = 0;
// Delete olbs jobs asynchronously
logic.DeleteOldJobs();
}
}
catch(Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("JobProcessor", ex.ToString());
}
}
}
private void ProcessJob(string type, IDictionary<string, string> parameters)
{
try
{
int duration = 22;
if (parameters.ContainsKey("duration"))
{
int.TryParse(parameters["duration"], out duration);
}
duration += new Random().Next(duration / 8);
if (type.Equals("ondemandreport", StringComparison.InvariantCultureIgnoreCase))
{
duration += int.Parse(parameters["offset"]);
}
var logic = new JobManagement.JobsCore(ConfigurationManager.AppSettings["database"]);
logic.ScheduleReport(type, duration);
}
catch(Exception ex)
{
LogException(ex, "Report type = " + type);
}
}
private static void LogException(Exception ex, string message)
{
// Typically here would be a real exception handling code, inputting a simple placeholder to log to DB
var logic = new JobManagement.JobsCore(ConfigurationManager.AppSettings["database"]);
logic.LogError(ex.ToString(), message);
}
}
}