4

Hangfire.io supports making a CRON-like scheduling of recurring jobs. But how do I specify, that a specific job should be run once, at a specific date/time, e.g. that a job should be run June 4th 2016, at 16:22 - and only at that specific point in time?

A similar way to ask the same question could be: how large a subset of the CRON expression described here, is supported by Hangfire? (The described CRON expression supports a "Year"-field which could be used).

Also, do you think Hangfire is the best choice to schedule one-off batch jobs in the first place, provided that I use Hangfire for job processing?

someName
  • 1,275
  • 2
  • 14
  • 33
  • I know that is a old question. But since cron expressions doesn't handle years just (minute, hour, day, month, day of week) the following cron expression: `22 16 4 6 *` means: “At 16:22 on day-of-month 4 in June.” - Please see the following site: https://crontab.guru/#22_16_4_6_* – Flavio Francisco Jun 03 '19 at 08:05

3 Answers3

3

You can use BackgroundJob.Schedule(Expression> methodCall, DateTimeOffsetdt) method.

BackgroundJob.Schedule(methodCall, enqueueAt);
Singha
  • 37
  • 7
1

In one of my application we schedule a job for only once run in a particular date time. Look into below code

  public string Schedule(Expression<Action> methodToCall, DateTimeOffset enqueueAt)
    {
        return BackgroundJob.Schedule(methodToCall, enqueueAt);
    }

Where enqueueAt is the date time when you want to run the job.

Singha
  • 37
  • 7
0

Cron expression with Year is not supported by Hangfire.

To run a job at specific point in time, use following schedule method overload from BackgroundJob class.

public static string Schedule([InstantHandle] Expression<Action> methodCall, DateTimeOffset enqueueAt);

BackgroundJob.Schedule(() => Console.Write("test"), new DateTime(2016, 6, 4, 16, 22, 0));
  • When you try to specify year part in cron expression by calling `RecurringJob.AddOrUpdate("test", () => Console.Write("test"),"44 16 2 FEB * 2016");` It throws this exception `{"'44 16 2 FEB * 2016' is not a valid crontab expression. It must contain at least 5 components of a schedule (in the sequence of minutes, hours, days, months, days of week)."}` – JogiKalpesh Mar 03 '16 at 00:46
  • Thats not a valid CRON format that Hangfire supports. Refer to this: https://en.wikipedia.org/wiki/Cron#CRON_expression – Erick Smith Apr 14 '16 at 13:18