Task is not called

Asked

Viewed 150 times

3

Guys I have an api where your processing takes around 6 hours of execution, so I added a Task.Factory.Startnew with the main processing. So when someone calls, she responds to Statuscode 200 and continues processing "heavy" with the Task:

public override async Task<HttpResponseMessage> Processar(TarefaViewModel tarefa){
        var task = Task.Factory.StartNew(() =>
        {                
            var result = ProcessamentoDe6HorasNoBanco();

            // Chama HttpClient e responde para outra api externa:
            RequestPostStatusGerenciador(result, tarefa.Id, Action.Processar); 

        }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

        return await ResponderAccepted("OK");
    }

However, back and forth the Task is not called I have to recycle the IIS to get back to normal. I suspect it is something related to the IIS Cache. Can anyone shed any light on that? In the properties of Task.Factory.Startnew, such as Taskcreationoptions.Longrunning would make a difference?

1 answer

3


I believe that the best option for you would be to integrate the Hangfire to your project.

First install the nuget package

PM> Install-Package Hangfire

In your class Startup add to the method Configuration

GlobalConfiguration.Configuration.UseSqlServerStorage("<connection string or its name>");

app.UseHangfireDashboard();
app.UseHangfireServer();

To view the executed Jobs, you can access the address http://localhost:port/hangfire

inserir a descrição da imagem aqui

To add a Job to Hangfire

BackgroundJob.Enqueue(() => ContabilidadeJobs.GerarLancamentos())

The method is as follows:

[AutomaticRetry(Attempts = 0)]
[LogFailure]
public static async Task GerarLancamentos()
{
    //Processamento pesado
}

I believe that with the use of Hangfire, you will have a better follow-up of the execution of the executed Jobs, including if there have been failures of execution. inserir a descrição da imagem aqui

In my github, I have a project that used the Hangfire.

  • How you use Hangfire with Usememorystorage to run a Schedule every 30 seconds ?

  • 1

    I don’t use Hangfire with data in memory, the smallest unit that hangfire implements from interval is minute. I gave a search and all the places I found no one could implement task to execute in the unit of seconds

  • Nor with TimeSpan.FromMinutes(1) works. I no longer know what to do. Soh runs 1x when starting, does not trigger the Schedule in the given time

  • 1

    take a look at my project https://github.com/pablotdv/CodingCraftEX06HangFire, I use hangfire

Browser other questions tagged

You are not signed in. Login or sign up in order to post.