Example #1
0
        public async Task Consume(ConsumeContext <global::MassTransit.Scheduling.ScheduleMessage> context)
        {
            var correlationId = context.Message.CorrelationId.ToString("N");

            var jobKey = new global::Quartz.JobKey(correlationId);

            var jobDetail = await CreateJobDetail(context, context.Message.Destination, jobKey, context.Message.CorrelationId).ConfigureAwait(false);

            var triggerKey = new global::Quartz.TriggerKey(correlationId);
            var trigger    = global::Quartz.TriggerBuilder.Create()
                             .ForJob(jobDetail)
                             .StartAt(context.Message.ScheduledTime)
                             .WithSchedule(global::Quartz.SimpleScheduleBuilder.Create().WithMisfireHandlingInstructionFireNow())
                             .WithIdentity(triggerKey)
                             .Build();

            var scheduler = _scheduler ??= await _schedulerTask.ConfigureAwait(false);

            if (await scheduler.CheckExists(trigger.Key, context.CancellationToken).ConfigureAwait(false))
            {
                await scheduler.UnscheduleJob(trigger.Key, context.CancellationToken).ConfigureAwait(false);
            }

            await scheduler.ScheduleJob(jobDetail, trigger, context.CancellationToken).ConfigureAwait(false);

            LogContext.Debug?.Log("Scheduled: {Key} {Schedule}", jobKey, trigger.GetNextFireTimeUtc());
        }
Example #2
0
        public async Task Consume(ConsumeContext <global::MassTransit.Scheduling.ScheduleRecurringMessage> context)
        {
            var jobKey = new global::Quartz.JobKey(context.Message.Schedule.ScheduleId, context.Message.Schedule.ScheduleGroup);

            var jobDetail = await CreateJobDetail(context, context.Message.Destination, jobKey).ConfigureAwait(false);

            var triggerKey = new global::Quartz.TriggerKey("Recurring.Trigger." + context.Message.Schedule.ScheduleId, context.Message.Schedule.ScheduleGroup);

            var trigger = CreateTrigger(context.Message.Schedule, jobDetail, triggerKey);

            var scheduler = _scheduler ??= await _schedulerTask.ConfigureAwait(false);

            if (await scheduler.CheckExists(triggerKey, context.CancellationToken).ConfigureAwait(false))
            {
                await scheduler.UnscheduleJob(triggerKey, context.CancellationToken).ConfigureAwait(false);
            }

            await scheduler.ScheduleJob(jobDetail, trigger, context.CancellationToken).ConfigureAwait(false);

            LogContext.Debug?.Log("Scheduled: {Key} {Schedule}", jobKey, trigger.GetNextFireTimeUtc());
        }
Example #3
0
        global::Quartz.ITrigger CreateTrigger(global::MassTransit.Scheduling.RecurringSchedule schedule, global::Quartz.IJobDetail jobDetail, global::Quartz.TriggerKey triggerKey)
        {
            var tz = TimeZoneInfo.Local;

            if (!string.IsNullOrWhiteSpace(schedule.TimeZoneId) && schedule.TimeZoneId != tz.Id)
            {
                tz = global::Quartz.Util.TimeZoneUtil.FindTimeZoneById(schedule.TimeZoneId);
            }

            var triggerBuilder = global::Quartz.TriggerBuilder.Create()
                                 .ForJob(jobDetail)
                                 .WithIdentity(triggerKey)
                                 .StartAt(schedule.StartTime)
                                 .WithDescription(schedule.Description)
                                 .WithCronSchedule(schedule.CronExpression, x =>
            {
                x.InTimeZone(tz);
                switch (schedule.MisfirePolicy)
                {
                case MissedEventPolicy.Skip:
                    x.WithMisfireHandlingInstructionDoNothing();
                    break;

                case MissedEventPolicy.Send:
                    x.WithMisfireHandlingInstructionFireAndProceed();
                    break;
                }
            });

            if (schedule.EndTime.HasValue)
            {
                triggerBuilder.EndAt(schedule.EndTime);
            }

            return(triggerBuilder.Build());
        }