Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Running Background Worker on 1st of every month #5515


User avatar
0
ajayak created

Hi,

I have created a background worker that should run on 1st of every month. I can easily do this using hangfire cron jobs but I'm not sure, what is the effect of Timer.Period property has on this.

If I skip entering the timer property, I get exception. If I fill Timer property, I don't want the code to execute on any other day than 1st of every month.

What is the best approach to achieve this? :D

I'm using something like below:

RecurringJob.AddOrUpdate<InvoiceBackgroundWorker>(job => job.Start(), Cron.MonthInterval(0));

6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ajayak,

    You should use something else I guess :) <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Background-Jobs-And-Workers#advanced-scheduling">https://aspnetboilerplate.com/Pages/Doc ... scheduling</a>

  • User Avatar
    0
    BobIngham created

    @ajayak, Hope this helps. This definition will run at midnight, first day of the month.

    RecurringJob.AddOrUpdate<InvoiceBackgroundWorker>(job => job.Start(), "0 0 1 * *");
    

    I'm sure it will be fired by server time and not by system time and therefore Timer.Period should be irrelevant. That's why clocks go back and forward at 02:00 and not 01:00!!!! If you have a system which has several timezones then it's another matter altogether, you will need a job to run at the offset between server time and UTC to run each hour and process data for each timezone in the system. Good luck with that if that's what you have to do. I once worked on a global betting system which had to do something similar!

  • User Avatar
    0
    ajayak created

    <cite>BobIngham: </cite> @ajayak, Hope this helps. This definition will run at midnight, first day of the month.

    RecurringJob.AddOrUpdate<InvoiceBackgroundWorker>(job => job.Start(), "0 0 1 * *");
    

    I'm sure it will be fired by server time and not by system time and therefore Timer.Period should be irrelevant. That's why clocks go back and forward at 02:00 and not 01:00!!!! If you have a system which has several timezones then it's another matter altogether, you will need a job to run at the offset between server time and UTC to run each hour and process data for each timezone in the system. Good luck with that if that's what you have to do. I once worked on a global betting system which had to do something similar!

    Thanks @BobIngham,

    But how will Timer property affect this? Does that mean the job will run with cron and with the set timer?

  • User Avatar
    0
    BobIngham created

    Hi @ajayak, I think I see the problem but I can't comment without seeing your code. You shouldn't need the timer at all, I think you're mixing implementations of Zero's background workers and Hangfire when you really only need one or the other. Can you show me the code where you are referring to Timer.Period and I will try advise accordingly.

  • User Avatar
    0
    ryancyq created
    Support Team

    Hi @ajayak, it seems to me that Timer.Period is to configure how frequent does your job timer runs. However the Cron format should be use for actual job scheduled timings.

    For example, timer runs at 30 mins interval and Cron job specific 12:00am. The worst case scenario will be timer trigger at 11:59pm then Cron job will not execute. The next interval for Cron job to execute will be at 12.29am

  • User Avatar
    0
    ajayak created

    Thanks @ryancyq