jahra
jahra

Reputation: 1235

Quartz cron expression to run job with interval of minutes

I wonder if it is possible to write an cron expression with several conditions:

  1. Job should be run with given interval in minutes. For example with interval 42 minutes the fire times would be 10:00, 10:42, 11:24, 12:06 and etc.
  2. If the current minute does not end with 0 (e.g. 10:28,10:29), then cron first fire time should be 10:30. So it means that first fire time should have "round" minutes.

I hope that you understand these conditions. Is it possible to describe them with quartz cron?

Upvotes: 2

Views: 1972

Answers (3)

Riddhi
Riddhi

Reputation: 209

You can use job trigger like described below in Quartz.net 3.0:

 var jobTrigger = TriggerBuilder.Create()
                .StartNow()
                .WithSimpleSchedule(s => s
                    .WithIntervalInMinutes(42)
                    .RepeatForever())
                .Build();

And you can restart app at first round time, so it will fire first time at the same time only.

Upvotes: 1

Puchacz
Puchacz

Reputation: 2115

  1. It is not possible, see for explanation and similar issue: Quartz.net - Repeat on day n, of every m months?

  2. it is also not possible by Cron expressions. To do this, you would need to apply some complex logic, use some operator that is not present in evaluators. Why do you need this? Would you like to combine those 2 requirements and create single complex pattern?

Upvotes: 0

dandashino
dandashino

Reputation: 314

I usually use http://www.cronmaker.com/ to generate my cron expressions. And if you try the every 42 mins option you'll get the following expression: " 0 0/42 * 1/1 * ? *". As for the "round" minutes thing, you can try this when building your trigger:

ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity(JobTrigger, JobGroup)
            .WithCronSchedule(CroneExpression)
            .StartAt(new DateTimeOffset(DateTime.Now, 
             TimeSpan.FromMinutes(DateTime.Now.Minute % 10)))
            .Build();

Upvotes: 0

Related Questions