Search code examples
c#asp.net-mvcscheduled-tasksschedulerhangfire

How do I trigger a Hangfire job manually?


The idea is, on a loading page, there is an email input field. Once the user writes his email and clicks the Get Email button, a background job should trigger. It will check if the transaction is complete, and once it is, it will send an email to the user. All the necessary code regarding the email service (I used mailgun) is already written, but I am completely lost on how to implement the scheduler.

How should I go about achieving this?


Solution

  • You can just use Enqueue to trigger job manually

    BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));
    

    @EDIT

    To run second job, if first was succesfull you can use ContinueWith()

    var id = BackgroundJob.Enqueue<ITask1>(x => Console.WriteLine("First task")); 
    if (id != null) 
    { 
        BackgroundJob.ContinueWith<ITask2>(id, x => Console.WriteLine("Second task")); 
    }