HangfireIO / HangfireIO/Hangfire

Unusual DisableConcurrentExecution attribute behaviour with worker count limitations

Open
#231 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
10.1k
Forks
1.8k
Avg merge
1h 19m
Merged PRs (30d)
1

Description

(Using Hangfire v1.1.1 & SQL Server)

Please consider the following with the worker count set to 2 on a single server, where Test() is called once to enqueue the jobs: (The logger is Log4Net but you can just replace it with Console.WriteLine)

```
private void Test()
{
_log.WarnFormat("[{0}] Queuing tasks", DateTime.Now);

BackgroundJob.Enqueue(() => TestTaskConcurrent(1));
BackgroundJob.Enqueue(() => TestTaskConcurrent(2));
BackgroundJob.Enqueue(() => TestTask(3));
BackgroundJob.Enqueue(() => TestTask(4));
}

[DisableConcurrentExecution(600)]
public static void TestTaskConcurrent(int index)
{
ILog log = LogManager.GetLogger("Hangfire.Job");

log.WarnFormat("[{0}] {1} TestTaskConcurrent started", DateTime.Now, index);
System.Threading.Thread.Sleep(10000);
log.WarnFormat("[{0}] {1} TestTaskConcurrent completed", DateTime.Now, index);
}

public static void TestTask(int index)
{
ILog log = LogManager.GetLogger("Hangfire.Job");

log.WarnFormat("[{0}] {1} TestTask started", DateTime.Now, index);
System.Threading.Thread.Sleep(10000);
log.WarnFormat("[{0}] {1} TestTask completed", DateTime.Now, index);
}
```

Both sets of jobs are set to take 10 seconds, but this is the output you get:

```
[22/10/2014 00:22:32] 1 TestTaskConcurrent started
[22/10/2014 00:22:42] 1 TestTaskConcurrent completed
[22/10/2014 00:22:42] 2 TestTaskConcurrent started
[22/10/2014 00:22:42] 3 TestTask started
[22/10/2014 00:22:52] 2 TestTaskConcurrent completed
[22/10/2014 00:22:52] 4 TestTask started
[22/10/2014 00:22:52] 3 TestTask completed
[22/10/2014 00:23:02] 4 TestTask completed
```

You would expect task #3 to start earlier at 22/10/2014 00:22:32 but instead it is blocked by task #2 which is waiting for task #1. This means that not all of the workers are able to process jobs as you might expect. The above set of tasks could have been completed in a total of 20 seconds, but instead they took 30 seconds. This works as implemented in the code, but does not seem to be optimal behaviour.

Additionally in the web UI, task #2 is shown as having a duration of 20 seconds rather than 10 seconds - I'm not sure that the duration time should include blocking caused by the DisableConcurrentExecution attribute.

http://discuss.hangfire.io/t/queue-with-1-worker-processes-more-than-1-job-concurrently/253/5?u=yngndrw

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.