public List <JobInstance> QueueScheduledJobs(bool clearExistingScheduledJobInstancesFromWaitingQueue = true) { if (clearExistingScheduledJobInstancesFromWaitingQueue) { JobQueueManager.DequeueScheduledJobs(); } List <JobInstance> jobInstances = new List <JobInstance>(); var integrations = _configurator.GetIntegrations().Where(d => d.IsEnabled); foreach (var integration in integrations) { var jobs = integration.Jobs.Where(d => d.IsEnabled); foreach (var job in jobs) { if (job.Steps.Where(d => d.IsEnabled).Count() > 0 && job.SourceDataSources.Values.Count > 0) { var nextSoonestRunTime = GetNextScheduledRunTimeForJob(job.Id); if (nextSoonestRunTime.HasValue) { var addedJobInstances = JobQueueManager.QueueJob(integration, job.SourceDataSources.Values, job, nextSoonestRunTime.Value, "Sync Engine Scheduler", JobInvocationSourceType.Scheduled); jobInstances.AddRange(addedJobInstances); } } } } return(jobInstances); }
public List <JobInstance> QueueJobForNextScheduledRunTime(Guid jobId) { var nextSoonestRunTime = GetNextScheduledRunTimeForJob(jobId); var integration = _configurator.GetIntegrationByJobId(jobId); var job = _configurator.GetJobById(jobId); if (nextSoonestRunTime.HasValue) { return(JobQueueManager.QueueJob(integration, job.SourceDataSources.Values, job, nextSoonestRunTime.Value, "Sync Engine Scheduler", JobInvocationSourceType.Scheduled)); } else { return(new List <JobInstance>()); } }
/// <summary> /// Clears the in-memory queue and refreshes from the database as best as possible. /// /// NOTE: Job configurations may have changed and new changes will be reflected within the job instances. /// </summary> /// <returns>Returns a list of queued job instances.</returns> public List <JobInstance> RecoverJobInstancesFromQueueLog() { if (!JobQueueManager.IsSafeForQueueLogRetrieval) { throw new Exception("The Job Queue Manager is in an unsafe state as job statuses may change. Queue retrieval from the database is disallowed."); } bool queueManagerWasStarted = JobQueueManager.Status == JobQueueManagerStatus.Started ? true : false; if (queueManagerWasStarted) { JobQueueManager.Stop(); } // clear the in-memory queue JobQueueManager.DequeueAllJobs(); List <JobInstance> queuedJobInstances = new List <JobInstance>(); using (var dbContext = new ls.QueueManagementDataContext(_connectionString)) { foreach (var jobInstanceGuid in dbContext.JobQueues.Select(d => d.JobInstanceId).ToList()) { var jobInstance = GetJobInstanceFromQueueLog(jobInstanceGuid, throwExceptionIfSyncObjectsAreDeletedOrDisabled: false); if (jobInstance == null) { DeleteFromQueueLog(jobInstanceGuid); continue; } else { switch (jobInstance.Status) { case JobQueueStatus.Scheduled: case JobQueueStatus.Delayed_NoAvailableThread: case JobQueueStatus.InProgress: case JobQueueStatus.InProgress_MaxDelayExceeded: DeleteFromQueueLog(jobInstanceGuid); // all job instances will start over with JobQueueStatus = Scheduled JobQueueManager.QueueJob(jobInstance); queuedJobInstances.Add(jobInstance); break; case JobQueueStatus.Completed: case JobQueueStatus.Completed_WithError: case JobQueueStatus.Terminated_WithError: case JobQueueStatus.DidNotRun_ErrorWithinQueueRequest: MoveToHistoryLog(jobInstance); break; default: throw new EnumValueNotImplementedException <JobQueueStatus>(jobInstance.Status); } } } } if (queueManagerWasStarted) { JobQueueManager.Restart(); } return(queuedJobInstances); }