How to change job id?

This is 2nd day i’m using Hangfire for a POC. I want to know,

  1. can we change the jobid (eg. #1268) to a custom string like 200_19000_89 so that we can clearly spot them on the dashboard in event of re-trying or failure.

I had the similar requirement and I created a parameter in the job that I can view in Dashboard

public class JobCreateAttribute : JobFilterAttribute, IClientFilter {
public void OnCreated(CreatedContext filterContext)
{

    }

    public void OnCreating(CreatingContext filterContext)
    {                      
        filterContext.SetJobParameter("BackgroundJobID", "custom string");
    }

}

1 Like

You can’t alter the job ID as it is defined by the storage engine. For example, in SQL Server it is automatically generated as an identity:

1 Like

I just change the code. Result shows as attachment.

  1. I want to change “Job ID” not “BackgroundJobID”
    Or
  2. How to change this “Customer string” BackgroundJobID dynamically.

Thanks.

Each job in Hangfire has a set of parameters. You can form a custom string based on some business logic just before assigning the value of parameter.

In my case, I am assigning the value of custom string from the second argument of the method that the job will invoked on processing the job.

    public void OnCreating(CreatingContext filterContext)
    {
        if (filterContext.Job == null || filterContext.Job.Args == null || !filterContext.Job.Args.Any())
            return;

        var backgroundJobID = (long) filterContext.Job.Args[1];
        filterContext.SetJobParameter("BackgroundJobID", backgroundJobID);
   }

My method look like this

void Process(string param1, long backgroundJobID)
{

}

and is enqueued like this:

string stringParam1 = “test”;
long backgroundJobID = CreateNewBackgroundJobID();
Hangfire.BackgroundJob.Enqueue(() => backgroundJobHandler.Process(stringParam1, backgroundJobID));

1 Like

@dmehta Thanks for your reply. I got an idea.

Hello,
I am also looking to provide each job unique custom name.
I tried above code whilst it is adding a new field to the queue list page e.g CurrentCulture, CurrentUICulture, BackgroundJobId

Thanks
Archana