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

not support hangfire.console? I need use it for WriteProgressBar, but not work #2

Open
JiChao99 opened this issue Mar 7, 2024 · 1 comment

Comments

@JiChao99
Copy link

JiChao99 commented Mar 7, 2024

thanks for your work on this project.

I’ve been using Hangfire with a custom attribute to dynamically schedule and invoke jobs, and I pass specific parameters to them.

Handler

var x = scope.ServiceProvider.GetRequiredService<IPerformContextAccessor>();
var result = !method.GetParameters().Any(t => t.ParameterType == typeof(PerformContext)) ? (Task?)method.Invoke(obj, new object[] { 
tenants }) :
    (Task?)method.Invoke(obj, new object?[] { tenants, x.PerformingContext });

And here is the job method that is intended to display progress:

[CustomArrtibute("0 14 * * *")]
public Task ScheduleJob(IEnumerable<long> tenants, PerformContext performContext)
{
    var bar = performContext.WriteProgressBar(10);

    for (var i = 0; i <= 100; i += 10)
    {
        bar.SetValue(i); 
        System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(200));
    }
    Console.WriteLine("do " + DateTime.Now);

    return Task.CompletedTask;
}

However, the bar.SetValue(i) method does not seem to be functioning as expected. The progress bar does not reflect the current progress state during the job execution.

@meriturva
Copy link
Owner

IPerformContextAccessor works using an AsyncLocal : https://github.com/meriturva/Hangfire.PerformContextAccessor/blob/main/Hangfire.PerformContextAccessor/PerformContextAccessor.cs#L8

It works when the call flow is from the same async context.
I don't know about your flow but you could check the usage of the IPerformContextAccessor here: https://github.com/meriturva/NLog.HangfireJobLogsTarget/blob/main/NLog.HangfireJobLogsTarget/JobDecoratorLayoutRenderer.cs

where I correlate ILogger (nlog) with JobId from PerformContext. The call logger.LogInformation works in this case because it starts from the same async context.

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

No branches or pull requests

2 participants