I'm looking at having a scheduled task (Console app) that will run every 20 minutes.
I've looked into using Quartz.Net for doing this, and it looks great but I can't see if it will work with my console application.
What I want is:
If I use only quartz.net then this console application will have to be open forever for my code to be run on schedule. Is there a way where I can have windows run my Quartz.Net scheduled task for me every 20 minutes?
So, Windows Task Schedular --20 mins--> run console app using Quartz.Net.
Or should I make a standard .NET Console App and just have it run the console app every 20 mins?
Cheers
Don't mix Task Scheduler with Quartz they are 2 different methods of doing things.
Normally Task Scheduler is configured at an interval to run Console Applications that just do the work and close.
The suggested way to use Quartz is to Embed it in in a windows service application.
Then install this service on the server and configure it to run always and automatic start (This should protect you from crashes and kills and ensure the application will be running always unlike a console application)
Related
how does Quartz.net work in the background. suppose i have created a job which would get triggered next year. so what's happening in the background for one year. . Is there a process or thread which keeps running continuously for one year.does it implement timer in the background?
You need to start the Quartz scheduler. As long as the scheduler is running, Quartz will keep track of schedules and start jobs according to their schedule. But as Quartz is a library hosted inside your application, you have to start the Quartz scheduler yourself.
In scenarios where you don't have an application that is always active (like a website), you have to find some way to let the Quartz scheduler run independent of that (for instance in a scheduler service dedicated to running your jobs). I've found a few references to using Quartz.net in ASP.Net projects, but I have no personal experience with running Quartz within a website. I don't know how Quartz would react to IIS shutting down or reusing the application pool your site is running in. But if your job can handle being aborted, it may be a viable option. You'd have to start the scheduler in the Application_Start event in your Global class.
No matter how you run Quartz, you will want to use a non-volatile job store. Quartz can store the scheduled jobs in memory, but you'd have to re-schedule all jobs after each application start. So it is highly recommended to use another job store that can persist jobs between application restarts.
The Quartz.net quick-start tutorial has bits of code to show you how to use Quartz in your application.
This is about ASP.NET MVC background. When I need to call action methods on timely basis, I use such 3rd part tools as HangFire, which runs under the application pool. I want to create a sync job, which reads a .csv file generated by our ERP system and update our custom DB.
But I know that having this sync job as an action method inside of my ASP.NET MVC is not the correct way of doing things, as background jobs should not be part of my web application.
So to handle my sync job I did the following :
Using Visual studio 2012 , I created a new console application and inside its main method I wrote the sync job as follow:-
class Program
{
static void Main(string[] args)
{
using (Entities sd = new Entities())
{
//code goes here
sd.SaveChanges();
}
}
}
How can I call this console application on timely basis? I am thinking on creating a new task using windows task scheduler, which would be executed each hour and call the application, mainly calls the .application file?
now my question is how i can call this console application on timely basis? . i am thinking on creating a new task using windows task scheduler , which will be executed each hour and call the application, mainly calls the .application file?
Running a program is one of the things the Windows Task Scheduler (TS) lives for. So yes, please do use the TS.
Additional benefits of using the TS are:
Able to specify the user account to run the process under
Multitude of ways to specify a trigger for when the task should run
Conditional mechanisms to control if the task should run (computer idle; power savings)
And a jolly nice history of the task when run
The alternative is creating you own scheduling service just to run your app but such a choice is frowned upon due to:
reinventing of the wheel
Windows gets flooded with an inordinate number of proprietary task scheduling services all essentially doing the same thing.
Background Jobs in Web Apps
Though one could arguably spawn/schedule a background thread/worker item in say ASP.NET to invoke some c# code, the danger here is that IIS is unaware that you have done so. It may decide to suspend or recycle your App Pool due to web inactivity and then your scheduled jobs would be sent to the void. Hence why it is not recommended.
Now you could always tell IIS not to timeout your App Pool so that it is not recycled periodically but I suspect that will lead to other issues.
Best not to do so in the first place.
However, there is nothing wrong in ASP.NET using the Task Scheduler API to schedule an operation to run say a console app.
Windows Task scheduler is a perfectly valid way to call a .net console application. I have several that I wrote and support that have been running for years with no issues.
A while ago I've wrote an application that crawlers data from several websites, parses the data into specific format and sends this data to specific email address.
The app was executed x times a day via windows task scheduler.
Now I want to deploy this app to appharbor (i am not sure if it's possible to deploy console or library applications).
How can I schedule method executions via code?
We're working on adding background tasks to AppHarbor (it will be in the form of running console applications that you push). If the task doesn't take too long (eg. less that ~90 seconds), doing it in a controller is a viable workaround. You can use a service like MomentApp to trigger your crawler.
I have a ASP.net web application that checks the status of my servers, it then wraps all this information up and puts it in a email. My Question how do I run this automatically say every day at like 2:00am, or like every 12 Hours?
Thanks
The best solution is to create a simple MS Windows Service which will do this job.
You'd better implement this as a separate process from your ASP.NET application. Phil Haack has summarized the reasons in this blog post. A Windows service for example or even a console application using the windows scheduler could work just fine for this task.
You want a scheduler - I recommend Quartz.NET.
As others have said, your code doesn't have to be in a web app.
If it is, then schedule a job that uses WebClient to make a request to your web app.
Check out WebDriver.
It's intended as a test / qa framework, but there's nothing stopping you from using it in a console application, which you can then run as a Scheduled Task. Note that whatever machine runs the Scheduled Task has to have a browser that WebDriver can fire up.
The easiest solution would be to create a scheduled task on your server using the Windows Task Scheduler and setup this job so that it uses internet explorer to visit your webpage.
If you open the task scheduler and create a new task. In the "Run" field put:
"C:\Program Files\Internet Explorer\IEXPLORE.EXE""http://www.yoursite.com/yourpage.aspx"
Then in the "Start in" field put:
"C:\Program Files\Internet Explorer"
Now configure this task to run at 2:00am every day.
I have windows task which restarts IIS at midnight 00:00. In my application there is a background thread which runs a global refresh at around 02:00.
My problem is that the application starts only on the first request from a browser. This may not occur for quite some time and the global refresh can be late in starting.
Is there any way to start the application without first browsing to the web application?
Ideally you should keep maintenance tasks such as this separate from your web application (either as a scheduled task or Windows service).
But, if you really need to do it this way could create a batch file that does:
iisreset /restart
"C:\Program Files\GnuWin32\bin\wget.exe" -O nul http://www.myapp.com/default.aspx
Then run this batch file as your scheduled task at 12:00. This will restart IIS and warm up your application.
You can get GNU wget.exe from:
WGET for Windows (SourceForge)
You can have another task that accesses your web site after IIS is restarted.
Still, I can't see why would you have a thread doing maintenance inside your IIS worker process. If the process dies from some reason (for example - because of the recycling configuration in the web site's application pool) the work won't get done. It's better to do this from a separate process, such as windows service or a scheduled windows task.
You shouldn't have any threads scheduled inside an IIS web application - becasue IIS has some logic to recycle the worker process and your application when it is not used. Its better to run it as a separate application (scheduled separately).
You could also use a Powershell script called by task manager. Here is simple six-line script we use to "warm up" SharePoint servers.
You could repurpose or find a similar script for a basic .NET application.