简体   繁体   English

如何在asp.net(v4.0)中创建cron作业以每30分钟运行一次方法

[英]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. 我需要一些有关在asp.net(C#.net)中创建和运行Cron作业以每30分钟运行一次的指南。我创建了一个类,该类中我编写了用于获取tweet和facebook feed的代码。

  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. 我想创建cron作业,以便数据库将自动与新的tweets,facebook feed同步。

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. 创建一个具有逻辑的控制台应用程序,以获取推文和提要,并使用任务计划程序每30分钟运行一次。

  2. You could build a windows service, which polls the feeds within a timer and updates the db. 您可以构建Windows服务,该服务在计时器内轮询提要并更新数据库。

  3. You could checkout this scheduler which is a rough equivalent to cron jobs. 您可以检出调度程序,这大致相当于cron作业。 Personally haven't tried it. 我个人还没有尝试过。 Check out this SO 看看这个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. 如果您打算按计划的30分钟计划任务是一项离散的事务操作(例如,例如,您同步某些数据库值的示例),那么您可能想看看Revalee开源项目。

You can use it to schedule web callbacks at specific times. 您可以使用它在特定时间安排Web回调。 In your case, you could schedule a web callback (30 minutes in the future). 根据您的情况,您可以安排网络回调(未来30分钟)。 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. 当您的ASP.NET应用程序收到回调时,它可以安排接下来的30分钟回调,以及每半小时执行一次您需要处理的任务。 When your ASP.NET application launches for the very first time, then you would schedule the first web callback. 当您的ASP.NET应用程序首次启动时,您将安排第一个Web回调。 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). 由于您的Web应用程序已被回调,因此您无需担心Web应用程序的卸载(例如,它将在IIS上定期执行)。

For example using Revalee, you might do the following: 例如,使用Revalee,您可以执行以下操作:

  1. Register a future (30 minutes from now) callback when your application launches via the ScheduleThirtyMinuteCallback() method (see below). 通过ScheduleThirtyMinuteCallback()方法启动应用程序时,注册将来(从现在开始30分钟)回调。

     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. 当Web计划的任务激活并回叫您的应用程序时,您每30分钟执行一次需要执行的操作, 并且还计划下一个回调。 You do this by adding the following method call ( CallbackMonitor() ) to your Callback.aspx page handler. 您可以通过将以下方法调用( CallbackMonitor() )添加到Callback.aspx页面处理程序中来实现。

     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. 需要明确的是,Revalee服务不是外部的第三方在线调度程序服务,而是您在自己的网络上安装完全控制的Windows服务。 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. 它驻留并运行在您自己选择的服务器上,很可能是您的Web服务器(但这不是必需的),在该服务器上它可以从ASP.NET应用程序接收回调注册请求。

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. 但是,如果“每30分钟运行一次”任务是一项长期运行的任务,则您可能不想将该功能嵌入到ASP.NET应用程序中。

I hope this helps. 我希望这有帮助。

Disclaimer: I was one of the developers involved with the Revalee project. 免责声明:我是参与Revalee项目的开发人员之一。 To be clear, however, Revalee is free, open source software. 不过要明确一点,Revalee是免费的开源软件。 The source code is available on GitHub . 源代码可在GitHub上获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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