KnowHoper
KnowHoper

Reputation: 4602

MassTransit - Issue with ScheduleRecurringSend

I have a service which id like to use to provide an adapter to job scheduling via a REST api. The scheduled jobs are to make HTTP calls to various other services.

I have the following implementation:

       

[HttpPost]
            public async Task<IActionResult> Post([FromBody] ScheduleModel schedule)
            {
                _logger.LogInformation(JsonConvert.SerializeObject(schedule));
     
                var recurringSchedule = new AcmeRecurringSchedule
                {
                    Description = schedule.Description,
                    CronExpression = schedule.CronExpression,
                    ScheduleId = schedule.JobIdentifier,
                    ScheduleGroup = schedule.Group
                };
     
                var schedulerUrl = new Uri($"{_massTransitSettings.Protocol}://{_massTransitSettings.RabbitMqHost}/scheduler");
     
                var endpoint = await _busControl.GetSendEndpoint(schedulerUrl);
     
                await endpoint.ScheduleRecurringSend(schedulerUrl, recurringSchedule, new ScheduledRestInvocationMessage()
            {
                Url = new Uri("http://wwww.google.com")
            });
 
            return Ok(schedule.Key);
        }

However I am finding that the ScheduleRecurringSend method doesn't consistently persist the jobs to the Db. It's intermittent. I've tried varying the ScheduleId and Group but this yields intermittent results. I get many messages in the skipped queue.

Im using a retro-fitted version of this service into Azure Service Fabric to ensure only one service is aware of the Quartz DB location.

https://github.com/MassTransit/MassTransit-Quartz

Is this approach wrong?

Thanks

Upvotes: 0

Views: 1094

Answers (1)

Alexey Zimarev
Alexey Zimarev

Reputation: 19610

You are using it incorrectly.

First, you need to configure the bus so it uses the correct scheduler address:

var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
    var host = cfg.Host(new Uri("rabbitmq://localhost/"), h =>
    {
        h.Username("guest");
        h.Password("guest");
    });

    cfg.UseMessageScheduler(
        new Uri($"{_massTransitSettings.Protocol}://{_massTransitSettings.RabbitMqHost}/scheduler"));
});

Then, you need to use the ScheduleRecurringSend correctly. The first parameter is not the scheduler Uri, but the Uri of the service endpoint that will get the scheduled message. The scheduler Uri must be specified in the bus configuration.

Upvotes: 1

Related Questions