NTP Analyzer  0.8.2
Analyze the operation of time servers
Ntp.Common.Process.Scheduler Class Reference

A scheduler performs scheduling of jobs according to job schedule descriptions. More...

Inheritance diagram for Ntp.Common.Process.Scheduler:
Collaboration diagram for Ntp.Common.Process.Scheduler:

Public Member Functions

 Scheduler (LogBase log)
 Initializes a new instance of the Scheduler class. More...
 
void Add (JobDescription description)
 Add the specified job to the scheduler queue. More...
 
void Dispose ()
 
IEnumerator< JobGetEnumerator ()
 Gets the enumerator. More...
 
void RunOneCycle ()
 Run the scheduler "queue pump" method. More...
 
void Stop ()
 

Public Attributes

IEnumerable< ScheduledJobSchedule => schedule
 Gets the schedule. More...
 

Properties

bool Active [get, private set]
 Gets a value indicating whether this Scheduler is active. More...
 
ActivityLog ActivityLog [get]
 Gets the activity log. More...
 
LogBase Log [get]
 Gets the log used by this Scheduler. More...
 
ScheduledJob NextJob [get, private set]
 Gets the next job to be executed. More...
 
DateTime StartTime [get]
 Gets the start up time of this Scheduler. More...
 
EventWaitHandle WaitHandle [get]
 Gets the wait handle of this Scheduler. More...
 
- Properties inherited from Ntp.Common.Process.IScheduler
bool Active [get]
 
ActivityLog ActivityLog [get]
 
ScheduledJob NextJob [get]
 
IEnumerable< ScheduledJobSchedule [get]
 

Private Member Functions

 ~Scheduler ()
 
void Dispose (bool disposing)
 
void ExecuteJob (Job job)
 
IEnumerator IEnumerable. GetEnumerator ()
 Gets the enumerator. More...
 
void JobThreadStart (object stateInfo)
 
void PostponeJob (Job job)
 Postpones the job when putting it to the queue. More...
 
void QueueJob (Job job, DateTime run)
 Queue job for scheduled run. More...
 

Private Attributes

bool disposedValue
 
bool firstRun
 
readonly List< Jobjobs
 
readonly List< Thread > runningThreads
 
readonly List< ScheduledJobschedule
 

Detailed Description

A scheduler performs scheduling of jobs according to job schedule descriptions.

Definition at line 35 of file Scheduler.cs.

Constructor & Destructor Documentation

Ntp.Common.Process.Scheduler.Scheduler ( LogBase  log)
inline

Initializes a new instance of the Scheduler class.

Parameters
logLog.

Definition at line 41 of file Scheduler.cs.

References Ntp.Common.Log.LogGroup.Add(), Ntp.Common.Log.LogFactory.CreateActivityLog(), and Ntp.Common.Log.LogFactory.CreateGroupLog().

42  {
43  StartTime = DateTime.Now;
44  firstRun = true;
45  Active = false;
46 
47  runningThreads = new List<Thread>();
48  WaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
49 
50  schedule = new List<ScheduledJob>();
51  jobs = new List<Job>();
53 
54  LogGroup logGroup = LogFactory.CreateGroupLog();
55  logGroup.Add(log);
56  logGroup.Add(ActivityLog);
57 
58  Log = logGroup;
59  }
void Add(LogBase log)
Definition: LogGroup.cs:71
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
readonly List< ScheduledJob > schedule
Definition: Scheduler.cs:63
static ActivityLog CreateActivityLog()
Definition: LogFactory.cs:45
static LogGroup CreateGroupLog()
Definition: LogFactory.cs:63
readonly List< Job > jobs
Definition: Scheduler.cs:61
EventWaitHandle WaitHandle
Gets the wait handle of this Scheduler.
Definition: Scheduler.cs:70
bool Active
Gets a value indicating whether this Scheduler is active.
Definition: Scheduler.cs:106
DateTime StartTime
Gets the start up time of this Scheduler.
Definition: Scheduler.cs:76
readonly List< Thread > runningThreads
Definition: Scheduler.cs:62

Here is the call graph for this function:

Ntp.Common.Process.Scheduler.~Scheduler ( )
inlineprivate

Definition at line 363 of file Scheduler.cs.

364  {
365  Dispose(false);
366  }

Member Function Documentation

void Ntp.Common.Process.Scheduler.Add ( JobDescription  description)
inline

Add the specified job to the scheduler queue.

Parameters
descriptionDescription.

Definition at line 130 of file Scheduler.cs.

References Ntp.Common.Process.JobDescription.Configuration, Ntp.Common.Process.IJobConfiguration.FixedRun, Ntp.Common.Process.IJobConfiguration.Frequency, and Ntp.Common.Process.IJobConfiguration.InitialRun.

Referenced by Ntp.Analyzer.Process.Initializer.InitializeScheduler().

131  {
132  if (description.Configuration == null || description.Configuration.Frequency == -1)
133  return;
134 
135  var scheduleDescription = new JobScheduleDescription(
136  description.Configuration.InitialRun,
137  description.Configuration.FixedRun,
138  description.Configuration.Frequency);
139 
140  var job = new Job(description, scheduleDescription, Log);
141  jobs.Add(job);
142 
143  Log.SchedulerJobAdded(description, job);
144 
145  QueueJob(job, StartTime);
146  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
void QueueJob(Job job, DateTime run)
Queue job for scheduled run.
Definition: Scheduler.cs:317
readonly List< Job > jobs
Definition: Scheduler.cs:61
DateTime StartTime
Gets the start up time of this Scheduler.
Definition: Scheduler.cs:76

Here is the caller graph for this function:

void Ntp.Common.Process.Scheduler.Dispose ( bool  disposing)
inlineprivate

Definition at line 350 of file Scheduler.cs.

351  {
352  if (disposedValue)
353  return;
354 
355  if (disposing)
356  {
357  WaitHandle.Dispose();
358  }
359 
360  disposedValue = true;
361  }
EventWaitHandle WaitHandle
Gets the wait handle of this Scheduler.
Definition: Scheduler.cs:70
void Ntp.Common.Process.Scheduler.Dispose ( )
inline

Definition at line 368 of file Scheduler.cs.

369  {
370  Dispose(true);
371  GC.SuppressFinalize(this);
372  }
void Ntp.Common.Process.Scheduler.ExecuteJob ( Job  job)
inlineprivate

Definition at line 256 of file Scheduler.cs.

References Ntp.Common.Process.Job.Description, e, Ntp.Common.Process.Job.Execute(), and Ntp.Common.Process.JobDescription.Name.

257  {
258  try
259  {
260  job.Execute();
261  }
262  catch (Exception e)
263  {
264  Log.SchedulerError(job.Description.Name, e);
265  }
266  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
var e
Definition: bootstrap.min.js:6

Here is the call graph for this function:

IEnumerator<Job> Ntp.Common.Process.Scheduler.GetEnumerator ( )
inline

Gets the enumerator.

Returns
The enumerator.

Definition at line 88 of file Scheduler.cs.

89  {
90  return jobs.GetEnumerator();
91  }
readonly List< Job > jobs
Definition: Scheduler.cs:61
IEnumerator IEnumerable. Ntp.Common.Process.Scheduler.GetEnumerator ( )
inlineprivate

Gets the enumerator.

Returns
The enumerator.

Definition at line 97 of file Scheduler.cs.

98  {
99  return jobs.GetEnumerator();
100  }
readonly List< Job > jobs
Definition: Scheduler.cs:61
void Ntp.Common.Process.Scheduler.JobThreadStart ( object  stateInfo)
inlineprivate

Definition at line 268 of file Scheduler.cs.

References e.

269  {
270  try
271  {
272  lock (runningThreads)
273  runningThreads.Add(Thread.CurrentThread);
274 
275  var job = (Job) stateInfo;
276  Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
277  //Thread.CurrentThread.Name = job.Description.Name;
278  job.Execute();
279  }
280  catch (Exception e)
281  {
282  Log.SchedulerError(Thread.CurrentThread.Name, e);
283  }
284  finally
285  {
286  lock (runningThreads)
287  {
288  if (runningThreads.Contains(Thread.CurrentThread))
289  {
290  runningThreads.Remove(Thread.CurrentThread);
291  }
292  }
293  }
294  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
void Add(JobDescription description)
Add the specified job to the scheduler queue.
Definition: Scheduler.cs:130
var e
Definition: bootstrap.min.js:6
readonly List< Thread > runningThreads
Definition: Scheduler.cs:62
void Ntp.Common.Process.Scheduler.PostponeJob ( Job  job)
inlineprivate

Postpones the job when putting it to the queue.

Parameters
jobJob.

Definition at line 300 of file Scheduler.cs.

References Ntp.Common.Process.JobScheduleDescription.CreatePostponed(), Ntp.Common.Process.Job.Postponed, Ntp.Common.Process.Job.Queued, and Ntp.Common.Process.Job.Schedule.

301  {
302  lock (schedule)
303  {
304  ScheduledJob scheduledJob = job.Schedule.CreatePostponed(job);
305  schedule.Add(scheduledJob);
306  job.Queued = true;
307  job.Postponed = true;
308  Log.SchedulerJobStatus(scheduledJob);
309  }
310  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
readonly List< ScheduledJob > schedule
Definition: Scheduler.cs:63

Here is the call graph for this function:

void Ntp.Common.Process.Scheduler.QueueJob ( Job  job,
DateTime  run 
)
inlineprivate

Queue job for scheduled run.

Parameters
jobJob.
runRun.

Definition at line 317 of file Scheduler.cs.

References Ntp.Common.Process.JobScheduleDescription.CalculateNextRun(), Ntp.Common.Process.JobScheduleDescription.CanMove, Ntp.Common.Process.JobScheduleDescription.CreateNew(), Ntp.Common.Process.JobScheduleDescription.Frequency, Ntp.Common.Process.Job.Postponed, Ntp.Common.Process.Job.Queued, and Ntp.Common.Process.Job.Schedule.

318  {
319  DateTime next = job.Schedule.CalculateNextRun(run);
320  double offset = 0;
321 
322  // Adjust offset to avoid simultaneously job execution.
323  if (job.Schedule.CanMove)
324  {
325  lock (schedule)
326  {
327  while (schedule.Count(j => Math.Abs(next.Subtract(j.Run).TotalMilliseconds) < 5000) != 0)
328  {
329  double move = job.Schedule.Frequency/40.0;
330  next = next.AddMinutes(move);
331  offset += move;
332  }
333  }
334  }
335 
336  lock (schedule)
337  {
338  ScheduledJob scheduledJob = job.Schedule.CreateNew(job, run, offset);
339  schedule.Add(scheduledJob);
340  job.Queued = true;
341  job.Postponed = false;
342  Log.SchedulerJobStatus(scheduledJob);
343  }
344  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
readonly List< ScheduledJob > schedule
Definition: Scheduler.cs:63

Here is the call graph for this function:

void Ntp.Common.Process.Scheduler.RunOneCycle ( )
inline

Run the scheduler "queue pump" method.

Definition at line 151 of file Scheduler.cs.

References Ntp.Common.Process.Job.Description, Ntp.Common.Process.JobScheduleDescription.Frequency, Ntp.Common.Process.ScheduledJob.Job, Ntp.Common.Process.Job.Queued, Ntp.Common.Process.ScheduledJob.Run, Ntp.Common.Process.Job.Schedule, and Ntp.Common.Process.JobDescription.ThreadType.

152  {
153  Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
154 
155  if (firstRun)
156  {
157  firstRun = false;
158  Active = true;
159 
160  lock (schedule)
161  {
162  Log.SchedulerStart(schedule.Count);
163  }
164  }
165 
166  // Take the next job of the queue
167  ScheduledJob next;
168  lock (schedule)
169  {
170  next = schedule.OrderBy(j => j.Run).ThenBy(j => j.Job.Description.Priority).First();
171  schedule.Remove(next);
172  next.Job.Queued = false;
173  NextJob = next;
174  }
175 
176  // Wait until job should be run
177  int wait = Convert.ToInt32(next.Run.Subtract(DateTime.Now).TotalMilliseconds);
178  if (wait < 0)
179  {
180  if (wait < -5000)
181  Log.SchedulerBehind();
182 
183  wait = 0;
184  }
185 
186  bool signal = WaitHandle.WaitOne(wait);
187  if (signal)
188  {
189  // Abort
190  return;
191  }
192 
193  // Check if the job is already running and postpone if needed
194  lock (schedule)
195  {
196  if (next.Job.Description.ThreadType == ThreadType.SingleThreaded &&
197  schedule.Count(j => j.Job.Description.ThreadType == ThreadType.SingleThreaded && j.Job.Running) != 0)
198  {
199  PostponeJob(next.Job);
200  return;
201  }
202  }
203 
204  Log.SchedulerJobExecuting(next);
205  if (next.Job.Description.ThreadType != ThreadType.NoThread)
206  {
207  // Queue the job on the threadpool
208  ThreadPool.QueueUserWorkItem(JobThreadStart, next.Job);
209  }
210  else
211  {
212  // Execute directly in scheduler thread
213  ExecuteJob(next.Job);
214  }
215 
216  // Dont re-schedule "run-only-once" jobs.
217  if (next.Job.Schedule.Frequency == 0)
218  return;
219 
220  QueueJob(next.Job, DateTime.Now);
221  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
readonly List< ScheduledJob > schedule
Definition: Scheduler.cs:63
void QueueJob(Job job, DateTime run)
Queue job for scheduled run.
Definition: Scheduler.cs:317
void JobThreadStart(object stateInfo)
Definition: Scheduler.cs:268
EventWaitHandle WaitHandle
Gets the wait handle of this Scheduler.
Definition: Scheduler.cs:70
bool Active
Gets a value indicating whether this Scheduler is active.
Definition: Scheduler.cs:106
ScheduledJob NextJob
Gets the next job to be executed.
Definition: Scheduler.cs:124
void PostponeJob(Job job)
Postpones the job when putting it to the queue.
Definition: Scheduler.cs:300
void Ntp.Common.Process.Scheduler.Stop ( )
inline

Definition at line 223 of file Scheduler.cs.

224  {
225  int count;
226 
227  // Remove finished threads
228  lock (runningThreads)
229  {
230  foreach (var finished in runningThreads.Where(t => !t.IsAlive).ToList())
231  runningThreads.Remove(finished);
232 
233  count = runningThreads.Count;
234  }
235 
236  if (count != 0)
237  Log.SchedulerWaiting(count);
238 
239  lock (runningThreads)
240  {
241  foreach (var thread in runningThreads.ToList())
242  {
243  // Wait maximum 15 seconds
244  thread.Join(15000/count);
245  if (!thread.IsAlive)
246  continue;
247 
248  Log.SchedulerAbort(thread);
249  thread.Abort();
250  }
251  }
252 
253  Log.SchedulerFinished();
254  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
readonly List< Thread > runningThreads
Definition: Scheduler.cs:62

Member Data Documentation

bool Ntp.Common.Process.Scheduler.disposedValue
private

Definition at line 348 of file Scheduler.cs.

bool Ntp.Common.Process.Scheduler.firstRun
private

Definition at line 64 of file Scheduler.cs.

readonly List<Job> Ntp.Common.Process.Scheduler.jobs
private

Definition at line 61 of file Scheduler.cs.

readonly List<Thread> Ntp.Common.Process.Scheduler.runningThreads
private

Definition at line 62 of file Scheduler.cs.

readonly List<ScheduledJob> Ntp.Common.Process.Scheduler.schedule
private

Definition at line 63 of file Scheduler.cs.

IEnumerable<ScheduledJob> Ntp.Common.Process.Scheduler.Schedule => schedule

Gets the schedule.

The schedule.

Definition at line 118 of file Scheduler.cs.

Property Documentation

bool Ntp.Common.Process.Scheduler.Active
getprivate set

Gets a value indicating whether this Scheduler is active.

true if active; otherwise, false.

Definition at line 106 of file Scheduler.cs.

ActivityLog Ntp.Common.Process.Scheduler.ActivityLog
get

Gets the activity log.

The activity log.

Definition at line 112 of file Scheduler.cs.

LogBase Ntp.Common.Process.Scheduler.Log
get

Gets the log used by this Scheduler.

The log.

Definition at line 82 of file Scheduler.cs.

Referenced by Ntp.Analyzer.Process.Initializer.InitializeScheduler().

ScheduledJob Ntp.Common.Process.Scheduler.NextJob
getprivate set

Gets the next job to be executed.

The next job.

Definition at line 124 of file Scheduler.cs.

DateTime Ntp.Common.Process.Scheduler.StartTime
get

Gets the start up time of this Scheduler.

The start up time.

Definition at line 76 of file Scheduler.cs.

Referenced by Ntp.Analyzer.Process.Initializer.InitializeScheduler().

EventWaitHandle Ntp.Common.Process.Scheduler.WaitHandle
get

Gets the wait handle of this Scheduler.

The wait handle.

Definition at line 70 of file Scheduler.cs.

Referenced by Ntp.Common.Process.Cluster.Cluster().


The documentation for this class was generated from the following file: