Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a scheduled task in an ASP.NET MVC website

I'm in the process of working out the architecture for a website and I've come across something I've never done before with ASP.NET. What I'd like to do is every night run a query against entities in a database table to check a date column and perform either X or Y action, depending on what the date is. Basically I want a function to be called once a night. Everything that I've searched for has lead me in a few different directions that seem either extremely complex for such a simple action, or are leaned more toward job queuing which isn't exactly what I want.

Any suggestions are welcome, just looking for the simplest approach. Thanks!

like image 208
Chiggins Avatar asked Jan 25 '16 21:01

Chiggins


People also ask

How do I run a scheduled task?

Regardless of the Windows version or edition you have, you can also use the Run window to launch the Task Scheduler. Press the Windows + R keys on your keyboard to open Run, and then type taskschd. msc in the Open field. Finally, click or tap on OK, or press Enter on your keyboard.

What is MVC scheduler?

The ASP.NET MVC Scheduler, or event calendar, is a fully featured event calendar component that helps users manage their time efficiently. It facilitates easy resource scheduling and the rescheduling of events or appointments through editor pop-ups, drag and drop, and resizing actions.


2 Answers

I've used Hangfire on a project before for doing stuff like this. Its absolutely awesome and keeps all of your code in one solution (no messing around with windows services), I highly recommend.

like image 67
Barry Avatar answered Sep 18 '22 15:09

Barry


I can think of three different options.

  1. If the work you're doing is purely database related, most database platforms such as Microsoft SQL Server, Oracle, etc have the ability to schedule jobs that run at regular intervals. These jobs could be SQL statements or stored procedures. MS SQL Server would also let you call C# code from within a stored procedure, which would be handy.

  2. Use the operating system to schedule. Windows has Windows Scheduler jobs (The AT command) and Unix has cron jobs. You could schedule an executable to run every night, which would run C# code or SQL code.

  3. Write your own scheduling service. This service would run all the time as a Windows Service, and execute some code at a regular interval. Check out scheduling frameworks such as Quartz.net, which can help organize some of the scheduling details.

like image 28
Mike Christensen Avatar answered Sep 16 '22 15:09

Mike Christensen