7

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!

Chiggins
  • 8,197
  • 22
  • 56
  • 81
  • 2
    Awful solution (do not use it): Create an action in controller that do the job, then write a simple get in power-shell, run script in task schduler... But i do not recommend it, better solution is to use f.e Quartz.net that runs as f.e. as Windows Service. – pwas Jan 25 '16 at 21:23
  • Sql Server has tasks that you can schedule. I'd recommend doing it there. – Scottie Jan 25 '16 at 21:28
  • 1
    Where are you hosting your application. If you're using Azure Web Apps (I highly recommend this) you can easily attach a Web Job to your app that can be easily scheduled to run at a given interval – Jesse Carter Jan 25 '16 at 21:39
  • @JesseCarter We don't use Azure, we host our own servers. – Chiggins Jan 25 '16 at 21:41
  • @Chiggins Ahhh, that's a shame – Jesse Carter Jan 25 '16 at 22:08
  • 1
    Possible duplicate of [how to schedule a task in MVC4 C#?](https://stackoverflow.com/questions/14620195/how-to-schedule-a-task-in-mvc4-c) – Andreas Bergström Oct 23 '17 at 11:28

2 Answers2

11

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.

Barry
  • 1,084
  • 1
  • 8
  • 22
7

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.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • I agree: a web server job is receive http requests and return http reponses. If you want to use it as a job scheduler, you can find a sneaky way to do it, but... you would do it wrong. Use the right tool for the job. ASP.NET MVC (as everything running in a web server) is not. – Gian Paolo Jan 25 '16 at 21:42
  • Most of what I'm doing is wanting to check a column called finish_date and see if it's approaching a year. Once it's a week out from being a year old, email the owner and say that unless it's updated, it'll be deleted. Then on its "birthday", delete it. Not sure if your first suggestion with using a database would work with that, and I kind of would rather avoid using Windows Scheduler. – Chiggins Jan 25 '16 at 21:44
  • 1
    @Chiggins - You could probably implement all of that in a stored procedure. SQL Server can send email using the [sp_send_dbmail](https://msdn.microsoft.com/en-us/library/ms190307.aspx) sproc. The rest is just basic logic. Then use the agent to schedule that. – Mike Christensen Jan 25 '16 at 21:48