I created a functionality to update my web application database using data from an xml file. I want this database to be updated after ever 24hrs. My problem is I do not know where and how in the code should I call this method such that it does not have to be triggered by a button click and its executes after every 24hrs.
Please Help.
Part of the problem with background tasks in ASP.NET is that you don't have a standard application running to kick off your threads.
If you start firing off background tasks in the webapp, you can end up with inconsistent behaviour if something causes your appdomain to fail, your task can be stopped part-way, which if you're updating a DB could be a serious issue.
The most flexible way to implement this is using a seperate application running on a windows scheduled task which updates the DB for you. This separates the task from the webapp completely, but can be awkward to maintain.
It does however mean that even if your webapp is offline for whatever reason, the DB is still updated so long as the server running the task is working at the time.
If you do wish to embed the task in the webapp, something akin to cron would be preferable, as it will allow you to fire the task according to a set of rules, and leave it to its own devices.
Quartz Scheduler can achieve this, but can be fiddly to get working, and is sometimes overkill for simple tasks. It is worth learning for more involved systems which require a lot of tasks running in complex schedules, and allows the use of cron expressions.
HangFire is a different solution to the same problem, and is somewhat more simple than Quartz Scheduler, while still allowing cron configuration for tasks.
Using HangFire, the following will trigger the task at midnight every day (where UpdateDB is the method which runs the update).
RecurringJob.AddOrUpdate(() => UpdateDB(), "0 0 0 * * ?");
For your needs, HangFire is probably the quickest way to get this working.
The following will give you an overview of HangFire : http://docs.hangfire.io/en/latest/quick-start.html
HangFire is available in the NuGet archive so you can add it very quickly using the NuGet packages context menu, and documentation for timed tasks is here : http://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html
Hopefully this is enough to get you started.
Related
I am working on an application where I have multiple servers on different machines doing long operations for me. There is a windows service running on those machines written with hangfire/topshelf. Only one operation can run at a time per machine. Additionally I want to do some status check and cleaning jobs periodically on each server, so I can't just queue them as jobs.
Is there a way to do that in hangfire? Also, is there a way to send a follow-up job to the same server as an earlier job?
ADD-ON: I know one possibility would be to add another hangfire layer: Make each of the services a hangfire client with own DB and serve themselves, and then schedule recurring jobs for them, but that seems awfully complicated - especially when scaling out and adding servers.
If your task is to run some scheduled task on each server, I think, the best option is to implement it yourself, Hangfire don't support events handling, only command handling. I think, you reached the point of Hangfire possibilities and need to switch to more powerful and general tool.
For events and their handling you can use other systems, for example RabbitMQ. You just specify event generator and subscribe all your machines for this event.
I know this is a bit late, but the way we handle this sort of thing is just to write a simple console application and schedule it with Windows Task Scheduler.
You've probably resolved this by now, but
1 - one job per server - as you have it - worker count - probably the best as you can have multiple queues per server and the filters won't help you there.
2 - should the cleanup run after each processing job?
if yes, you can create the cleanup job from within your process job execution (ok maybe not perfect design but it works just fine) and assign to a queue on the same server, just add some logic in filters to ensure processing job is followed by a cleanup job and you're sorted.
alternatively you can use Continuation jobs (as on the site https://www.hangfire.io/) - Haven't used these but sounds like it might do the trick.
if you just want to periodically run the cleanup code then just schedule the job as recurring on each of the servers
I am in need of scheduling tasks that need to run every hour daily. I also ideally need to "fire and forget" a task, although i could handle this by creating another task to run every 5 minutes instead.
I have created a WebApi and ideally would like to run a scheduler within this, rather than having to create another application, such as a console app and then setup a windows schedule task. I see that there is a nuget package called Quartz.net that can deal with this.
Quartz sounds good to me, because code is all in one place. Plus because i will have multiple copies of this webApi running, Quartz could run for each instance.
But overall i want stablility! I have been reading sites and they say because your application pool is recycled by default every X hours, scheduling in a webApi is not a good idea.
Has anyone done this before, and which option did they go for and why?
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.
I am creating a web application in which I need to allow the user to schedule the excecution of a task.
I have gone through the various threads for scheduling the task but all of them are using windows service that I am not aware of. Moreover I cannot install visual studio in the server systems due to budget constraints.
Is there a way to create a method that runs a scheduler in a background thread in the asp .net application.Any code sample will be of great help.
That's not the way to go. If you need a scheduled task you should create a console application and run it using the Windows task scheduler.
You could create an application that sends an email to the user with a link to the page where the task is supposed to be done.
One thing to understand is that ASP.NET is intended to service requests from the network.
Everything in it is geared towards that. So, yes, you can run background tasks, but there are a number of caveats and gotcha's.
For instance, IIS will shut down your ASP.NET application if it does not receive any requests for some period. Also, your Application may be restarted without warning, if it detects changes to the file system. So, running code will be aborted.
That's not to say you can't work around these, but it's really not the best fit for scheduled task execution.
Your best bet would be to store the details of the task in a database, and then using either a single always-running Windows Service (really not that difficult to do, there are plenty of examples), or a console application (as suggested) scheduled manually to run regularly, to execute these tasks.
You may find that a library such as Quartz.NET may be of help scheduling/running these tasks.
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