Ejemplo 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;
                }
            }
        }
Ejemplo n.º 2
0
 public Maybe <Advice> GetAdviceById(int id)
 {
     return(AdviceRepository.GetByKey(id));
 }