Get the parent job by the ID of the current job

Hello everyone,

is there a way to get the parent job by the ID of the current job? E.g. I have the job with the ID 123. Now I would like to increase the expiretime of the parent job to a few days/weeks. Is there a way to get the parent job (commonly with the ID 122 in this case) and the object of this job in Visual Basic/C#?

Thanks and regards,
Marvin Klar

Hi Marvin, in my usage of the tool, I get jobId like this:

if you make the method Signature take a ‘PerformContext’ you can pass it in the enqueue method

public void BackgroundTaskExample(int primaryKeyExample, PerformContext context){
    DoSomeWork(primaryKeyExample);
    var jobId = context.BackgroundJob.Id;
}

and when enqueueing:

BackgroundJob.Enqueue(() => BackgroundTaskExample(101, null));

the hangfire system will handle the insertion of PerformContext object properly

1 Like

Hello Paul,

thank you very much for your answer. Does this context.BackgroundJob.Id; get the ID of the PARENT job, or the ID of the current job?

Bump, bump, bump,bump, bump

The PerformContext contains the current job id. If you like to get the parental job id, you should provide this value as an argument to your continuation method. More details can be found in this answer: Get the (ID of the) parent job by the (ID of the) current job - #2 by Oliver