Pete
Pete

Reputation: 58422

Web API 2 return OK response but continue processing in the background

I have create an mvc web api 2 webhook for shopify:

public class ShopifyController : ApiController
{
    // PUT: api/Afilliate/SaveOrder
    [ResponseType(typeof(string))]
    public IHttpActionResult WebHook(ShopifyOrder order)
    {
        // need to return 202 response otherwise webhook is deleted
        return Ok(ProcessOrder(order));
    }
}

Where ProcessOrder loops through the order and saves the details to our internal database.

However if the process takes too long then the webhook calls the api again as it thinks it has failed. Is there any way to return the ok response first but then do the processing after?

Kind of like when you return a redirect in an mvc controller and have the option of continuing with processing the rest of the action after the redirect.

Please note that I will always need to return the ok response as Shopify in all it's wisdom has decided to delete the webhook if it fails 19 times (and processing too long is counted as a failure)

Upvotes: 15

Views: 29970

Answers (4)

HansElsen
HansElsen

Reputation: 1753

I used Response.CompleteAsync(); like below. I also added a neat middleware and attribute to indicate no post-request processing.

[SkipMiddlewareAfterwards]
[HttpPost]
[Route("/test")]
public async Task Test()
{
    /*
       let them know you've 202 (Accepted) the request 
       instead of 200 (Ok), because you don't know that yet.
    */
    HttpContext.Response.StatusCode = 202; 
    await HttpContext.Response.CompleteAsync();
    await SomeExpensiveMethod();
    //Don't return, because default middleware will kick in. (e.g. error page middleware)
}

public class SkipMiddlewareAfterwards : ActionFilterAttribute
{
    //ILB
}

public class SomeMiddleware 
{
    private readonly RequestDelegate next;

    public SomeMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        await next(context);
        if (context.Features.Get<IEndpointFeature>().Endpoint.Metadata
            .Any(m => m is SkipMiddlewareAfterwards)) return;
        //post-request actions here
    }
}

Upvotes: 2

Dan Leonard
Dan Leonard

Reputation: 169

Task.Run(() => ImportantThing() is not an appropriate solution, as it exposes you to a number of potential problems, some of which have already been explained above. Imo, the most nefarious of these are probably unhandled exceptions on the worker process that can actually straight up kill your worker process with no trace of the error outside of event logs or something at captured at the OS, if that's even available. Not good.

There are many more appropriate ways to handle this scenarion, like a handoff a service bus or implementing a HostedService.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio

Upvotes: 1

Serj Sagan
Serj Sagan

Reputation: 30208

There are a few options to accomplish this:

  1. Let a task runner like Hangfire or Quartz run the actual processing, where your web request just kicks off the task.
  2. Use queues, like RabbitMQ, to run the actual process, and the web request just adds a message to the queue... be careful this one is probably the best but can require some significant know-how to setup.
  3. Though maybe not exactly applicable to your specific situation as you are having another process wait for the request to return... but if you did not, you could use Javascript AJAX kick off the process in the background and maybe you can turn retry off on that request... still that keeps the request going in the background so maybe not exactly your cup of tea.

Upvotes: 6

Pete
Pete

Reputation: 58422

I have managed to solve my problem by running the processing asynchronously by using Task:

    // PUT: api/Afilliate/SaveOrder
    public IHttpActionResult WebHook(ShopifyOrder order)
    {
        // this should process the order asynchronously
        var tasks = new[]
        {
            Task.Run(() => ProcessOrder(order))
        };

        // without the await here, this should be hit before the order processing is complete
        return Ok("ok");
    }

Upvotes: 17

Related Questions