public async Task <ScheduledJob> FindAciveJobAsync(ScheduledJobType jobType) { using (var scope = _container.BeginLifetimeScope()) { var repository = scope.Resolve <IScheduledJobRepository>(); await repository.CancelExpiredJobsAsync(jobType); ScheduledJob existingJob = null; var jobs = await repository.GetActiveJobsAsync(jobType); if (jobs != null && jobs.Any()) { existingJob = jobs.OrderByDescending(j => j.StartDate).First(); foreach (var job in jobs) { if (job.JobId != existingJob.JobId) { job.Status = JobStatus.Cancelled; job.CompletedDate = DateTime.UtcNow; repository.Commit(); } } } return(existingJob); } }
public async Task <ScheduledJob> StartJobAsync(ScheduledJobType jobType) { using (var scope = _container.BeginLifetimeScope()) { var repository = scope.Resolve <IScheduledJobRepository>(); var existingJob = await FindAciveJobAsync(jobType); if (existingJob == null) { existingJob = repository.Add(new ScheduledJob { StartDate = DateTime.UtcNow, CompletedDate = null, JobType = jobType, JobName = jobType.ToString(), Status = JobStatus.Pending }); repository.Commit(); } else { if (existingJob.Status == JobStatus.Pending || existingJob.Status == JobStatus.Paused) { existingJob.Status = JobStatus.Pending; repository.Commit(); } } return(existingJob); } }
public async Task CancelExpiredJobsAsync(ScheduledJobType jobType) { using (var scope = _container.BeginLifetimeScope()) { var repository = scope.Resolve <IScheduledJobRepository>(); await repository.CancelExpiredJobsAsync(jobType); } }
public async Task <IList <ScheduledJob> > GetHistoryAsync(ScheduledJobType jobType) { var jobs = await Dbset .Where(j => (j.Status == JobStatus.Cancelled || j.Status == JobStatus.Completed || j.Status == JobStatus.Error) && (j.JobType == jobType || jobType == ScheduledJobType.All)) .OrderByDescending(j => j.StartDate).ToListAsync(); return(jobs); }
public async Task <IList <ScheduledJob> > GetActiveJobsAsync(ScheduledJobType jobType) { var jobs = await Dbset.Where(j => (j.Status == JobStatus.InProgress || j.Status == JobStatus.Paused || j.Status == JobStatus.Pending) && (j.JobType == jobType || jobType == ScheduledJobType.All)) .OrderByDescending(j => j.StartDate).ToListAsync(); return(jobs); }
public async Task <IList <ScheduledJob> > GetActiveJobsAsync(ScheduledJobType jobType) { using (var scope = _container.BeginLifetimeScope()) { var repository = scope.Resolve <IScheduledJobRepository>(); var jobs = await repository.GetActiveJobsAsync(jobType); return(jobs); } }
/// <summary> /// Get a job instance by its type /// </summary> /// <param name="jobType"> /// Type of the job being asked for /// </param> /// <returns> /// A new instance of the job by type /// </returns> public static IScheduledJob GetJobByType(ScheduledJobType jobType) { switch (jobType) { case ScheduledJobType.ProcessExtract: return(new ProcessExtractJob()); case ScheduledJobType.ProcessPts: return(new ProcessPtsJob()); case ScheduledJobType.ProcessAcknowledgment: return(new ProcessAcknowledgmentJob()); case ScheduledJobType.PingJob: return(new PingJob()); case ScheduledJobType.AmexOfferRegistrationFileSync: return(new AmexOfferRegistrationFileSyncJob()); case ScheduledJobType.DiscountActivationJob: return(new DiscountActivationJob()); case ScheduledJobType.ProcessAmexTransactionLog: return(new ProcessAmexTransactionLogJob()); case ScheduledJobType.ProcessAmexStatementCredit: return(new ProcessAmexStatementCreditJob()); case ScheduledJobType.ProcessMasterCardFiltering: return(new ProcessMasterCardFilteringJob()); case ScheduledJobType.ProcessMasterCardClearing: return(new ProcessMasterCardClearingJob()); case ScheduledJobType.ProcessMasterCardRebate: return(new ProcessMasterCardRebateJob()); case ScheduledJobType.ProcessMasterCardRebateConfirmation: return(new ProcessMasterCardRebateConfirmationJob()); case ScheduledJobType.ProcessRewardsNetworkReport: return(new ProcessRewardsNetworkReportJob()); case ScheduledJobType.ProcessVisaRebate: return(new ProcessVisaRebateJob()); default: throw new ArgumentOutOfRangeException("jobType"); } }
public async Task CancelExpiredJobsAsync(ScheduledJobType jobType) { const string sql = @" UPDATE J SET Status = 3, CompletedDate = @Now FROM ScheduledJob J WHERE J.Status IN (0, 1, 4) AND (J.JobType = @JobType OR @JobType = 0) AND (J.StartDate < @StartDate)"; await DbContext.Database.ExecuteSqlCommandAsync(sql, new SqlParameter("@Now", DateTime.UtcNow), new SqlParameter("@JobType", (int)jobType), new SqlParameter("@StartDate", DateTime.Today.AddDays(-1))); }
/// <summary> /// Get all active (Running or Paused) jobs /// </summary> /// <param name="type"> /// Type of jobs we want to query /// </param> /// <returns> /// Enumeration of all entities that match the filter /// </returns> public IEnumerable <ScheduledJobEntity> GetAllActiveJobsByType(ScheduledJobType type) { string partitionKeyFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, ScheduledJobEntity.GetPartitionKey(type)); string activeJobFilter = TableQuery.CombineFilters( TableQuery.GenerateFilterCondition("State", QueryComparisons.Equal, "Running"), TableOperators.Or, TableQuery.GenerateFilterCondition("State", QueryComparisons.Equal, "Paused")); TableQuery <ScheduledJobEntity> query = new TableQuery <ScheduledJobEntity>().Where( TableQuery.CombineFilters( partitionKeyFilter, TableOperators.And, activeJobFilter)); return(azureTableProvider.Table.ExecuteQuery(query)); }
public async Task <ScheduledJob> FindPendingJobAsync(ScheduledJobType jobType) { using (var scope = _container.BeginLifetimeScope()) { ScheduledJob result = null; var repository = scope.Resolve <IScheduledJobRepository>(); await repository.CancelExpiredJobsAsync(jobType); var jobs = await repository.GetActiveJobsAsync(jobType); if (jobs != null && jobs.Any()) { var pendingJobs = jobs.Where(j => j.Status == JobStatus.Pending).ToList(); if (pendingJobs.Any()) { if (pendingJobs.Count == 1) { result = pendingJobs.First(); } else { var mostRecent = pendingJobs.OrderByDescending(j => j.StartDate).First(); foreach (var job in pendingJobs) { if (job.JobId != mostRecent.JobId) { job.Status = JobStatus.Cancelled; job.CompletedDate = DateTime.UtcNow; repository.Commit(); } } result = mostRecent; } } } return(result); } }
public async Task <ScheduledJob> GetCurrentJobAsync(ScheduledJobType jobType) { var job = await _scheduledJobs.FindAciveJobAsync(jobType); return(job); }
public async Task <ScheduledJob> StartScheduledJobAsync(ScheduledJobType jobType) { return(await _scheduledJobs.StartJobAsync(jobType)); }
public async Task <IList <ScheduledJob> > GetJobHistoryAsync(ScheduledJobType jobType) { var jobs = await _scheduledJobs.GetHistoryAsync(jobType); return(jobs); }
/// <summary> /// Get partition key for the Entity /// </summary> /// <param name="scheduledJobType"> /// Type of the job /// </param> /// <returns> /// partition key /// </returns> public static string GetPartitionKey(ScheduledJobType scheduledJobType) { return(Enum.GetName(typeof(ScheduledJobType), scheduledJobType)); }
/// <summary> /// Get all jobs of matching on type and description in specified states /// </summary> /// <param name="type"> /// Type of jobs we want to query /// </param> /// <param name="description"> /// Description belonging to jobs we want to query /// </param> /// <param name="states"> /// States job must be in to be returned by query /// </param> /// <returns> /// Enumeration of matching jobs /// </returns> public IEnumerable <ScheduledJobDetails> GetJobsByTypeAndDescription(ScheduledJobType type, string description, ScheduledJobState states) { string partitionKeyFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, ScheduledJobEntity.GetPartitionKey(type)); string descriptionFilter = TableQuery.GenerateFilterCondition("JobDescription", QueryComparisons.Equal, description); string partitionAndDescriptionFilter = TableQuery.CombineFilters( partitionKeyFilter, TableOperators.And, descriptionFilter); IEnumerable <string> statesToQueryOn = FindAllStates(states); string stateFilter = null; foreach (string state in statesToQueryOn) { if (stateFilter == null) { stateFilter = TableQuery.GenerateFilterCondition("State", QueryComparisons.Equal, state); } else { stateFilter = TableQuery.CombineFilters(stateFilter, TableOperators.Or, TableQuery.GenerateFilterCondition("State", QueryComparisons.Equal, state)); } } TableQuery <ScheduledJobEntity> query = new TableQuery <ScheduledJobEntity>().Where( TableQuery.CombineFilters( partitionAndDescriptionFilter, TableOperators.And, stateFilter)); IEnumerable <ScheduledJobEntity> entities = azureTableProvider.Table.ExecuteQuery(query); Collection <ScheduledJobDetails> jobs = new Collection <ScheduledJobDetails>(); foreach (ScheduledJobEntity scheduledJobEntity in entities) { ScheduledJobDetails details = new ScheduledJobDetails() { JobId = new Guid(scheduledJobEntity.RowKey), JobState = (ScheduledJobState)Enum.Parse(typeof(ScheduledJobState), scheduledJobEntity.State), JobType = (ScheduledJobType)Enum.Parse(typeof(ScheduledJobType), scheduledJobEntity.PartitionKey), Recurrence = JsonConvert.DeserializeObject <Recurrence>(scheduledJobEntity.Recurrence), StartTime = scheduledJobEntity.StartTime, Version = scheduledJobEntity.Version, JobDescription = scheduledJobEntity.JobDescription }; if (scheduledJobEntity.Payload != null) { details.Payload = JsonConvert.DeserializeObject <ConcurrentDictionary <string, string> >(scheduledJobEntity.Payload); } jobs.Add(details); } return(jobs); }