3

I'm trying to find the way to keep the database updated, but the method which does it consumes a lot of time so I try to create a background task to do it.

I searched for solutions and I read this article of different options to run background processes: https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx

But I don't know what's is the best solution out of those, like I'm trying to execute it outside the application. I found something about creating a Windows Service too, but I don't know how, I didn't manage to find some good examples.

What is the best way to keep my database updated everytime I access the application without losing the time it consumes? If you can help me to see the light I would appreciate that so much.

Jose M Martin
  • 373
  • 1
  • 3
  • 19
  • 1
    I use the hangfire that allows me to trigger any method based on timer. That is free and worked for me. I hope it helps you – Canela Apr 28 '17 at 07:23
  • @Canela I looked at it, but the problem is that I'm using a MySQL Database :/ I will try it with a provider. – Jose M Martin Apr 28 '17 at 08:05
  • You are right, I am not sure if hangfire can use a different provider. But what about if you use a SQL express that is free just for the hangfire? – Canela Apr 28 '17 at 08:08
  • @Canela I found this: https://github.com/arnoldasgudas/Hangfire.MySqlStorage And it looks pretty easy to use. Thanks man! – Jose M Martin Apr 28 '17 at 08:09
  • You are Welcome mate!!! Good luck – Canela Apr 29 '17 at 13:26

2 Answers2

2

I'm really happy with FluentScheduler, which is handling all my mission-critical scheduling. As well as firing jobs on a scheduled basis, it can also do them on demand, like so:

// Define your job in its own class

public abstract class MyJob : IJob
{
    public void Execute()
    {
        // Do stuff here...
    }
}

// Schedule your job at startup

var runAt = DateTime.Today.AddHours(1); // 1am
if (runAt<DateTime.Now)
    runAt = runAt.AddDays(1);

Schedule<MyJob>()
    .WithName("My Job Name") // Job name, required for manually triggering
    .NonReentrant() // Only allow one instance to run at a time
    .ToRunOnceAt(runAt) // First execution date/time
    .AndEvery(1).Days().At(runAt.Hour, runAt.Minute); // Run every day at the same time

// To manually trigger your job

ScheduledJobRegistry.RunTaskAsync("My Job Name");

I have the scheduled jobs running in a windows Service and use SignalR as a means of triggering them remotely from an MVC Web App when required.

Pete
  • 1,807
  • 1
  • 16
  • 32
-1

You can use an async method. Just use a void instead of Task.

public async void LongRunningMethod () {
    ...
    // Insert long running code here
    ...
}

Then call it and it will execute in the background. Be aware that you can have hidden exceptions without proper were handling.

You can also use Hangfire which is a pretty awesome background task scheduler

Here is an example of using Hangfire to run a daily task

RecurringJob.AddOrUpdate(() => Console.Write("Easy!"), Cron.Daily);
Austin Winstanley
  • 1,339
  • 9
  • 19
  • The problem for this is that the process is called only when someone access to the application, no? I would like to do it outside the application and have it already updated when someone access. – Jose M Martin Apr 28 '17 at 07:33
  • 1
    Then use Hangfire. It has scheduling so you can run tasks whenever you want. And it is easy to use, even including a built in Dashboard. Hangfire is what you are looking for I think – Austin Winstanley Apr 28 '17 at 07:35
  • I will take a look at it. Thanks! – Jose M Martin Apr 28 '17 at 07:36