1

I need to schedule a job to run at 9:00AM, 12:00PM and 5:00PM on Monday to Friday only. Did not find any documentation on FluentScheduler.

I can do it by having multiple(separately for 5 days) Schedule of the job but can we have single Schedule to do this repeatedly on the given time and days?

gmsi
  • 1,062
  • 1
  • 16
  • 29

4 Answers4

1

I would have thought the simplest solution would be to have the Execute() method in your IJob check the day of the week at its entry point and bail out immediately on Saturdays or Sundays...

Pete
  • 1,807
  • 1
  • 16
  • 32
1

You could use Weekdays i.e:

var schedule = Schedule(yourJob);
schedule.ToRunEvery(0).Weekdays().At(9, 0);
schedule.ToRunEvery(0).Weekdays().At(12, 0);
schedule.ToRunEvery(0).Weekdays().At(17, 0);

ToRunEvery(0) means we need to start now.

ToRunEvery(1) would wait one interval for first execution - in our case 1 week day.

Wojciech
  • 199
  • 9
0

I ran into the same issue. FluentScheduler isn't robust enough to handle very complex schedules. A better solution would be to use http://www.quartz-scheduler.net/ It is very flexible, is supported by Topshelf, and has support for most IoC containers. For example in my service I used:

    config.Service<Service>(sc =>
      { sc.ScheduleQuartzJob(configurator =>
        configurator.WithJob(
        () => JobBuilder.Create<DataLoadJob>().WithIdentity("DataLoad", "Job1").Build())
                        .AddTrigger(() => TriggerBuilder.Create().WithIdentity("DataLoadSchedule", "Job1")
                        .WithSimpleSchedule(builder => builder.WithIntervalInSeconds(10).RepeatForever()).Build()));
             sc.ScheduleQuartzJob(configurator =>
                 configurator.WithJob(
                     () => JobBuilder.Create<DataMergeJob>().WithIdentity("DataMerge", "Job1").Build())
                     .AddTrigger(() => TriggerBuilder.Create().WithIdentity("DataMergeSchedule", "Job1")
                     .WithCronSchedule("0 30 7-20/3 ? * MON-FRI").Build()));
             sc.ConstructUsingSimpleInjector();
             sc.WhenStarted((s, h) => s.Start(h));
             sc.WhenStopped((s, h) => s.Stop(h));
         });

This is a fragment from a Topshelf service using SimpleInjector along with Quartz.

Bill Clyde
  • 220
  • 5
  • 8
0
public class Example: Registry
{
    public Example()
    {
        Schedule(() =>
        {
            DayOfWeek[] available = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };

            if (DateTime.Now.DayOfWeek.IsOn(available) && (DateTime.Now.Hour == 8 && DateTime.Now.Minute == 0))//etc
            {
                //code
            }
        }).WithName("Example").ToRunEvery(0).Hours().At(0).Between(8, 0, 17, 0);
    }
}
Raul
  • 61
  • 6