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 Job Filter attribute is not working if it is annotated at the class or method #2438

Open
iamSathishMohan opened this issue Sep 5, 2024 · 0 comments

Comments

@iamSathishMohan
Copy link

When the custom job filter attribute is not working when it is annotated at the class or method. It works only when registered as Global job filter.

Replication Steps:

  1. Create LogEverythingAttribute Custom job filter as documented in https://docs.hangfire.io/en/latest/extensibility/using-job-filters.html
  2. Create LongJob as given below
[LogEverything]
 public class LongJob
 {
     public void Execute()
     {
         Console.WriteLine("LongJob Started at {0}", DateTime.UtcNow.ToLongDateString());
         Task.Delay(6 * 60 * 1000).GetAwaiter().GetResult();
         Console.WriteLine("LongJob Finished at {0}", DateTime.UtcNow.ToLongDateString());
     }
 }
  1. Configure jobs using Configure jobs
public class ConfigureJobs(IConfiguration configuration, IRecurringJobManager manager, ILogger<ConfigureJobs> logger) : BackgroundService
{
    private readonly IConfiguration _configuration = configuration;
    private readonly IRecurringJobManager _manager = manager;
    private readonly ILogger<ConfigureJobs> _logger = logger;

    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        try
        {
            var jobOptions = new DynamicRecurringJobOptions
            {
                QueueName = "scheduler", 
                MisfireHandling = MisfireHandlingMode.Ignorable,
            };

            _manager.AddOrUpdateDynamic<LongJob>("LongJob", "scheduler", x => x.Execute(), "*/15 * * * * ?", jobOptions);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An exception occurred while creating recurring jobs.");
        }

        return Task.CompletedTask;
    }
}
  1. Register the scheduler
builder.Services.AddHostedService<ConfigureJobs>();

builder.Services.AddHangfire(configuration => configuration
                            .SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
                            .UseSimpleAssemblyNameTypeSerializer()
                            .UseRecommendedSerializerSettings()
                            .UseFilter(new AutomaticRetryAttribute { Attempts = 0 })
                            .UseInMemoryStorage(new InMemoryStorageOptions
                            {
                                IdType = InMemoryStorageIdType.Long,
                                StringComparer = StringComparer.InvariantCultureIgnoreCase // Default value, case-sensitive.
                            }));

builder.Services.AddHangfireServer(opts =>
{
    opts.WorkerCount = Environment.ProcessorCount * 5;
    opts.ServerName = "Test Scheduler";
    opts.Queues = ["scheduler"];
    opts.StopTimeout = TimeSpan.FromMinutes(3);
    opts.ShutdownTimeout = TimeSpan.FromMinutes(3);
});

only works when the attribute is registered as Global filter using .UseFilter(new LogEverythingAttribute())

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