private bool Execute(Tick <TJob, TIdentity> tick)
        {
            var now = Context.System.Scheduler.Now.UtcDateTime;

            foreach (var schedule in State.Entries.Values.Where(e => ShouldTriggerSchedule(e, now)))
            {
                _jobDefinitionService.Load(schedule.Job.GetType());
                var jobDefinition = _jobDefinitionService.GetDefinition(schedule.Job.GetType());
                Log.Info("JobScheduler for Job of Name={0}; sending job of Definition={1} to JobRunner.", Name,
                         jobDefinition);

                var manager = Context.ActorSelection(Context.Parent.Path.Parent);
                manager.Tell(schedule.Job, ActorRefs.NoSender);

                Emit(new Finished <TJob, TIdentity>(schedule.JobId, now), ApplySchedulerEvent);

                var next = schedule.WithNextTriggerDate(now);
                if (next != null)
                {
                    Emit(new Scheduled <TJob, TIdentity>(next), ApplySchedulerEvent);
                }
            }

            return(true);
        }
        public async Task <IJobId> ScheduleNowAsync(IJob job, CancellationToken cancellationToken)
        {
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }

            var jobDefinition = _jobDefinitionService.GetDefinition(job.GetType());

            try
            {
                var json = _jsonSerializer.Serialize(job);

                if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Trace))
                {
                    _logger.LogTrace($"Executing job '{jobDefinition.Name}' v{jobDefinition.Version}: {json}");
                }

                await _jobRunner.ExecuteAsync(jobDefinition.Name, jobDefinition.Version, json, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                // We want the InstantJobScheduler to behave as an out-of-process scheduler, i.e., doesn't
                // throw exceptions directly related to the job execution
                _logger.LogError(e, $"Execution of job '{jobDefinition.Name}' v{jobDefinition.Version} failed!");
            }

            return(JobId.New);
        }
示例#3
0
        private Task <IJobId> ScheduleAsync(IJob job, Func <IBackgroundJobClient, JobDefinition, string, string> schedule)
        {
            var jobDefinition = _jobDefinitionService.GetDefinition(job.GetType());
            var json          = _jsonSerializer.Serialize(job);

            var id = schedule(_backgroundJobClient, jobDefinition, json);

            _log.Verbose($"Scheduled job '{id}' in Hangfire");

            return(Task.FromResult <IJobId>(new HangfireJobId(id)));
        }
示例#4
0
        public async Task <IJobId> ScheduleNowAsync(IJob job, CancellationToken cancellationToken)
        {
            var jobDefinition = _jobDefinitionService.GetDefinition(job.GetType());
            var json          = _jsonSerializer.Serialize(job);

            _log.Verbose(() => $"Executing job '{jobDefinition.Name}' v{jobDefinition.Version}: {json}");

            // Don't schedule, just execute...
            await _jobRunner.ExecuteAsync(jobDefinition.Name, jobDefinition.Version, json, cancellationToken).ConfigureAwait(false);

            return(JobId.New);
        }
示例#5
0
        private async Task <IJobId> ScheduleAsync(
            IJob job,
            CancellationToken cancellationToken,
            Func <IBackgroundJobClient, JobDefinition, string, string, string> schedule)
        {
            var jobDefinition = _jobDefinitionService.GetDefinition(job.GetType());
            var json          = _jsonSerializer.Serialize(job);
            var name          = await _jobDisplayNameBuilder.GetDisplayNameAsync(job, jobDefinition, cancellationToken).ConfigureAwait(false);

            var id = schedule(_backgroundJobClient, jobDefinition, name, json);

            _log.Verbose($"Scheduled job '{id}' with name '{name}' in Hangfire");

            return(new HangfireJobId(id));
        }
示例#6
0
        public async Task <IJobId> ScheduleNowAsync(IJob job, CancellationToken cancellationToken)
        {
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }

            var jobDefinition = _jobDefinitionService.GetDefinition(job.GetType());

            try
            {
                var json = _jsonSerializer.Serialize(job);

                _logger.LogDebug(
                    "Executing job {JobName} v{JobVersion}: {Json}",
                    jobDefinition.Name,
                    jobDefinition.Version,
                    json);

                await _jobRunner.ExecuteAsync(jobDefinition.Name, jobDefinition.Version, json, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                // We want the InstantJobScheduler to behave as an out-of-process scheduler, i.e., doesn't
                // throw exceptions directly related to the job execution

                _logger.LogError(
                    e,
                    "Execution of job {JobName} v{JobVersion} failed due to {ExceptionType}: {ExceptionMessage}",
                    jobDefinition.Name,
                    jobDefinition.Version,
                    e.GetType().PrettyPrint(),
                    e.Message);
            }

            return(JobId.New);
        }