# Recurring Job every Seconds

**URL:** <https://discuss.hangfire.io/t/recurring-job-every-seconds/877>\
**Category:** question\
**Created:** [April 20, 2015, 9:33am UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877 "2015-04-20T09:33:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pietro](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/pietro/32/244_2.png) [@Pietro](https://discuss.hangfire.io/u/Pietro)\
**Post date:** [April 20, 2015, 9:33am UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/1 "2015-04-20T09:33:07Z")

</div>

Hi,  
is there a plan for a feature that allows of scheduling a Job that run every X second (10sec., 20 sec. etc…) ?

Thanks.

Pietro.

---

<div class="post-metadata">

**Author:** ![odinserj](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/odinserj/32/648_2.png) [@odinserj](https://discuss.hangfire.io/u/odinserj)\
**Post date:** [April 21, 2015, 11:51am UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/2 "2015-04-21T11:51:54Z")

</div>

Current implementation recurring job scheduler wakes up every minute, fetches current schedule records, and creates new background jobs based on matched recurring jobs. If we simply change this implementation to wake up every second, we’ll stress our storage with a high number of requests.

To avoid stressing, we can cache schedule records and update them each minute, for example. However, stale records will add a lot of fun cases, and we should think about them first.

On the other hand, I’m planning to implement support for constantly running processes with the following API:

```auto
public class MyProcess : IServerComponent
{
    public void Execute(CancellationToken shutdownToken)
    {
        // ...
    }
}

app.UseHangfireServer(new MyProcess()); // or so

```

So you’ll have an option to define such a constantly running process and use it to create background jobs even every 100 milliseconds. Hangfire already have such an infrastructure, but it is internal now for some reasons. If you are interested, I can show how to extract it to simplify the development.

---

<div class="post-metadata">

**Author:** ![Pietro](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/pietro/32/244_2.png) [@Pietro](https://discuss.hangfire.io/u/Pietro)\
**Post date:** [April 21, 2015, 4:43pm UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/3 "2015-04-21T16:43:25Z")

</div>

> [@odinserj](#):
>
> IServerComponent

Hi Sergey,  
if it is possible, I’m interested in solution that you have suggested.

Thanks

Regards

Pietro.

---

<div class="post-metadata">

**Author:** ![odinserj](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/odinserj/32/648_2.png) [@odinserj](https://discuss.hangfire.io/u/odinserj)\
**Post date:** [April 21, 2015, 4:58pm UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/4 "2015-04-21T16:58:59Z")

</div>

After answering you, I’ve thought that you can simply use `System.Threading.Timer` class in your case. However, for other long-running processes you can take these sources files:

- [`IServerComponent`](https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/Server/IServerComponent.cs) – base interface for Hangfire’s long-running processes. [`ServerWatchdog`](https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/Server/ServerWatchdog.cs) is the simplest implementation you can look.
- [`ServerSupervisor`](https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/Server/ServerSupervisor.cs) – class that creates a new thread for server component, calls its `Execute` method in a loop until `CancellationToken` instance is not canceled.
- [`AutomaticRetryServerComponentWrapper`](https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/Server/AutomaticRetryServerComponentWrapper.cs) – wrapper for `IServerComponent` to retry `Execute` method in case of exception with increasing intervals.

Some of this classes are internal yet. The usage is:

```auto
class MyComponent : IServerComponent 
{  
    public void Execute(CancellationToken shutdownToken)
    {
        /* Create background jobs, poll queues or whatever */
        shutdownToken.WaitHandle.Wait(TimeSpan.FromSeconds(15));
    }
}

var component = new MyComponent();
var supervisor = new ServerSupervisor(new AutomaticRetryServerComponentWrapper(component));

supervisor.Start(); // Starts supervisor, non-blocking
supervisor.Stop(); // Non-blocking call, calls `CancellationToken.Cancel` method.
supervisor.Dispose(); // Blocking call, waits for `Thread.Join`

```

You can place them into `Application_Start` and `Application_End methods`.

---

<div class="post-metadata">

**Author:** ![Brian\_Bakkebo](https://avatars.discourse-cdn.com/v4/letter/b/45deac/32.png) [@Brian\_Bakkebo](https://discuss.hangfire.io/u/Brian_Bakkebo)\
**Post date:** [August 3, 2017, 8:17am UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/5 "2017-08-03T08:17:52Z")

</div>

Hello  
Now it has been a few years from your posting of this and I still have not seen Hangfire support to run jobs less than 1 minute? I have read several places of several in the community wanting this functionality. But now I have requirements for this and as a longtime user of Hangfire, I would like to use Hangfire for these jobs. So I am wondering if we need to do some customized coding as you proposed above? Or is there something simple that can be done to allow jobs to run every 30 seconds? Our scenario is quite simple, we just need to run very small fast running jobs \<1ms every 30 seconds.

---

<div class="post-metadata">

**Author:** ![cdev](https://avatars.discourse-cdn.com/v4/letter/c/8797f3/32.png) [@cdev](https://discuss.hangfire.io/u/cdev)\
**Post date:** [December 12, 2017, 2:06pm UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/6 "2017-12-12T14:06:55Z")

</div>

I came up with a solution that works for me. I use a scheduler to repeat task under a minute and allow sleep in between. For example, I want every 30 secs. I set the scheduler to run at most 2 times. it does not run second iteration if the first iteration passed the start time for second iteration.

All my tasks follow this format -

> public async Task Process(IJobCancellationToken cancellationToken)

Changed to use the scheduler hence body now looks like below where InternalProcess is the old Process.

> await \_processScheduler.Execute(async () =\> await InternalProcess(cancellationToken), DateTime.UtcNow.AddMinutes(1), MaxIterations);

Scheduler looks like below

> public async Task Execute(Func action, DateTime cutoff, byte maxIterations)  
> {  
> /\*  
> \* Example: Every 30 seconds  
> \*  
> \* DateTime.UtcNow = 00:00:00  
> \* Cutoff = 00:01:00  
> \* MaxIterations = 2  
> \*  
> \* Interval = 30 seconds  
> \*  
> \*  
> \* Iteration 1:  
> \* Next Start =  
> \* 00:00:30 (if process took less than 30 seconds)  
> \* 00:01:00 (if process took more than 30 secounds) will not run again  
> \*/
> 
> ```
> var start = DateTime.UtcNow;
> var interval = (cutoff - start).TotalSeconds / maxIterations;
> 
> for (byte iteration = 1; iteration <= maxIterations && iteration < byte.MaxValue - 1; ++iteration)
> {
> await action();
> 
> var nextStart = start.AddSeconds(iteration * interval);
> 
> while (nextStart <= DateTime.UtcNow && iteration < byte.MaxValue - 1)
> {
> ++iteration;
> nextStart = start.AddSeconds(iteration * interval);
> }
> 
> if (nextStart >= cutoff) break;
> 
> await Task.Delay(nextStart - DateTime.UtcNow);
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Archana](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Archana](https://discuss.hangfire.io/u/Archana)\
**Post date:** [February 1, 2018, 5:24pm UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/7 "2018-02-01T17:24:27Z")

</div>

Hello Sergey,  
We are using Hangfire to run our background job; but we want scheduler to constantly get updated jobs from database.  
In short, we want recurring scheduler to run every second.

Could you please provide details on this?

---

<div class="post-metadata">

**Author:** ![Yura\_Yeshurin](https://avatars.discourse-cdn.com/v4/letter/y/ce73a5/32.png) [@Yura\_Yeshurin](https://discuss.hangfire.io/u/Yura_Yeshurin)\
**Post date:** [January 23, 2019, 11:08am UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/8 "2019-01-23T11:08:47Z")

</div>

Hi Sergey,  
We’re checking option to use Hangfire in our product, but we’re need below-the-minute resolution as a must.  
As I understood from reading similar articles - it need to upgrade whole internal Hangfire’s infrastructure.  
Few questions:

1. Does this functional exists in PRO version?
2. I saw you mentioned that this functional will be added in Hangfire 2.\* - is this correct? Any timeline?
3. If you can provide short description how-to-implement it - I can try to implement it by my own and send you a PR. Looks like description from beginning of the topic is obsolete

---

<div class="post-metadata">

**Author:** ![Yura\_Yeshurin](https://avatars.discourse-cdn.com/v4/letter/y/ce73a5/32.png) [@Yura\_Yeshurin](https://discuss.hangfire.io/u/Yura_Yeshurin)\
**Post date:** [January 23, 2019, 12:45pm UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/9 "2019-01-23T12:45:07Z")

</div>

Please see my answer above

---

<div class="post-metadata">

**Author:** ![Zapnologica](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/zapnologica/32/2045_2.png) [@Zapnologica](https://discuss.hangfire.io/u/Zapnologica)\
**Post date:** [November 23, 2022, 5:56am UTC](https://discuss.hangfire.io/t/recurring-job-every-seconds/877/10 "2022-11-23T05:56:26Z")

</div>

I am also interested in a way to have a permanently running process etc that could do very short schedules or background tasks.
