Hangfire recurring job daily on specific time

I am trying to run hangfire recurring job daily on 9.00 AM. Here is what I want to do-

RecurringJob.AddOrUpdate(() => MyMethod(), "* 9 * * *");

Where should I put this line of code?

Sorry if this is a foolish question.

Assuming you are using .Net Core, where you can find the file startup.cs. In that you can find a Configure() method. Inside the method you can use that piece of line right after app.UseHangfireDashboard() and app.UseHangfireServer() which is for configuring hangfire dashboard and this is optional. Don't forget to Register Hangfire Services in ConfigureServices() method which can be found in the startup.cs itself.

You can Register Hangfire Services inside ConfigureServices() in Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {    
    /*
    other services
    */

    services.AddHangfire(x => x.UseSqlServerStorage("YOUR_HangfireServerConnectionString"));

    /*
    services.AddMvc()
    */
    }

You can Set Hangfire Cron inside Configure() in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseHangfireDashboard();  
    app.UseHangfireServer();
    RecurringJob.AddOrUpdate(() => MyMethod(), "* 9 * * *");
}

for more refer the link

UPDATE

The cron expression * 9 * * * denotes that the job will fires every minutes after 9 (24 hour format) of system time UTC time.

For creating a recurring job at 9.00 AM Daily, the expression should be 0 9 * * * refer here cron expressions

Performing recurrent tasks — Hangfire Documentation, For Hangfire "0 2 1/1 * *" is a right value for everyday 2 am not one above. It seems is there a way to declarer a specific time to the recurring job to happen ? This line creates a new entry in persistent storage. A special component in Hangfire Server (see Processing background jobs) checks the recurring jobs on a minute-based interval and then enqueues them as fire-and-forget jobs. This enables you to track them as usual.

In asp.net, you can add Microsoft.Owin Middleware and OwinStartup to your project via this nugetpackage and then use the startup.cs to setup hangfire. We always use the Configuration method of Startup.cs file for the hangfire recurring jobs:

            public void Configuration(IAppBuilder app) 
            {
                app.UseHangfireDashboard();
                app.UseHangfireServer();
                RecurringJob.AddOrUpdate(19872.ToString(),() => MyMethod(), Cron.Daily(9, 0));
            }

How to queue one shot job for a specific time? - question, In the docs I can queue up recurring jobs like daily jobs for a specific time but I can't seem to find a one time job that occurs like at 23 August at� I am looking for a code example where one sets the start time for a job to a specific time and set to repeat every 2 hours. Chris_Jones 2016-07-04 14:10:05 UTC #2 The Cron object you use to specify the delay has overloads to specify the start hour and minute.

I use a web api endpoint for creating jobs. I always give the recurring job a unique identifier like a guid. That way, if i need to edit the recurring jobs chron expression and can update it via my web api endpoint.

So in the web api i have :-

[HttpPost]
[Microsoft.AspNetCore.Mvc.Route("daily")]
public IActionResult AddOrUpdateDailyReportJob([FromBody]JobOptions options)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var result = _clientReportJobCreator.AddOrUpdateDailyClientReport(options.JobId, options.ChronExpression);

    return Ok(result);
}

And a service method like this that adds or updates an existing recurring job.

 public JobCreationResult AddOrUpdateDailyClientReport(string jobId, string chronExpression)
        {
            try
            {
                RecurringJob.AddOrUpdate(jobId, () => _clientDailyReportService.Run(), chronExpression);

                var result = new JobCreationResult
                {
                    JobId = jobId,
                    Success = true
                };

                return result;
            }
            catch (Exception ex)
            {
                var result = new JobCreationResult
                {
                    JobId = jobId,
                    Success = false,
                    Errors = new List<string>()
                    {
                        $"{ex.Message}"
                    }
                };

                return result;
            }
        }

Create recurring job to start at specific time and run every 2 hours , Create recurring job to start at specific time and run every 2 hours to schedule a recurring job that will run daily, but don't want it to start running I've been googling around and couldn't find a way of doing it in Hangfire. 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).

Create Hangfire recurring job daily on specific time, I am trying to run hangfire recurring job daily on 11.00 AM. Here is what I want to do- RecurringJob.AddOrUpdate(() => MyMethod(), "* 11 * * *")� Recurring job is a timer that enqueues a job at specific time intervals defined with a cron expression. What is important is, that it does not execute the job. Only enqueues an ordinary Hangfire job. This implementation is very elegant, but it also means that if the queue is full the job will have to wait for its turn. So there is no guarantee about the time it will actually execute. Cron expressions

Start Recurring job at a certain time � Issue #504 � HangfireIO/Hangfire, Hi. Can we start Recurring job at a certain time For example: Start Recurring job (recurring every 10 minutes) at 18:34 22.07.2016. You could explore the avenue of using a hangfire Scheduled job to create the RecurringJob: new BackgroundJobClient().Schedule( () => Hangfire.RecurringJob.AddOrUpdate( () => Console.WriteLine("Hello World"), Cron.Daily()), startDateTime) Which could be set to run the Scheduled job shortly before the first expected run time of the Recurring Job, and thus hangfire runs the schedule and creates the job itself.

Hangfire recurring job daily on specific time - asp.net-mvc - iOS, I am trying to run hangfire recurring job daily on 9.00 AM. Here is what I want to do- RecurringJob.AddOrUpdate(() => MyMethod(), "* 9 * * *"); Where should I put � Time zones is an essential part of recurring jobs, I agree. It was a strange decision to use UTC time zone by default instead of a local one, as all other frameworks and programs do that for CRON-based launchers. Looks like I was drunk ;) I have a simple implementation for this, now thinking about time zone identifiers on Windows and Linux

Comments
  • thanks for your reply. Unfortunately, I am not using .net core.
  • For your information, If you don't want the same job running from different instances, You should give a unique identifier while adding the cron like this RecurringJob.AddOrUpdate("YOUR_UNIQUE_STRING",() => MyMethod(), "* 9 * * *");
  • @s.k.paul Don't forget in Hangfire to daily schedule. Works like this: RecurringJob.AddOrUpdate(() => MyMethod(), Cron.Daily(9,0));
  • @WladimirTeixeira I have updated the answer with cron expression. That was an unnoticed mistake i copied from the question which makes a remarkable impact on the server.