NTP Analyzer  0.8.2
Analyze the operation of time servers
Job.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using Ntp.Common.Log;
24 
25 namespace Ntp.Common.Process
26 {
27  public sealed class Job
28  {
35  public Job(JobDescription description, JobScheduleDescription schedule, LogBase log)
36  {
37  lock (NextJobLocker)
38  {
39  JobId = nextJobId++;
40  }
41 
42  Description = description;
43  Schedule = schedule;
44  this.log = log;
45 
46  RunCount = 0;
47  }
48 
49  private static readonly object NextJobLocker = new object();
50  private static int nextJobId = 1;
51 
52  private readonly LogBase log;
53  private TimeSpan time = new TimeSpan(0);
54 
59  public int JobId { get; }
60 
65  public JobScheduleDescription Schedule { get; }
66 
71  public JobDescription Description { get; }
72 
77  public bool Queued { get; internal set; }
78 
83  public bool Postponed { get; internal set; }
84 
89  public bool Running { get; private set; }
90 
95  public DateTime Started { get; private set; }
96 
101  public int RunCount { get; private set; }
102 
107  public string State
108  {
109  get
110  {
111  if (Running)
112  return "running";
113  if (Postponed)
114  return "postponed";
115 
116  return Queued ? "queued" : "stopped";
117  }
118  }
119 
124  public string Runtime => Running ? DateTime.Now.Subtract(Started).ToString() : string.Empty;
125 
130  public string TotalRuntime => Running ? (DateTime.Now.Subtract(Started) + time).ToString() : time.ToString();
131 
135  public void Execute()
136  {
137  Started = DateTime.Now;
138 
139  // Narrow scope to reduce thread interference
140  var threadStart = DateTime.Now;
141 
142  Queued = false;
143  Postponed = false;
144  Running = true;
145  RunCount++;
146 
147  bool error = false;
148 
149  try
150  {
151  Description.Execute();
152  }
153  catch (Exception e)
154  {
155  error = true;
156  log.JobError(this, e);
157  }
158  finally
159  {
160  Running = false;
161  }
162 
163  time += DateTime.Now.Subtract(threadStart);
164  log.JobExecutionStatus(this, error);
165  }
166 
167  public static void Reset()
168  {
169  lock (NextJobLocker)
170  {
171  nextJobId = 1;
172  }
173  }
174 
179  public override string ToString()
180  {
181  var name = string.IsNullOrWhiteSpace(Description.Name) ? string.Empty : " " + Description.Name;
182  return $"{Description.JobType} job{name}";
183  }
184  }
185 }
override string ToString()
Returns a string that represents the current JobDescription.
override string ToString()
Returns a string that represents the current Job.
Definition: Job.cs:179
readonly LogBase log
Definition: Job.cs:52
A description of job schedule rules.
static void Reset()
Definition: Job.cs:167
var e
Definition: bootstrap.min.js:6
Job(JobDescription description, JobScheduleDescription schedule, LogBase log)
Initializes a new instance of the Job class.
Definition: Job.cs:35
void Execute()
Execute this Job.
Definition: Job.cs:135
Base class for jobs following the GoF Command Pattern.