24 using System.Collections.Generic;
25 using System.Globalization;
30 namespace Ntp.Common.Process
43 StartTime = DateTime.Now;
47 runningThreads =
new List<Thread>();
48 WaitHandle =
new EventWaitHandle(
false, EventResetMode.AutoReset);
50 schedule =
new List<ScheduledJob>();
51 jobs =
new List<Job>();
56 logGroup.
Add(ActivityLog);
61 private readonly List<Job>
jobs;
70 public EventWaitHandle WaitHandle {
get; }
76 public DateTime StartTime {
get; }
90 return jobs.GetEnumerator();
97 IEnumerator IEnumerable.GetEnumerator()
99 return jobs.GetEnumerator();
106 public bool Active {
get;
private set; }
118 public IEnumerable<ScheduledJob> Schedule => schedule;
140 var job =
new Job(description, scheduleDescription, Log);
143 Log.SchedulerJobAdded(description, job);
145 QueueJob(job, StartTime);
153 Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
162 Log.SchedulerStart(schedule.Count);
170 next = schedule.OrderBy(j => j.Run).ThenBy(j => j.Job.Description.Priority).First();
171 schedule.Remove(next);
177 int wait = Convert.ToInt32(next.
Run.Subtract(DateTime.Now).TotalMilliseconds);
181 Log.SchedulerBehind();
186 bool signal = WaitHandle.WaitOne(wait);
197 schedule.Count(j => j.Job.Description.ThreadType ==
ThreadType.SingleThreaded && j.Job.Running) != 0)
199 PostponeJob(next.
Job);
204 Log.SchedulerJobExecuting(next);
208 ThreadPool.QueueUserWorkItem(JobThreadStart, next.
Job);
213 ExecuteJob(next.
Job);
220 QueueJob(next.
Job, DateTime.Now);
228 lock (runningThreads)
230 foreach (var finished
in runningThreads.Where(t => !t.IsAlive).ToList())
231 runningThreads.Remove(finished);
233 count = runningThreads.Count;
237 Log.SchedulerWaiting(count);
239 lock (runningThreads)
241 foreach (var thread
in runningThreads.ToList())
244 thread.Join(15000/count);
248 Log.SchedulerAbort(thread);
253 Log.SchedulerFinished();
272 lock (runningThreads)
273 runningThreads.Add(Thread.CurrentThread);
275 var job = (
Job) stateInfo;
276 Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
282 Log.SchedulerError(Thread.CurrentThread.Name, e);
286 lock (runningThreads)
288 if (runningThreads.Contains(Thread.CurrentThread))
290 runningThreads.Remove(Thread.CurrentThread);
305 schedule.Add(scheduledJob);
308 Log.SchedulerJobStatus(scheduledJob);
327 while (schedule.Count(j => Math.Abs(next.Subtract(j.Run).TotalMilliseconds) < 5000) != 0)
330 next = next.AddMinutes(move);
339 schedule.Add(scheduledJob);
342 Log.SchedulerJobStatus(scheduledJob);
346 #region IDisposable Support 357 WaitHandle.Dispose();
360 disposedValue =
true;
371 GC.SuppressFinalize(
this);
bool Queued
Gets or sets a value indicating whether this Job is queued for run.
readonly List< ScheduledJob > schedule
IJobConfiguration Configuration
Gets the configuration for the job.
bool Postponed
Gets or sets a value indicating whether this Job has been postponed.
A scheduler performs scheduling of jobs according to job schedule descriptions.
ScheduledJob CreatePostponed(Job job)
Creates a postponed job schedule.
void Add(JobDescription description)
Add the specified job to the scheduler queue.
DateTime Run
Gets the time of planned execution.
ScheduledJob CreateNew(Job job, DateTime start, double offset)
Creates a new scheduled job based on description and parameters.
static ActivityLog CreateActivityLog()
void QueueJob(Job job, DateTime run)
Queue job for scheduled run.
A description of job schedule rules.
Job Job
Gets the job to execute.
static LogGroup CreateGroupLog()
readonly List< Job > jobs
abstract ThreadType ThreadType
Gets a value indicating whether this JobDescription should run as a single thread.
DateTime CalculateNextRun(DateTime start)
Calculates the time of next run according to description parameters.
void JobThreadStart(object stateInfo)
JobScheduleDescription Schedule
Gets the schedule.
void Execute()
Execute this Job.
Scheduler(LogBase log)
Initializes a new instance of the Scheduler class.
A job which have been scheduled for execution.
void RunOneCycle()
Run the scheduler "queue pump" method.
bool CanMove
Gets a value indicating whether this description allow schedules to be moved.
int Frequency
Gets the schedule frequency in minutes.
Base class for jobs following the GoF Command Pattern.
string Name
Gets the name.
void Dispose(bool disposing)
IEnumerator< Job > GetEnumerator()
Gets the enumerator.
void PostponeJob(Job job)
Postpones the job when putting it to the queue.
readonly List< Thread > runningThreads
JobDescription Description
Gets the description.