Scheduled Task Time Constraint - c#

I've read through a few posts on SO on whether to use a Windows Service or Scheduled Task and from my understanding I should be using a Scheduled Task.
I have a simple program, basically do a little logic and send an email. The only hard requirement that I have is the email must be sent on the :40 minute mark of each hour. So 8:40, 9:40, 10:40, etc. When I initially setup the schedule for the task I can set it to start at 8:40, recur every hour, every day.
That seems to fulfill the requirement but should I be worried about anything in regards to ensuring the task is ran on that schedule?
It all seems so simple that I'm sure I'm missing something?

Well few points to mention
If you are using a batch file make sure other programs can replace or change it.
Use and access log to log whether the execution was successful or not so you can monitor later(Debugging you application)
Use schedule task errors to check whether your application ran correctly (http://support.microsoft.com/kb/308558)
Use as few privileges as possible.(which is the default)

Related

Calling different API with different interval time , saved in my database table

I have a list of API from different client saved in my Database table and all the API have different time interval for there API to be called. What should be my approach to call the API. New data may be added in the List of API table . Should I go for Dynamic Timers?
I have an application (GUI) which clients use to add new records.
These records represent an API url and the time (Schedule) at which that API should be called.
Your Challenge is to write code that is able to call all the Client specified API's at the specified schedule/time.
To me - API calling & Handling the responses (Storing into DB etc) should be one component. and, scheduling when to call which API - should be other component (Something like cron job). This way - when the time is right appropriate API call would be triggered. This also gives you flexibility to do multiple tries/retries in a day etc.
Update after your comment:
You have an application (GUI) which clients use to add new records.
These records represent an API url and the time (Schedule) at which that API should be called.
Your Challenge is to write code that is able to call all the Client specified API's at the specified schedule/time.
If I have got that problem right - my original suggestion stands.
Component 1 - Scheduler
Use Quartz.net (or create your own using a Timer etc) - and create a Service (say WCF) or Process which will read records from Database and identify all the schedules and the API urls that need to be called. When the scheduled time happens Quartz.net will trigger your handler method - where you will make a Call to Component 2 and pass on the API url.
Component 2 - API Engine
When it receives a call from Component 1 - it will make the API call and fetch the response. Store/process it as required.
There are various schedulers that can be used to do this automatically. For example, you could use Quartz.NET and its AdoJobStore. I haven't used that myself, but it sounds appropriate:
With the use of the included AdoJobStore, all Jobs and Triggers configured as "non-volatile" are stored in a relational database via ADO.NET.
Alternatively, your database may well have timers built into it. However, if this is primarily an academic exercise (as suggested by "your challenge") you may not be able to use these.
I would keep a table of scheduled tasks, with columns specifying:
When the task should next be run
What the task should do
How to work out the next iteration of that task afterwards
If the task has been started, when it was started
If the task completed, when it completed
You can then write code in an infinite loop to just scan that table, e.g. once per minute. It should look for all tasks with a "next time" earlier than now that haven't completed:
If the task hasn't been started, update the row to show that it has been started (now), and start executing the task
If the task was started recently, ignore it
If the task was started "a long time ago" (i.e. longer than it would take to run successfully), either mark it as "broken" somehow, or restart
When a task completes successfully, update the row to indicate that it's finished, and add another row for the next time it should be started.
You'll need to work out exactly what your error strategy is:
How long should the gap be between a task starting and you deciding it's failed?
Do you always want to restart the task, or should some failures be permanent?
Do you need to record how often a task failed, and give up after a certain number of tries?
What do you do if you explicitly notice that the task has failed while you're executing it? (Rather than just by the fact that it was started a long time ago.)
For extra reliability, you'd need to think of other aspects too:
Do you need multiple task runners?
How can you spot when a task runner has failed, and restart that?
How do you deal with multiple task runners trying to start the same task at the same time?
You may not need to actually implement everything here, but it's worth considering them.

Run a scheduled process every day at a certain time on web service

I've been building a web service to synchronize data between SalesForce and Zendesk at my company. In the process of doing so, I've built several optimizations to drastically reduce execution time, such as caching some of the larger datasets that are retrieved from each service.
However, this comes at a price. When caching the data, it can upwards to 3-5 minutes to download everything through SalesForce and Zendesk's APIs.
To combat this, I was thinking of having a background worker that automatically cached all the required data every day a midnight. However, I'm not sure what the best method of doing this would be.
Would it suffice to build a class that merely has a worker thread that checks every several minutes to see if it is after midnight, and activate it on launch from Global.asax. Or is there some sort of scheduler already in existence?
EDIT
There seems to be some division between using something like:
FluentScheduler or Quartz.net to house everything within my applications.
Versus using something like windows task scheduler and writing a secondary application to call a function of my application to do so. It seems that using a third party library would be more simple, but is there any inherent benefit to using the Windows Task Scheduler.
I think you want to add your data caching logic to a project of type "console application". You'll be able to deploy this to your server and run it as a scheduled task using windows "Task Scheduler". If you've not worked with this project type or scheduled tasks before there are stack overflow questions which should help here, here, and here. You can add command line parameters if you need and you should have a look at adding a mutex so that only one instance of your code will ever run at once.
add an endpoint that will know how do it and use the windows task scheduler to call that new caching endpoint.

how can i find the instanceid/correlation ID of the scheduled Task that started my process

if i have a windows scheduled task that runs my EXE. is there a way from inside my EXE to find the scheduled task instance that triggered me?
Easy answer for that would be: no.
Best thing you could do is programatically access either the task scheduler library and see whether the process you are is in "running" mode, get the PID of the process (scheduler actually writes it out in its history) and compare it to yours.
Easier thing to do would be accessing to system event logs and seeing if there's any mention of execution your exe file (though they may not be such a log if the exe was actually executed).
The only thing you can associate with your process is the caller (which might be NETWORK SERVICE or some predefined account) which do not give any information regarding it being scheduled.
I tried for a while to solve this same issue, however I don't think it's possible using the current API. If you have a reference to the running task, you can get the PID of the task engine that started it. From there, you can to go to the task event log and look up the latest events with ID 200 (Action Started) having the same engine PID, however since you can have multiple task processes running beneath a single task engine, you can't go any farther with absolute certainty (e.g., a second instance of your process could be running under the same engine PID -- you won't be able to tell which correlation ID belongs to your target process.)

Ways to perform scheduled tasks - Windows / .NET

My issue is pretty simple.
I have an application that should be executed automatically once a day. I have no prior experience with this kind of scenario (some time ago I worked with IBM Control-M but guess that it is way more complete, complex and expensive =))
I thought about two possible solutions:
Creating a Task inside Windows Task Scheduler, which would execute the application;
Implement the application as a Window Service which would run 24/7, but only would perform the needed actions depending on the current time.
Which are the advantages/disadvantages of each approach?
Is there another way to do this?
Thanks in advance.
If it only executes once a day (or so) then just do it as a regular command line app that is executed by the windows task scheduler. Task scheduler already has all of the UI necessary to determine when to kick off the program, pass in parameters and anything else related to the scheduling of the task.
The only real reason to do this type of function as a windows service is if it needs higher execution resolution than once a minute. However, the main downside to a windows service is that you would have to manage the logic for how often/when to kick it off. Another one is that the app is always running, which leaves open the possibility for leaked memory if your code has issues.
On Unix/Linux you would use a cron job schedule a task to be executed. MS Windows' version is called the Task Scheduler and it is already a service that run 24/7 and performs the needed actions depending on the time.
Create a repeating task with the Task Scheduler to run your application. Creating, installing and configuring a service application is not exactly trivial. It's a much more involved process than creating a standard Forms or command line app and you don't need to do it anyway.
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-csharp/70633/Waitable-Timer-in-C
Another library that might be of interest is Quartz.NET

Using Windows Service as a scheduling service

I have this question from a client, to run some database scripts on a daily or biweekly basis, depending on the type of period. They have defined a 'busy' period and a 'quiet' period. They are using shared hosting for their applications and they can't add SQL Server jobs to their database.
What I want to do is create a windows service, which is configurable using a config file, containing the different periods and their intervals.
This service is checking the current time and date versus the scheduled periods in the configuration file and will execute a sql server stored procedure once the interval matches
My question: Is there a better way to do this kind of scheduling without sql server or do you know if an existing, more generic (free) solution already exists?
[Edit]
I can't use windows task scheduler since their quiet period is during winter and their busy period is during summer. With task scheduler I'd have to modify the scheduled tasks manually twice a year. That's now what I nor my client wants.
[/Edit]
Thanks
You could use "Scheduled Tasks" feature of windows to start some console application whenever it is required.
If you want to create your own scheduling solution, then I'd recommend to have a look at Quartz.NET, which offers the same flexibility as the windows task scheduler.
Why not use the task scheduler and then every time your application gets started, it checks it's own schedule to work out whether or not it needs to do something.
So you could use the task scheduler to start your application every 15 minutes. When your application starts up it uses it's own configuration file to work out whether or not it needs to do something.
You could also use this to get your application to stop any currently running versions of your application to kill any long running tasks!

Categories