HangfireIO / HangfireIO/Hangfire

Nested (decorated?) jobs

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

Description

Hey,

I wanted to have a job that runs another job. For instance, have a job sending out emails and that is it's only concern. Then have a job that does logging of the job that is has been provided to run.

The logging job

```
internal class LogSmsJob
{
private readonly IBackgroundJobClient _backgroundJobClient;

public LogSmsJob(IBackgroundJobClient backgroundJobClient)
{
_backgroundJobClient = backgroundJobClient;
}

public string EnqueueLog(Expression methodCall)
{
Debug.WriteLine("log before");

var jobId = _backgroundJobClient.Enqueue(methodCall);

Debug.WriteLine("log after");

return jobId;
}
}
```

Queueing of the sms job through the LogSmsJob

```
var smsJob = new SmsNotificationJob(); // TODO: provide config to constructor
var logJob = new LogSmsJob(_backgroundJobClient);
// enqueue with Hangfire
_backgroundJobClient.Enqueue(
() => logJob.EnqueueLog(() => smsJob.Send(_smsConfig, toPhoneNumber, text, null)));
```

It produces the following exception

`System.NotSupportedException: 'Anonymous functions, delegates and lambda expressions aren't supported in job method parameters: it's very hard to serialize them and all their scope in general.'`

Is there some way we can have nested (decorated, ie decorator pattern style) jobs with Hangfire?

Obviously the easy solution would be to bring the logging into the actual working job itself but I'd rather keep jobs SRPed if you know what I mean.

Contributor guide

Open the contributing guide

Research direction

Start with the LogSmsJob.EnqueueLog and IBackgroundJobClient.Enqueue calls shown in the issue, then trace how Hangfire validates and serializes job method parameters. Determine the supported scope for nested or decorator-style jobs and define done as either a documented supported approach with coverage or a clearly documented limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.