コード例 #1
0
ファイル: Job.Obsolete.cs プロジェクト: xeronith/Hangfire
        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;
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
ファイル: EngineRuntime.cs プロジェクト: Andrewdt97/machine
            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);
            }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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));
        }
コード例 #7
0
 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;
 }
コード例 #8
0
 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;
 }
コード例 #9
0
        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);
        }
コード例 #10
0
 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}");
     }
 }
コード例 #11
0
 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;
     }
 }
コード例 #12
0
 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}");
     }
 }
コード例 #13
0
 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;
 }
コード例 #14
0
        public async Task Run(IJobCancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            try
            {
                await _objectsCacheHelper.PopulateObjects();
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, "ObjectsCacheJob error");
            }
        }
コード例 #15
0
        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);
                }
            }
        }
コード例 #16
0
ファイル: Job.cs プロジェクト: yuruei0308/sample.dotblog
        //[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));
            }
        }
コード例 #17
0
ファイル: JobMgr.cs プロジェクト: jonesmat/EFAPITest
        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++;
            }
        }
コード例 #18
0
        /// <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();
            }
        }
コード例 #19
0
 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;
     }
 }
コード例 #20
0
        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));
            }
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        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();
                }
            }
        }
コード例 #23
0
        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));
        }
コード例 #24
0
        // 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");
        }
コード例 #25
0
        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);
        }
コード例 #26
0
        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);
        }
コード例 #27
0
        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);
        }
コード例 #28
0
        public async Task Notify(IJobCancellationToken cancellationToken)
        {
            try
            {
                await _birthdayNotificationsService.CheckAndNotify(() =>
                {
                    cancellationToken.ThrowIfCancellationRequested();
                });
            }
            catch (Exception ex)
            {
                await _errorLog.LogAsync(new Error(ex));

                throw;
            }
        }
コード例 #29
0
        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");
        }
コード例 #30
0
        // 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);
        }
コード例 #31
0
        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();
        }
コード例 #32
0
        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);
        }
コード例 #33
0
ファイル: Services.cs プロジェクト: henningst/HangFire
        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;
            }
        }
コード例 #34
0
 public static void CancelableJob(IJobCancellationToken token)
 {
     token.ThrowIfCancellationRequested();
 }
コード例 #35
0
ファイル: Worker.cs プロジェクト: djrineh/Hangfire
        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."
                };
            }
        }