Task is not called

3

Personal I have an api where its processing takes around 6 hours of execution, so I added a Task.Factory.StartNew with the main processing. So when someone calls, it responds to StatusCode 200 and continues "heavy" processing 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, sometimes the Task is not called I have to recycle IIS to go back to normal. I suspect it is something related to IIS Cache. Can anyone give me a light on this issue? Would the Task.Factory.StartNew properties such as TaskCreationOptions.LongRunning make any difference?

    
asked by anonymous 27.11.2017 / 19:36

1 answer

3

I think the best option for you would be to integrate HangFire into your project.

First install the nuget package

PM> Install-Package Hangfire

In your class Startup add method Configuration

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

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

To view the jobs you have, you can access the link

ToaddaJobtoHangFire

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

Themethodlookslikethis

[AutomaticRetry(Attempts=0)][LogFailure]publicstaticasyncTaskGerarLancamentos(){//Processamentopesado}

IbelievethatbyusingHangFire,youwillhaveabetterfollow-upoftheexecutionoftheexecutedJobs,eveniftherewereexecutionfailures.

Inmy github , I have a project that I used HangFire .

    
27.11.2017 / 19:52