How to capture the creator of a job?

Hi Sergey,

Thanks for bring this awesome project to us, I really like its simplicity. however, can I know if there is a way to capture the creator of a specific job? as i found only the “createdAt” in the Job table. Coz i am thinking to implement a mechanism to notify the job creator upon their job’s completion.

Thanks in advanced for your help!

Alex

Great idea, i think you could do that manually in the job, by checking if job is successful, you just grab the Current user from the HTTPContext and do the rest of your logic.

But i think it is also a great idea for an Attribute, you can check here for how to extend Hangfire

but either way, I think we should be able to grab the Current User wither we are using Old ASP.NET Identity or the new Owin Identity.

Hello, @yinkinkwok and thanks for the good words!

Coz i am thinking to implement a mechanism to notify the job creator upon their job’s completion.

Just include the notification into the job itself. You can look at HangFire.Highlighter sample.

Static method:

public void TargetMethod(int entityId)
{
    // Processing
    JobCreator.Notify("completed", entityId);
}

Instance method:

public void TargetMethod(int entityId)
{
    // Processing
    var creator = new JobCreator();
    creator.Notify("completed", entityId);
}

Constructing JobCreator in the ctor:

public class MyService
{
    private JobCreator _jobCreator = new JobCreator();

    public void TargetMethod(int entityId)
    {
        // Processing
        _jobCreator.Notify("completed", entityId);
    }
}

Passing a dependency through constructor this requires integration with IoC container (see here), because HangFire should create an instance of the MyService class before calling the target method.

public class MyService
{
    private JobCreator _jobCreator;

    public MyService(JobCreator jobCreator)
    {
        _jobCreator = jobCreator;
    }

    public void TargetMethod(int entityId)
    {
        // Processing
        _jobCreator.Notify("completed", entityId);
    }
}

Hi Sergey,

Thanks very much for your thorough explanation!

Cheers,

Alex