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

Configuration of degree of parallelism #4

Closed
linkdotnet opened this issue Mar 17, 2024 · 5 comments · Fixed by #21
Closed

Configuration of degree of parallelism #4

linkdotnet opened this issue Mar 17, 2024 · 5 comments · Fixed by #21
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@linkdotnet
Copy link
Member

linkdotnet commented Mar 17, 2024

The options should allow a value that indicates how many jobs of a given type can run in parallel.

The configuration should be done via the registration of the job itself:

Services.AddCronJob<TheJob>(p => p.MaximumConcurrentRuns = 1);

If there is a run and MaximumConcurrentRuns is reached, the upcoming run will be omitted.
We could introduce another flag that indicates what should happen to the run:

public enum MaximumRunStrategy
{
  Omit = 0, // This would be the default - we just "drop" the run
  Reschedule = 1, // We would push the job run to the next possible scheduled run and try again
}

So a registration could look like this:

Services.AddCronJob<TheJob>(p => 
{
  p.MaximumConcurrentRuns = 1;
  p.MaximumRunStrategy = MaximumRunStrategy.Reschedule;
});

Workaround

There is a current workaround, that a job itself can implement to achieve the same.
Here an example of a job that only allows one running instance at a time:

public class MyJob : IJob
{
  private static readonly SemaphoreSlim Semaphore = new(1, 1);

  public async Task RunAsync(JobExecutionContext context, CancellationToken token)
  {
    var hasLock = await Semaphore.WaitAsync(0, token); // Try to grab the handle - if false then there is another job running
    if (hasLock)
    {
      return;
    } 
    try
    {
        // Job logic
    }
    finally
    {
        Semaphore.Release();
    }
  }
}
@linkdotnet linkdotnet added enhancement New feature or request help wanted Extra attention is needed labels Mar 17, 2024
@falvarez1
Copy link
Member

FYI, some of this is being handled in PR #21

@ryanbuening
Copy link

I'm assuming I know the answer, but wanted to ask anyway.

My .NET applications run load balanced on multiple servers. If I schedule a job to run every morning, but I only want it to run once, does NCronJob provide any mechanism to handle this? Or would this involve implementing some kind of external locking mechanism?

@linkdotnet
Copy link
Member Author

In its current version you would have to implement the lock in the job (like redis)

@falvarez1
Copy link
Member

My .NET applications run load balanced on multiple servers. If I schedule a job to run every morning, but I only want it to run once, does NCronJob provide any mechanism to handle this? Or would this involve implementing some kind of external locking mechanism?

@ryanbuening this PR does not handle Mutex locking nor distributed locking. I recommend to take a look at the DistributedLock library for help with that.

@ryanbuening
Copy link

take a look at the DistributedLock library

Very cool. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants