Is there a static hangfire context getter, similar to HttpContext.Current
Asked Answered
A

2

6

I have an existing api that stores data per-thread and is retrieved using HttpContext.Current.

I'm trying to refactor this class to be called from a hangfire job -- I want to know if there is an equivalent static method for retrieving the hangfire execution context.

If not, I would also like to know if there is a 1:1 relationship between hangfire jobs and threads. I couldn't find any documentation about the lifetime of a hangfire job -- ie threadstart -> job start -> job end -> thread dispose, or if 1 thread could process multiple jobs simultaneously, ie threadstart -> job1 start, job2 start, job3 start, job1 end, job4 start,job2 end, job1 end, job3 end -> thread dispose

Ambulant answered 29/1, 2016 at 22:14 Comment(0)
A
2

Came across this looking for something else, the newer way (works with 1.6.20, not sure how far back it works) of doing it is to have a parameter of type Server.PerformContext in the method your expression calls and hangfire will set it automatically (like the cancellation token for site shutdowns)

if you forgive the VB code i have this sigature for the job method

        <DisplayName("{0}")> 'use jobname param as the name of the job
        Sub RunJob(jobName As String, configID as Integer  hfContext As Server.PerformContext, cancellationToken As Hangfire.IJobCancellationToken)

I create the job with

        Dim jobExpression As Linq.Expressions.Expression(Of Action(Of HangfireJob)) = Sub(x) x.RunJob(opt.JobName, opt.configID, Nothing, JobCancellationToken.Null)
        RecurringJob.AddOrUpdate(Of HangfireJob)(opt.SchedulerSystemJobID, jobExpression, opt.RecurringCronSchedule, tzinfo)

And in the RunJob method to get the ID i use

hfContext.BackgroundJob.Id
Armstead answered 12/12, 2018 at 23:58 Comment(2)
I have some legacy apps in VB! I enjoy working on them...It was the very first language and project for me!. vb.net, winforms and an access 2003 database ;)Ambulant
quickbasic was my first language, then VB, whilst i can write C#, i find it more cryptic, though i wish MS wouldn't castrate us VBers so much.Armstead
A
7

From - https://discuss.hangfire.io/t/how-to-get-jobid-within-job/851/4

a [ThreadStatic] variable will do the trick in a ServerFilter

public class JobContext : IServerFilter
{
    [ThreadStatic]
    private static string _jobId;

    public static string JobId { get { return _jobId; } set { _jobId = value; } }

    public void OnPerforming(PerformingContext context)
    {
        JobId = context.BackgroundJobId;
    }
}

// And register it
GlobalConfiguration.Configuration.UseFilter(new JobContext());
Ambulant answered 1/2, 2016 at 18:2 Comment(3)
I have updated this code to replace the deprecated JobId with BackgroundJobId per discuss.hangfire.io/t/use-hangfire-job-id-in-the-code/2621/2Scruggs
I have not tested this in async scenarios. If your jobs are heavily task based this may not work properly.Ambulant
Update, 2 years later. Here be dragons, yadayadayada. This should probably be revisited in terms of async/await/tasks and AsyncLocal<T>. if your code is heavily async, this code may not work for you. msdn.microsoft.com/en-us/library/dn906268(v=vs.110).aspxAmbulant
A
2

Came across this looking for something else, the newer way (works with 1.6.20, not sure how far back it works) of doing it is to have a parameter of type Server.PerformContext in the method your expression calls and hangfire will set it automatically (like the cancellation token for site shutdowns)

if you forgive the VB code i have this sigature for the job method

        <DisplayName("{0}")> 'use jobname param as the name of the job
        Sub RunJob(jobName As String, configID as Integer  hfContext As Server.PerformContext, cancellationToken As Hangfire.IJobCancellationToken)

I create the job with

        Dim jobExpression As Linq.Expressions.Expression(Of Action(Of HangfireJob)) = Sub(x) x.RunJob(opt.JobName, opt.configID, Nothing, JobCancellationToken.Null)
        RecurringJob.AddOrUpdate(Of HangfireJob)(opt.SchedulerSystemJobID, jobExpression, opt.RecurringCronSchedule, tzinfo)

And in the RunJob method to get the ID i use

hfContext.BackgroundJob.Id
Armstead answered 12/12, 2018 at 23:58 Comment(2)
I have some legacy apps in VB! I enjoy working on them...It was the very first language and project for me!. vb.net, winforms and an access 2003 database ;)Ambulant
quickbasic was my first language, then VB, whilst i can write C#, i find it more cryptic, though i wish MS wouldn't castrate us VBers so much.Armstead

© 2022 - 2024 — McMap. All rights reserved.