简体   繁体   中英

how to create cron job in asp.net(v4.0) to run method every 30mins

  1. I need some guidance on creating and running a Cron Job in asp.net(C#.net) to run every 30 minutes.i have created a class in that i have written code for getting tweets, facebook feeds.

  2. i have created another page in that i have one button to download tweets and save in database. If i want to get tweets i have to click on sync button every time.

  3. i want to create cron job so that the database will get automatically synchronized with new tweets,facebook feeds.

Thanks

You can follow any one of the following approaches

  1. Create a console app with the logic to fetch the tweets and feeds, and use a Task scheduler to run it for every 30 mins.

  2. You could build a windows service, which polls the feeds within a timer and updates the db.

  3. You could checkout this scheduler which is a rough equivalent to cron jobs. Personally haven't tried it. Check out this SO

If your intended 30-minute scheduled task is meant to be a discrete transactional action (like, for instance, your example of synchronizing some database values), then you may want to take a look at the Revalee open source project.

You can use it to schedule web callbacks at specific times. In your case, you could schedule a web callback (30 minutes in the future). When your ASP.NET application receives the callback, it can schedule the next 30 minute callback as well as perform whatever tasks you need it to handle every half-hour. When your ASP.NET application launches for the very first time, then you would schedule the first web callback. Since your web application is being called back, you do not need to worry about your web application unloading (which it will do periodically on IIS, for example).

For example using Revalee, you might do the following:

  1. Register a future (30 minutes from now) callback when your application launches via the ScheduleThirtyMinuteCallback() method (see below).

     private DateTimeOffet? previousCallbackTime = null; private void ScheduleThirtyMinuteCallback() { // Schedule your callback 30 minutes from now DateTimeOffset callbackTime = DateTimeOffset.Now.AddMinutes(30.0); // Your web service's Uri, including any query string parameters your app might need Uri callbackUrl = new Uri("http://yourwebapp.com/Callback.aspx"); // Register the callback request with the Revalee service RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl); previousCallbackTime = callbackTime; } 
  2. When the web scheduled task activates and calls your application back, you perform whatever action you need to do every 30 minutes and you schedule the next callback too. You do this by adding the following method call ( CallbackMonitor() ) to your Callback.aspx page handler.

     private void CallbackMonitor() { if (!previousCallbackTime.HasValue || previousCallbackTime.Value <= DateTimeOffset.Now.AddMinutes(-30.0)) { // Perform your "30 minutes have elapsed"-related tasks // ...do your work here... // Schedule subsequent 30 minute callback ScheduleThirtyMinuteCallback(); } } 

To be clear, the Revalee Service is not an external 3rd party online scheduler service, but instead a Windows Service that you install and fully control on your own network. It resides and runs on a server of your own choosing, most likely your web server (but this is not a requirement), where it can receive callback registration requests from your ASP.NET application.

If, however, your 'run every 30 minutes' task is a long running task, then you probably do not want to embed that functionality within your ASP.NET application.

I hope this helps.

I was one of the developers involved with the Revalee project. 我是参与Revalee项目的开发人员之一。 To be clear, however, Revalee is free, open source software. The source code is available on GitHub .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM