private JobManager(JobManagerSettings settings) { Settings = settings; JobService = new JobDataService(Settings); //Get all jobs with status running and set them to status abort. var runningJobs = JobService.GetRunningJobs(); foreach (var job in runningJobs) { job.Status = JobStatus.Aborted; job.AbortedBy = Guid.Empty; //by system job.FinishedOn = DateTime.UtcNow; JobService.UpdateJob(job); } }
public void Process(JobContext context) { JobDataService jobService = new JobDataService(JobManager.Settings); Job job = new Job(); job.Id = context.JobId; job.StartedOn = DateTime.UtcNow; job.Status = JobStatus.Running; try { jobService.UpdateJob(job); lock (lockObj) { Pool.Add(context); } var instance = (ErpJob)Activator.CreateInstance(context.Type.ErpJobType); try { DbContext.CreateContext(ErpSettings.ConnectionString); using (var secCtx = SecurityContext.OpenSystemScope()) { //execute job method instance.Execute(context); } } catch (TargetInvocationException ex) { throw ex.InnerException; } catch (Exception) { throw; } finally { DbContext.CloseContext(); } if (context.Result != null) { job.Result = context.Result; } job.FinishedOn = DateTime.UtcNow; job.Status = JobStatus.Finished; jobService.UpdateJob(job); } catch (Exception ex) { try { DbContext.CreateContext(ErpSettings.ConnectionString); using (var secCtx = SecurityContext.OpenSystemScope()) { Log log = new Log(); log.Create(LogType.Error, $"JobPool.Process.{context.Type.Name}", ex); job.FinishedOn = DateTime.UtcNow; job.Status = JobStatus.Failed; job.ErrorMessage = ex.Message; jobService.UpdateJob(job); } } finally { DbContext.CloseContext(); } } finally { lock (lockObj) { Pool.Remove(context); } } }