Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for seconds in NCronTab expressions #368

Closed
wants to merge 2 commits into from

Conversation

edokan
Copy link

@edokan edokan commented May 14, 2015

added support for seconds in NCronTab expressions.

Needed to update NCronTab and CronExpressionDescriptor to latest versions to ensure seconds part is compatible. Seconds support is kinda hacky.

Added some unit tests.

@odinserj
Copy link
Member

Does it work? The RecurringJobScheduler class is still waking up every minute.

@edokan
Copy link
Author

edokan commented May 18, 2015

Actually, no. Well, partially. This patch allows, one to be able to specify seconds in crontab expressions, and my test are all about that. Actual work is to get scheduler to process them in seconds not in minutes as you explained. In my stupidest hour, I've assumed that should be enough, but it actually is not. Now I've enabled "seconds" in scheduler and some other places. It is less than ideal (SQL polling interval is now .5 seconds when second resolution is enabled), but It looks like it is working. Today I will create another pull request for that change. I'm new to all this pull request, so please forgive me.

@edokan
Copy link
Author

edokan commented May 18, 2015

QuickDoc: GlobalConfiguration.Configuration.UseSecondResolution() to enable it

@odinserj
Copy link
Member

Don't worry, let's do it 😄

@odinserj
Copy link
Member

Is there any way to lower the number of resulting classes without loosing their simplicity?

@odinserj
Copy link
Member

I think that second and minute throttling are the only cases, other invervals are unlikely be implemented.

@odinserj
Copy link
Member

Oh, @edokan, can you also re-target this feature against the dev branch? master is used only for bug fixes.

@edokan
Copy link
Author

edokan commented May 18, 2015

Hi,

Here are my replies.

We had outsourced a project that uses hangfire for scheduled jobs, which is pretty nice, but they set so jobs are scheduled to work on every 15 minutes.

What is the purpose of this change? I don't think it is necessary.

we needed some jobs to be scheduled to run on every 15 seconds, and there is an issue (#167) for that waiting for about a year.

Is there any way to move SchedulerResolution dependency from ScheduleInstant to RecurringJobScheduler?

I think it is possible, but code may not be cleaner. Let me have a look.

Is there any way to lower the number of resulting classes without loosing their simplicity?

It is possible, My first implementation used a lot less classes, then I refactored. Simplicity my not suffer that much

I think that second and minute throttling are the only cases, other invervals are unlikely be implemented.

"minute" implementation covers 99.9% of cases, with "second" implementation, almost every case should be covered. I just did not want to leak to your implementation too much.

Oh, @edokan, can you also re-target this feature against the dev branch? master is used only for bug fixes.

I will.

@odinserj
Copy link
Member

Ok, will wait for the changes. Regarding to the "What is the purpose of this change? I don't think it is necessary.", I've asked it because "every second" functionality should not cause to change the JobInitializationWaitTimeout.

{
public abstract class SchedulerResolution
{
private static SchedulerResolution _current = new MinuteSchedulerResolution();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but I don't like that the abstract class is aware of its inheritors.

@odinserj
Copy link
Member

Hi @edokan! Any news on this PR?

srudenko pushed a commit to srudenko/Hangfire that referenced this pull request Jul 8, 2015
@jmalczak
Copy link

Hi Guys. Is there any progress with that? I would like to use hangfire to pull amazon simple queue service every 20 seconds.

@Moulde
Copy link

Moulde commented Nov 16, 2015

+1 :) We also have a need for less than minute recurring jobs.

@lsoler
Copy link

lsoler commented Nov 16, 2015

+1 Hi! I would also like to use hangfire for recurring jobs every 30 seconds.

@tukaef
Copy link

tukaef commented Mar 2, 2017

+1

@odinserj
Copy link
Member

odinserj commented Jul 7, 2017

Use background processes appeared in Hangfire 1.5.0 to handle sub-minute intervals. Recurring jobs currently too slow to handle such intervals, since all of them are iterated on each run, harnessing the storage. Their implementation will be changed in Hangfire 2.0, opening a way to use seconds in intervals.

@Jedarc
Copy link

Jedarc commented Apr 25, 2018

Hello my friends, today I have an activity that is requiring recurring processes every 20 seconds. I know this item stayed for version 2.0, but is there any way I can implement it myself in my application?

@burningice2866
Copy link
Contributor

@Jedarc you can register a custom BackgroundProcess and set the delay inside it to 20 seconds. I do this for all my jobs which needs to run continuously/more often than ever minute.

Create a class that implements IBackgroundProcess and pass the instance of that class to UseHangfireServer

@Jedarc
Copy link

Jedarc commented Apr 25, 2018

@burningice2866 many thanks for the quick response, would you have any examples of this implementation, please?

The documentation is a bit obscure to me, and I can not imagine how I could implement the interface to allow my recurring jobs to run within a set amount of seconds.

I looked for examples, but I still did not succeed progress.

@burningice2866
Copy link
Contributor

burningice2866 commented Apr 25, 2018

@Jedarc The interface has a single method where you implement your logic.

The easiest is to wrap your code in a while (!context.IsShutdownRequested) block, and as the last thing in the block you call context.CancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(20));

So something like

public void Execute(BackgroundProcessContext context)
{
   var twentySeconds = TimeSpan.FromSeconds(20);

   while (!context.IsShutdownRequested)
   {
      ... your logic

      context.CancellationToken.WaitHandle.WaitOne(twentySeconds);
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

9 participants