public object Perform(JobActivator activator, IJobCancellationToken cancellationToken) { if (activator == null) throw new ArgumentNullException("activator"); if (cancellationToken == null) throw new ArgumentNullException("cancellationToken"); object instance = null; object result; try { if (!Method.IsStatic) { instance = Activate(activator); } var deserializedArguments = DeserializeArguments(cancellationToken); result = InvokeMethod(instance, deserializedArguments, cancellationToken.ShutdownToken); } finally { Dispose(instance); } return result; }
private object[] DeserializeArguments(IJobCancellationToken cancellationToken) { try { var parameters = Method.GetParameters(); var result = new List<object>(Arguments.Length); for (var i = 0; i < parameters.Length; i++) { var parameter = parameters[i]; var argument = Arguments[i]; object value; if (typeof(IJobCancellationToken).IsAssignableFrom(parameter.ParameterType)) { value = cancellationToken; } else { try { value = argument != null ? JobHelper.FromJson(argument, parameter.ParameterType) : null; } catch (Exception) { if (parameter.ParameterType == typeof(object)) { // Special case for handling object types, because string can not // be converted to object type. value = argument; } else { var converter = TypeDescriptor.GetConverter(parameter.ParameterType); value = converter.ConvertFromInvariantString(argument); } } } result.Add(value); } return result.ToArray(); } catch (Exception ex) { throw new JobPerformanceException( "An exception occurred during arguments deserialization.", ex); } }
/// <summary> /// Logic to run the production reset task /// </summary> /// <param name="jobContext">ProductionSetupOperation enumeration value. Passed as an int because Hangfire does not support deserialization of the Enumeration class</param> /// <param name="jobCancellationToken"></param> /// <returns></returns> protected override async Task <WorkflowResult> RunJob(int jobContext, IJobCancellationToken jobCancellationToken) { var sqlConfig = await _getOdsSqlConfigurationQuery.Execute(); _logger.Info($"User requested {WorkflowJobName} operation 'Interactive SetUp'"); var result = await _productionDatabaseLifecycleManagementService.ResetToMinimal( sqlConfig, jobCancellationToken?.ShutdownToken ?? CancellationToken.None); return(result); }
public async Task RunAsync(string engineId, IJobCancellationToken jobToken) { (Engine engine, EngineRuntime runtime) = await _engineService.GetEngineAsync(engineId); // the engine was removed after we enqueued the job, so exit if (engine == null) { return; } await runtime.BuildAsync(engine, _buildHandler, jobToken); }
public void Run(PerformContext context, IJobCancellationToken token) { foreach (var pair in context.Items) { System.Console.WriteLine($"{pair.Key} = {pair.Value}"); } while (!token.ShutdownToken.IsCancellationRequested) { Thread.Sleep(100); } }
public async Task Run(long showId, string showName, IJobCancellationToken jobCancellationToken) { var show = new Show { Id = showId, Name = showName, Cast = await this.GetCast(showId, jobCancellationToken.ShutdownToken) }; BackgroundJob.Enqueue <ISaveShowJob>( job => job.Run(show, JobCancellationToken.Null)); }
public ReadBatteryAndFirmwareCommand(ILogger <ReadBatteryAndFirmwareCommand> logger, IDeviceLockManager deviceLockManager, DatabaseContext databaseContext, IDeviceCommunicationService deviceService, IJobManager jobManager, IJobCancellationToken cancellationToken) { this.logger = logger; this.deviceLockManager = deviceLockManager; this.databaseContext = databaseContext; this.deviceService = deviceService; this.jobManager = jobManager; this.cancellationToken = cancellationToken.ShutdownToken; }
public DetectSensorCommand(ILogger <DetectSensorCommand> logger, IDeviceCommunicationService deviceService, IDeviceLockManager deviceLockManager, DatabaseContext databaseContext, LogEntryHandler logEntryHandler, IJobCancellationToken cancellationToken) { this.logger = logger; this.deviceLockManager = deviceLockManager; this.databaseContext = databaseContext; this.logEntryHandler = logEntryHandler; this.deviceService = deviceService; this.cancellationToken = cancellationToken.ShutdownToken; }
protected void SendCommonEmail(int electionId, IJobCancellationToken cancellationToken, Func <Election, EmailDefinition> emailDefinitionFetcher, RecipientIdsGenerator recipientIdsGenerator = null ) { Election election = GetElection(electionId); EmailDefinition definition = emailDefinitionFetcher(election); cancellationToken.ThrowIfCancellationRequested(); SendEmail(election, definition, recipientIdsGenerator, cancellationToken); }
public void Execute(int jobNumber, IJobCancellationToken cancellationToken) { for (var i = 0; i < 10; i++) { if (null != cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); } Thread.Sleep(5000); Console.WriteLine($"Fire-and-forget job {jobNumber} - {i + 1}"); } }
private object InvokeMethod(object instance, object[] deserializedArguments, IJobCancellationToken cancellationToken) { try { return(Method.Invoke(instance, deserializedArguments)); } catch (TargetInvocationException ex) { CoreBackgroundJobPerformer.HandleJobPerformanceException(ex.InnerException, cancellationToken, null); throw; } }
private void RunJobImpl(IJobCancellationToken jobCancellationToken, string jobRunTimestamp) { Logger.Info($"Begin Batch Job Waiting For Job Specific Lock for job: \"{JobName}\", jobRunTimestamp: {jobRunTimestamp} "); lock (ScheduledBackgroundLockByJobName[JobName]) { Logger.Info($"End Batch Job Waiting For Job Specific Lock for job: \"{JobName}\", jobRunTimestamp: {jobRunTimestamp} "); var stopWatch = Stopwatch.StartNew(); Logger.Info($"Begin Batch RunJobImplementation for job: \"{JobName}\", jobRunTimestamp: {jobRunTimestamp}"); RunJobImplementation(jobCancellationToken); stopWatch.Stop(); Logger.Info($"End Batch RunJobImplementation for job: \"{JobName}\", jobRunTimestamp: {jobRunTimestamp}, elapsed time: {stopWatch.Elapsed}"); } }
public DetectDeviceCommand(ILogger <DetectDeviceCommand> logger, IDeviceLockManager deviceLockManager, DatabaseContext databaseContext, IDeviceCommunicationService deviceService, IOptions <JsonOptions> options, LogEntryHandler logEntryHandler, IJobCancellationToken cancellationToken) { this.logger = logger; this.databaseContext = databaseContext; this.deviceService = deviceService; this.logEntryHandler = logEntryHandler; this.deviceLockManager = deviceLockManager; this.jsonSerializerOptions = options.Value.JsonSerializerOptions; this.cancellationToken = cancellationToken.ShutdownToken; }
public async Task Run(IJobCancellationToken token) { token.ThrowIfCancellationRequested(); try { await _objectsCacheHelper.PopulateObjects(); } catch (Exception exc) { _logger.LogError(exc, "ObjectsCacheJob error"); } }
public async Task Execute(IJobCancellationToken cancellationToken) { using (this._logger.BeginScope("Executing removal of expired files")) { IList <UploadedFile> uploadedFiles = await this._uploadedFileRepository.GetFiles().ConfigureAwait(false); foreach (UploadedFile uploadedFile in uploadedFiles) { cancellationToken.ThrowIfCancellationRequested(); this.ProcessSingleFile(uploadedFile); } } }
//[Queue("a2")] public static void LongRunning2(IJobCancellationToken cancellationToken) { for (var i = 0; i < int.MaxValue; i++) { if (cancellationToken.ShutdownToken.IsCancellationRequested) { Trace.WriteLine("Task Cancel"); } cancellationToken.ThrowIfCancellationRequested(); Thread.Sleep(TimeSpan.FromSeconds(1)); } }
public void ConvertAllOldJobs(IJobCancellationToken cancellationToken) { long loops = 0; int maxLoops = new Random().Next(1000, 10000); while (loops != maxLoops) { cancellationToken.ThrowIfCancellationRequested(); var jobs = _mainDBContext.Jobs.ToList(); loops++; } }
/// <summary> /// This wraps the call to <see cref="RunJobImplementation"/> with all of the housekeeping for being a scheduled job. /// </summary> public void RunJob(IJobCancellationToken jobCancellationToken) { // ReSharper disable once StringLiteralTypo var jobRunTimestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff"); try { // No-Op if we're not running in an allowed environment if (!RunEnvironments.Contains(FirmaWebConfiguration.FirmaEnvironment.FirmaEnvironmentType)) { return; } switch (_concurrencySetting) { case ConcurrencySetting.RunJobByItself: Logger.Info($"Begin Batch Job Waiting For Global Lock for job: \"{JobName}\", jobRunTimestamp: {jobRunTimestamp}"); lock (ScheduledBackgroundGlobalJobLock) { Logger.Info($"End Batch Job Waiting For Global Lock for job: \"{JobName}\", jobRunTimestamp: {jobRunTimestamp}"); RunJobImpl(jobCancellationToken, jobRunTimestamp); } break; case ConcurrencySetting.OkToRunWhileOtherJobsAreRunning: RunJobImpl(jobCancellationToken, jobRunTimestamp); break; default: // ReSharper disable once LocalizableElement throw new ArgumentOutOfRangeException(nameof(jobCancellationToken), _concurrencySetting, "Value out of range"); } } catch (OperationCanceledException) { // warn and swallow the exception - this just means that app pool was shutting down and that hangfire shut down our worker job Logger.Warn($"Job exited due to a hangfire cancellation - job: \"{JobName}\", jobRunTimestamp: {jobRunTimestamp}"); } catch (Exception ex) { // Wrap and rethrow with the information about which job encountered the problem throw new ScheduledBackgroundJobException(JobName, ex); } finally { // Reset thread setting to default, this thread may be used to do other work SitkaHttpRequestStorage.DisposeItemsAndClearStore(); } }
public void doIt( [DisplayData("start")] DateTime start = default, [DisplayData("end")] DateTime end = default, PerformContext context = null, IJobCancellationToken cancellationToken = null) { try { // do something... Console.WriteLine("Demo"); } catch { throw; } }
public void RunProcessing(string fileId, string filePath, IJobCancellationToken cancellationToken) { using (var processor = new PngProcessor()) { processor.ProgressChanged += (status) => { ImageProcessingStorage.UpdateFileStatus(fileId, status); }; ImageProcessingStorage.UpdateFileStatus(fileId, 0); processor.Process(filePath); // Sadly, but I can't schedule it as a continuation of current job. BackgroundJob.Schedule(() => ImageProcessingStorage.RemoveFileJobPairByFileName(fileId), DateTime.Now.AddMinutes(10)); } }
protected void SendCouncilEmail( int electionId, IJobCancellationToken cancellationToken, Func <CouncilElectionData, EmailDefinition> emailFetcher, RecipientIdsGenerator recipientIdsGenerator = null ) { Election election = GetElection(electionId); EmailDefinition definition = emailFetcher(GetCouncilData( election, "Requested to send council-specific email for election that isn't a council one" )); SendEmail(election, definition, recipientIdsGenerator, cancellationToken); }
public static void Execute(int electionId, IJobCancellationToken cancellationToken) { using (var db = new VotingDbContext()) { Election election = db.Elections.Find(electionId); if (election == null) { throw new ArgumentOutOfRangeException(nameof(electionId), "No election with such id"); } if (election.DisableAutomaticEligibility) { // Nothing to do return; } using (var timetable = new TimetableDbContext()) { IEnumerable <ElectionEligibilityEntry> electionEntries = EligibilityGenerator.GenerateElectionEntries(election, timetable.Users); cancellationToken.ThrowIfCancellationRequested(); IEnumerable <PositionEligibilityEntry> positionEntries = EligibilityGenerator.GeneratePositionEntries(election, timetable.Users, db); cancellationToken.ThrowIfCancellationRequested(); // Note that we cannot dispose the timetable db context here // since the queries are only "planned" yet and not actually executed // Save election entries, only ones that do not exist in database AddNewEntries( electionEntries, db.ElectionEligibilityEntries, (entry, set) => set.Find(entry.Username, entry.Election.Id) ); cancellationToken.ThrowIfCancellationRequested(); // Save position entries, only ones that do not exist in database AddNewEntries( positionEntries, db.PositionEligibilityEntries, (entry, set) => set.Find(entry.Username, entry.Position.Id) ); cancellationToken.ThrowIfCancellationRequested(); db.SaveChanges(); } } }
private Task <bool> RunIndexJobAsync(string currentUserName, string notificationId, bool suppressInsignificantNotifications, IEnumerable <IndexingOptions> allOptions, Func <IndexingOptions, ICancellationToken, Task> indexationFunc, IJobCancellationToken cancellationToken) { var success = false; // Reset progress handler to initial state _progressHandler.Start(currentUserName, notificationId, suppressInsignificantNotifications); // Make sure only one indexation job can run in the cluster. // CAUTION: locking mechanism assumes single threaded execution. try { using (JobStorage.Current.GetConnection().AcquireDistributedLock("IndexationJob", TimeSpan.Zero)) { try { var tasks = allOptions.Select(x => indexationFunc(x, new JobCancellationTokenWrapper(cancellationToken))); Task.WaitAll(tasks.ToArray()); success = true; } catch (OperationCanceledException) { _progressHandler.Cancel(); } catch (Exception ex) { _progressHandler.Exception(ex); } finally { // Report indexation summary only for "Recurring job for automatic changes indexation." if (notificationId.IsNullOrEmpty()) { _progressHandler.Finish(); } } } } catch { // TODO: Check wait in calling method _progressHandler.AlreadyInProgress(); } return(Task.FromResult(success)); }
// We limit the number of Notifications that this job will update to 2,000 for performance reasons. // For larger numbers, the EF context was becoming very large, and the job was slowing down considerably. // We would only expect a large number of updates to be needed here following a large migration. In this // scenario, the job should be run manually to import all the data (and if this is forgotten then we // trigger a warning in Sentry). public async Task Run(IJobCancellationToken token) { Log.Information($"Starting Drug resistance profile update job"); var numberOfNotificationsStillNeedingUpdate = await _service.UpdateDrugResistanceProfiles(MaxNumberOfUpdatesPerRun); var totalNumberOfNotificationsRequiredAtStartOfRun = numberOfNotificationsStillNeedingUpdate + MaxNumberOfUpdatesPerRun; if (numberOfNotificationsStillNeedingUpdate > 0) { Log.Warning($"Discovered {totalNumberOfNotificationsRequiredAtStartOfRun} Notifications which require updates to their " + $"Drug Resistance Profile. The Drug Resistance Profile update job will only handle {MaxNumberOfUpdatesPerRun} " + $"Notifications per run, so there are still {numberOfNotificationsStillNeedingUpdate} Notifications which " + "need to be updated. Trigger the job manually in order to update these Notifications."); } Log.Information($"Finishing Drug resistance profile update job"); }
public async Task RunAsync(PerformContext context, IJobCancellationToken cancellationToken, string id) { SendReceiveJob job = await _jobRepo.UpdateAsync(DbNames.Default, j => j.Id == id && j.BackgroundJobId == null, b => b.Set(j => j.BackgroundJobId, context.BackgroundJob.Id)); if (job == null) { return; } // TODO: perform send/receive await Task.Delay(5000); await _jobRepo.DeleteAsync(DbNames.Default, job); }
public Task Run(IJobCancellationToken cancellationToken) { var sw = new Stopwatch(); sw.Start(); _logger.LogInformation("Heartbeat completed, {@details}", new { HeartbeatCompleted = new { Took = sw.ElapsedMilliseconds } }); return(Task.CompletedTask); }
public void Dispatch <T>(T taskParameters, PerformContext context, IJobCancellationToken cancellationToken) where T : ITaskParameters { if (taskParameters == null) { throw new ArgumentNullException("taskParameters"); } var handler = (ITaskHandler <T>)_taskHandlers.Find(e => e is ITaskHandler <T>); if (handler == null) { throw new Exception("not handler found for task"); } handler.Process(taskParameters); }
public async Task Notify(IJobCancellationToken cancellationToken) { try { await _birthdayNotificationsService.CheckAndNotify(() => { cancellationToken.ThrowIfCancellationRequested(); }); } catch (Exception ex) { await _errorLog.LogAsync(new Error(ex)); throw; } }
public async Task Execute(Guid budgetId, PerformContext context, IJobCancellationToken cancellationToken) { context.WriteLine($"Updating read model for budget {budgetId} started"); var budget = await _budgetRepository.BudgetForIdAsync(budgetId); var viewModel = _mapper.Map <BudgetViewModel>(budget); UpdateMemoryCache(viewModel); await UpdateNoSqlModel(viewModel, cancellationToken.ShutdownToken); await NotifyClientsBudgetDataChanged(viewModel, cancellationToken.ShutdownToken); context.WriteLine($"Updating read model for budget {budgetId} completed"); }
// Why attributes above are set. // "DisableConcurrentExecutionAttribute" should have short timeout, because this attribute implemented by following manner: newly started job falls into "processing" state immediately. // Then it tries to receive job lock during timeout. If the lock received, the job starts payload. // When the job is awaiting desired timeout for lock release, it stucks in "processing" anyway. (Therefore, you should not to set long timeouts (like 24*60*60), this will cause a lot of stucked jobs and performance degradation.) // Then, if timeout is over and the lock NOT acquired, the job falls into "scheduled" state (this is default fail-retry scenario). // We can change this default behavior using "AutomaticRetryAttribute". This allows to manage retries and reject jobs in case of retries exhaust. // In our case, the job, awaiting for previous the same job more than 10 seconds, will fall into "deleted" state with no retries. public async Task ProcessAll(IJobCancellationToken cancellationToken) { var thumbnailTasks = await _taskSearchService.SearchAsync(new ThumbnailTaskSearchCriteria() { Take = 0, Skip = 0 }); var tasks = await _taskSearchService.SearchAsync(new ThumbnailTaskSearchCriteria() { Take = thumbnailTasks.TotalCount, Skip = 0 }); Action <ThumbnailTaskProgress> progressCallback = x => { }; await PerformGeneration(tasks.Results, false, progressCallback, cancellationToken); }
public async Task Start(int battleId, IJobCancellationToken cancellationToken) { Battle battle = await _battleService.GetBattleByIdAsync(battleId); var workers = new List <Task>(); foreach (Army army in battle.Armies.Where(x => !x.IsDead)) { workers.Add(Task.Run(() => Execute(army, cancellationToken))); } await Task.WhenAll(workers); battle.Stop(); await _unitOfWork.SaveChanges(); }
public async Task Run(PerformContext context, IJobCancellationToken cancellationToken) { context.WriteLine("Job Started" + DateTime.UtcNow); var status = scoreboardService.StoreVotes(JobId); if (status) { context.WriteLine("Daily votes are stored and scoreboar is clear!"); } else { context.WriteLine("Storing FAILD BE READY"); } context.WriteLine("Job Ended" + DateTime.UtcNow); }
public void Cancelable(int iterationCount, IJobCancellationToken token) { try { for (var i = 1; i <= iterationCount; i++) { Thread.Sleep(1000); Console.WriteLine("Performing step {0} of {1}...", i, iterationCount); token.ThrowIfCancellationRequested(); } } catch (OperationCanceledException) { Console.WriteLine("Cancellation requested, exiting..."); throw; } }
public static void CancelableJob(IJobCancellationToken token) { token.ThrowIfCancellationRequested(); }
private IState PerformJob(string jobId, IStorageConnection connection, IJobCancellationToken token) { try { var jobData = connection.GetJobData(jobId); if (jobData == null) { // Job expired just after moving to a processing state. This is an // unreal scenario, but shit happens. Returning null instead of throwing // an exception and rescuing from en-queueing a poisoned jobId back // to a queue. return null; } jobData.EnsureLoaded(); var performContext = new PerformContext( _context, connection, jobId, jobData.Job, jobData.CreatedAt, token); var latency = (DateTime.UtcNow - jobData.CreatedAt).TotalMilliseconds; var duration = Stopwatch.StartNew(); var result = _process.Run(performContext, jobData.Job); duration.Stop(); return new SucceededState(result, (long) latency, duration.ElapsedMilliseconds); } catch (OperationCanceledException) { throw; } catch (JobPerformanceException ex) { return new FailedState(ex.InnerException) { Reason = ex.Message }; } catch (Exception ex) { return new FailedState(ex) { Reason = "Internal Hangfire Server exception occurred. Please, report it to Hangfire developers." }; } }