Exemplo n.º 1
0
        /// <summary>
        /// Public only to allow HangFire to access it
        /// </summary>
        /// <param name="adviceId"></param>
        public void CreateOrUpdateJob(int adviceId)
        {
            var advice = AdviceRepository.GetByKey(adviceId);

            if (advice == null || advice.Scheduling == null || advice.AlarmDate == null)
            {
                throw new ArgumentException(nameof(adviceId) + " does not point to a valid id or points to an advice without alarm date or scheduling");
            }

            var adviceAlarmDate  = advice.AlarmDate.Value;
            var adviceScheduling = advice.Scheduling.Value;

            var adviceTriggers = AdviceTriggerFactory.CreateFrom(adviceAlarmDate, adviceScheduling);

            foreach (var adviceTrigger in adviceTriggers)
            {
                var prefix = advice.JobId;
                var jobId  = adviceTrigger.PartitionId.Match(partitionId => CreatePartitionJobId(prefix, partitionId), () => prefix);
                HangfireApi.AddOrUpdateRecurringJob(jobId, () => SendAdvice(adviceId), adviceTrigger.Cron);
            }

            if (advice.StopDate.HasValue)
            {
                //Schedule deactivation to happen the day after the stop date (stop date is "last day alive" for the advice)
                HangfireApi.Schedule(() => DeactivateById(advice.Id), new DateTimeOffset(advice.StopDate.Value.Date.AddDays(1)));
            }

            //If time has passed the trigger time, Hangfire will not fire until the next trigger data so we must force it.
            if (adviceAlarmDate.Date.Equals(OperationClock.Now.Date))
            {
                switch (adviceScheduling)
                {
                case Scheduling.Day:
                case Scheduling.Week:
                case Scheduling.Month:
                case Scheduling.Year:
                case Scheduling.Quarter:
                case Scheduling.Semiannual:
                    var mustScheduleAdviceToday =
                        advice.AdviceSent.Where(x => x.AdviceSentDate.Date == adviceAlarmDate.Date).Any() == false &&
                        WillTriggerInvokeToday() == false;
                    if (mustScheduleAdviceToday)
                    {
                        //Send the first advice now
                        HangfireApi.Schedule(() => SendAdvice(adviceId));
                    }
                    break;

                //Intentional fallthrough - no corrections here
                case Scheduling.Hour:
                case Scheduling.Immediate:
                default:
                    break;
                }
            }
        }
Exemplo n.º 2
0
        private void DeleteJobFromHangfire(Advice advice)
        {
            //Remove pending shcedules if any
            var allScheduledJobs = HangfireApi
                                   .GetScheduledJobs(0, int.MaxValue)
                                   .ToList();

            //Remove all pending calls to CreateOrUpdateJob
            var allScheduledCreateOrUpdate =
                allScheduledJobs
                .Where(x => x.Value.Job.Method.Name == nameof(CreateOrUpdateJob))
                .ToList();

            foreach (var j in allScheduledCreateOrUpdate)
            {
                var adviceIdAsString = j.Value.Job.Args[0].ToString();
                if (int.TryParse(adviceIdAsString, out var adviceId) && adviceId == advice.Id)
                {
                    HangfireApi.DeleteScheduledJob(j.Key);
                    break;
                }
            }

            //Remove all pending calls to CreateOrUpdateJob
            var allScheduledDeactivations =
                allScheduledJobs
                .Where(x => x.Value.Job.Method.Name == nameof(DeactivateById));

            foreach (var j in allScheduledDeactivations)
            {
                var adviceIdAsString = j.Value.Job.Args[0].ToString();
                if (int.TryParse(adviceIdAsString, out var adviceId) && adviceId == advice.Id)
                {
                    HangfireApi.DeleteScheduledJob(j.Key);
                    break;
                }
            }

            //Remove the job by main job id + any partitions (max 12 - one pr. month)
            HangfireApi.RemoveRecurringJobIfExists(advice.JobId);
            for (var i = 0; i < 12; i++)
            {
                HangfireApi.RemoveRecurringJobIfExists(CreatePartitionJobId(advice.JobId, i));
            }
        }
Exemplo n.º 3
0
        private void ScheduleAdvice(Advice advice)
        {
            if (advice.AdviceType == AdviceType.Immediate)
            {
                HangfireApi.Schedule(() => SendAdvice(advice.Id));
            }
            else if (advice.AdviceType == AdviceType.Repeat)
            {
                var alarmDate = advice.AlarmDate;

                if (alarmDate == null)
                {
                    throw new ArgumentException(nameof(alarmDate) + " must be defined");
                }

                //Only postpone the trigger creation if the alarm date has not been passed yet
                var runAt = OperationClock.Now.Date >= alarmDate.Value.Date ? default(DateTimeOffset?) : new DateTimeOffset(alarmDate.Value.Date);
                HangfireApi.Schedule(() => CreateOrUpdateJob(advice.Id), runAt);
            }
        }