I need to write a windows service which performs several operations as follows:
Fetching data from api and dump it to db - to be performed every hour
Consolidation - to be performed every 6 hours
Summary - to be performed once a day at 01:00
I don't want to use multiple timers or no timer at all. Please suggest best way to schedule these tasks.
There are multiple ways to do this. If you insist on using a windows service, you could look into Hangfire. The cost for using this is that you'll need to add a database that can store the information about the jobs.
Check the following link for Hangfire: https://www.hangfire.io/
You can also use the native Windows Scheduler for scheduling jobs.
Instead of creating a windows service you'll create a console app or something similar and use the Windows Scheduler for when it should execute.
If you don't want to use the native functionality I recommend using a nuget package like this one; https://www.quartz-scheduler.net/
All the above was found with a little bit of google magic and knowing the windows operating system..
Related
I have a C# console application. I would like to run that every hour of the day. This application is not intended to run on my local machine but on other users machine as a background process. I don't want the user to take the trouble of setting a task manually using task scheduler. How can I achieve the same for him ?
You can set it up to run on a timer with an interval of 1 hour.
See this link for examples
How do you add a timer to a C# console application
You could also install it as a windows service on the user's machine.
You can also use the Quartz.net scheduling service; in its simplest form it would install as a Windows Service and the schedule is configured in an XML file. You can then have it call your code at the scheduled time.
I kept some notes... not sure if this helps:
The Quartz.Net Guide I wish I had
Quartz.Net is very full featured, as far as I can read. If you use a scheduler so that you can defer tasks, or decouple a front-end from some rather long back-end processing, it can be done quite easily.
The large confusion for me was that Quartz can be used in many ways. You can embed it in your own app or service, etc. etc. The other confusion was that the official Tutorial could use some updating so that the code samples can work with the current version. Finally, the configuration seems pretty foreign for a .net user; I'm used to doing something like:
var myObject = new SuperUsefulClass();
myObject.ImportantSetting = SettingEnumeration.SomeValueThatIntellisenseSuggests;
myObject.OtherSetting = OtherSettingEnumeration.SomeValueThatIntellisenseSuggests;
Usually I like to explore a new class in LINQPad. I'll instantiate it as above, and play around with it until I am comfortable with using it in my project.
The configuration of Quartz.Net was about as foreign to me as imaginable. But that is because I overlooked one of the best pre-built options for using Quartz.Net, and I overlooked one of the best tutorials on Quartz.
For my use case, deferring tasks, I eventually realized the easiest way to use Quartz was with the pre-built Windows Service. To do that I simply modified an example configuration file, and I chose to persist all the jobs, job metadata, etc. to a SQL Server Database.
When it comes time for the job to be executed, the Quartz Windows Service will call the designated method (which of course implements the IJob interface). In order for the method to be called, your .dll or .exe ("assembly" in .net parlance) must be in the same folder where the service (Quartz.Service.exe) was installed. It then magically calls the method in your assembly. Of course if your method reads external files, those must also be copied to the service folder, or reachable somehow by the assembly.
After nearly giving up, I ran across Tarun Arora's tutorial. It is really excellent and since I cannot improve on it, I'll just link to it.
Install Quartz.Net as a windows service and Test installation
Quartz.Net Writing your first Hello World Job
Basically:
Tweak the quartz.config file (similar to the code above)
Install the Quartz.Net service (Quartz.Service.exe) as Administrator
Put assemblies (.exe or .dll) of code you want to execute, in the same folder as Quartz.Service.exe
Start the service
Make scheduling calls. i.e. your assembly code will make calls to the Quartz Service, which then persists and executes them on its
local system.
Create a service application: http://msdn.microsoft.com/en-us/library/zt39148a.aspx
I’m loath to ask another scheduler question here, I’ve read through dozens, but it’s still not clear to me what tools would best fit my need. I have three requirements for a reporting app:
User invoked
fixed scheduled
user scheduled.
I have an ASP.NET forms app to cover #1 and a C# console app to handle #2 but now #3 has been added to the mix.
So for the user scheduled reports I need to:
Present the user with a schedule selector and save their selection (into SQL Server?)
Have an app that checks the database for jobs to run/schedule
App to run the query and format the report
I suppose the latter two could be a single app but I’ve read it’s hard to debug service apps so keeping them separate may be good. I don’t know what parts of my requirements are met by Quartz.net and I’ve seen separate GUI tools (DayPilot) and backend (Task Manager API, CodePlex taskscheduler) mentioned. Not having used any of these I’m hoping to minimize my false starts.
If you require job scheduler try using hangfire.io
If you have SQL Server, then you should use SQL Server Reporting Services, which does all three.
I currently have a Windows Service which continually runs throughout the day. It has multiple threads which kick off:
tasks daily to update cache
tasks weekly to do cleanup
24/7 task to import XML into SQL Server
tasks which run for around 12 hours per day kicking off a console application to manage ETL
The tasks are not the important part of this question but it gives you the idea that this Windows service has grown to be a monster. It manages the imports of somewhere in the region of 300 million records per day.
It is hectic, but it works.
This iteration of development is giving me a chance to review the service and possibly break it down so that it is more manageable. It is thought that this could be multiple services with one manager service - ideal if a component needs to be updated then the whole thing does not need to grind to a hault.
Does anyone have any experience with this? I am keen to hear about your approach because it is new territory for me.
I have read this SO post which touches on the topic.
Cheers.
Your description sounds a lot like the thing I wrote about 2 years ago. A windows service which hosts addins and runs them in multithreaded environment. The architecture used is the .NET addin pipeline introduced in .NET 3.5 (System.AddIn-namespace). It works like a charm as I also integrated live/hot updates. It's so easy to implement new addins and just plug them in whenever I like. So I really recommend using this addin stuff.
See http://msdn.microsoft.com/en-us/library/cc175292.aspx for a quickstart.
I've done something similar for our background services, there's basically a ServiceHost and "Servlets" which are loaded via appdomains so they don't impact each other.
Why a service? Pretty much none of the things you do- except the 24/7 task for imports - are something I would do as a service.
I would use:
Either command line programs that get regularly scheduled, especially on the daily / weekly tasks
Or use SSIS and SQL Scheduler to schedule something.
The only thing that may justify a service is the 24/7 XML import - unless you can get away starting it, for example, every 5 minutes.
If I were to run some code, perhaps send an email, every 30 days to users of my site, how would that be done?
Use Windows Task Scheduler to run your application.
Options:
1) Console app that runs in Windows Task Scheduler
2) Windows Service
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx
Which framework are you working with? I have some pointers if you're developing with .Net 4.0 like I am.
If you have access to your machine where you can install services I would utilize the new and improved Workflow Service for this situation. What's nice about them is that Workflows can persists for minutes, hours, days, weeks, months, etc. lying "dormant" until delays time periods finish.
If it's an IIS machine (and if that's the route you would like to take) it is relatively simple to build it straight as a "Workflow Service" project/solution in VS 2010. From there you are presented with a designer and several workflow activities in the toolbar.
Add a flowchart activity to place your email activities inside of. Flowchart is ideal because it can initialize whatever you need to and decision flow can redirect backwards in direction (as opposed to the always forward moving sequence activity). What you need to do with your logic is up to you from there since your question doesn't provide a lot of details.
Now if you're using .Net 3.5 then I would think about refraining from building Workflows since you have to migrate when switching to 4.0 (WF 3.5 is NOT compatible with 4.0). With admin access to the machine you can install a Windows Service that contains a timer to fire code every 30 days (or however defined) as needed.
If it's SQL Server specific, you can use SQL Jobs.
If you want to do it with your ASP.NET app instead of a separate app, you have some options:
Since you want it to run every 30 days instead of daily, I recommend this method of using a schedule table instead of application variables or cache to schedule jobs:
You can setup a schedule table in your db and check when it was last updated in your global.asax. On the Session start, check if the current date is more than 30 days. If it is, then call a method to send out emails.
If you send out emails, then update your schedule table with the current date.
I'm making a small application that is supposed to download info from the web every day at 2am. It will download the information and write the strings to an XML file of my choosing.
Using .NET and C#.
My initial approach was to install a service on the users computer and have that run, but I'm not so sure. I've not even used it so much in the past, only once.
Which is the best (read: time tested :P ) approach to this very common problem.
You can either build your application as a Windows Service, as you mentioned.
Or else it would probably be a better idea to create a normal console application, and launch it automatically at 2.00am with the Windows Task Scheduler.
You can consider both methods as popular and "time-tested".
I would suggest having a console app, which calls data fetching algo in a separate public class (not the main method).
Like Daniel mentioned, run it via Windows Task Scheduler which itself will take care of most scheduling requirements.
This allows the solution to be scaled in the future if need be. E.g. convert into Windows Service, full GUI Winform or even SQL server scheduled tasks etc.