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

Custom IBackgroundJobStateChanger Not Being Called #2423

Open
JulianDev24 opened this issue Jul 11, 2024 · 0 comments
Open

Custom IBackgroundJobStateChanger Not Being Called #2423

JulianDev24 opened this issue Jul 11, 2024 · 0 comments

Comments

@JulianDev24
Copy link

Environment

  • Hangfire Version: 1.8.7
  • Hosting Environment: (e.g., Linux, .NET Core 8)

Issue

I am attempting to implement a custom IBackgroundJobStateChanger to intercept and log every state change in my Hangfire jobs. My goal is to execute specific logic every time a job's state changes (e.g., from Enqueued to Processing, or from Processing to Succeeded, same with failed).

Despite following the standard procedure for replacing the default IBackgroundJobStateChanger with a custom implementation, I'm observing that my custom state changer is not being invoked consistently for every state change.

Current Implementation

I've configured my custom IBackgroundJobStateChanger as follows in my Program.cs:

public void ConfigureServices(IServiceCollection services)
{
    builder.Services
    .AddTransient<IBackgroundJobStateChanger, BackgroundJobStateChanger>()
    .AddTransient<IBackgroundJobFactory, BackgroundJobFactory>()     
    .AddTransient<IBackgroundJobStateChanger>(x
    => new CustomBackgroundJobStateChanger(
        x.GetRequiredService<ILogger<CustomBackgroundJobStateChanger>>(),
        new BackgroundJobStateChanger(
            x.GetRequiredService<IJobFilterProvider>())));
}

CustomBackgroundJobStateChanger Implementation

public class CustomBackgroundJobStateChanger(
    ILogger<CustomBackgroundJobStateChanger> iLogger,
    [NotNull] IBackgroundJobStateChanger inner) : IBackgroundJobStateChanger
{
    private readonly IBackgroundJobStateChanger _inner = inner ?? throw new ArgumentNullException(nameof(inner));

    public IState ChangeState(StateChangeContext context)
    {
        iLogger.LogInformation("Entering ChangeState in CustomBackgroundJobStateChanger");
        iLogger.LogError("ChangeState {ContextBackgroundJobId} to {ContextNewState}", context.BackgroundJobId, context.NewState);
        var result = _inner.ChangeState(context);
        iLogger.LogInformation("Exiting ChangeState in CustomBackgroundJobStateChanger");
        return result;
    }
}

Questions

  1. Are there known issues or limitations with IBackgroundJobStateChanger in Hangfire 1.8.7 that could prevent a custom implementation from being invoked as expected?
  2. Is there a recommended approach or alternative method to ensure that custom logic can be executed reliably on every state change in Hangfire jobs?

Thank you for your assistance and any guidance you can provide on this issue.

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

No branches or pull requests

1 participant