I have a classified web application developed with ASP.NET MVC and I need to implement a advert bump up option.
As a example if someone use this option (for 3 days) today at 10.15 AM, advert should bump up each day 10.15 AM for 3 days.
So I need a scheduler to execute some code to bring up the advert to top of the list.
Can someone explain me how approach this inside my ASP.NET MVC application or using any other 3rd party resource?
save on the database every time the user click on the option. and every time he clicks on the option check if he has click in the pass two days.
You will need to use a task scheduler service. It is not possible to create a scheduled task in ASP.NET MVC, because it simply listens and responds to HTTP requests. You could put your code to bump up the advert in an ASP.NET MVC controller, but you would still need something to send an HTTP request to trigger the controller at the given time. So there's probably not a great reason to put that code in a controller under most circumstances.
It is easy to set up scheduled tasks in most cloud computing environments. For example, in AWS you could write a Lambda function in C# and schedule it with a CloudWatch Events rule.
If your application is running on a Windows Server, the most robust way to implement a task scheduler would probably be to write a Windows Service application to call the task logic on a polling loop. It is also possible to create a console application and trigger it with the Windows Task Scheduler. This approach is quicker and easier to get up and running than developing a Windows service, but in my experience is more likely to fail and quit running without your knowledge. Either way, it will be important to make sure to write good logging and error notification code.
I'm sure there are also good 3rd party tasks schedulers out there too, I just haven't used any of them. Those are just some general guidelines, but if you want more detail on them just let me know and I can post some links.
Related
I am trying to develop an application that will scan a website, get data from the website and save that data into database 3x per day at given hour that can be set in xml configuration file.
As an addition group of users can trigger the start of the application manually max few times per day.
I am looking for pros and cons of using a windows service for this solution or should I set a 3 scheduled tasks that will run the console application?
If I will decide to use a windows service then what is the best way to trigger a manual start of the service while the service is already running? The group of users will have some kind of web interface to trigger manual start.
This could be easily done using a scheduled task. I would just set a 3x scheduled tasks that will run the application at given time and the group of users could just start the .exe file from the web interface. However how to only allow the user to run a manual trigger only if application is not already running?
Since the UI is ultimately in web, and thus the service itself won't need any UI, I would go with Windows service which can be triggered to start by the user through web or automatically as the time comes (by its internal code).
Then, either:
In the service it has something to indicate its status as running which can be captured by the web app to see it, or,
In the web, there is mechanism to request/monitor the service status
Is quite flexible I think. I would go with whichever is easier.
I picked the task scheduler option for my case. It was easier to implement the manual triggering by users mechanism.
Pros - Triggers can be added easly using the taks scheduler library.
I need to create an application that will run on a server and be able to be configured to run commands at certain times. For instance, there will be a web interface allowing a user to set an engage time and a disengage time. Once those values have been saved by the user I need for the server to be able to fire off those commands precisely at the time specified each day.
I would also need to be able to set single use non recurring events that would occur... maybe 10 minutes from the time an event was triggered and have a command fired off when that 10 minute timer goes off.
I've already got a class library written that has the engage and disengage commands exposed. I would hope to be able to integrate this into whatever solution I end up with and simply be able to make calls directly to the class. Alternatively I could also compile the class library into an executable and have commands issued to it via command line. I'm hoping to not have to do the latter.
I've never written anything like this before. I've peeked a bit at Windows Services, but there is a lot of chatter out there saying that it isn't necessarily the best option. Can someone please guide me in the right direction please?
A windows service is not a bad idea, its perfect for this kind of application. Unless you end up using standard windows scheduled tasks as the trigger for your command, you need some sort of process that is always running to contain your scheduler. A windows service is an excellent candidate for this.
Using a windows service in conjunction with Quartz.NET and some sort of persistence layer so you can store your schedules (in case you need to restart the service or it crashes etc) would be a good way to go.
Alternatively, you could write an application that just adds and removes windows scheduled tasks, but considering you have existing class libraries, using Quartz.NET will fit in well with your existing libraries.
easiest solution:
make a console exe and run under scheduled task in windows.
Let web page to accept user input and modify a configuration file.
I have a clockIn & clockOut module that records start time & end time for workers from an Asp.NET app.
I want to ask how to do cron job for asp.net application to see if a worker has not entered their time on that date or week and send an email notification to remind them to enter start time and end time.
ASP.NET is the wrong tool for the job. You would be better off either writing a Console Application that is run on a scheduled task, or a Windows Service that polls on a regular interval. ASP.NET is purely meant for a request/response model.
Either one can access whatever data store the website is reading from/writing to just as easily as the ASP.NET site can.
If you've done neither, the Console Application is by far the simplest to write and implement. Windows Services aren't all that bad, but involve extra overhead, including difficulty debugging, and the need to go through a special installation process as compared to the XCOPY deployment model possible with Console applications.
If you REALLY want to do it in ASP.NET, you can write an asp.net web page that does this, and use the Windows Scheduled Task to run it. The Scheduled Task can open up Internet Explorer to a specific page just as easily as it can run any executable. But I wouldn't recommend it. You'll forever have to close the IE window when the task is finished, and it's just really a "hackish" solution. I did it back when I was a pure web developer and didn't know any better, but not since.
Jeff Atwood posted on the Stack Overflow blog, titled as Easy Background Tasks in ASP.NET, in the early days of this site about a simple way to do background tasks in ASP.NET. If your tasks are relatively lightweight, this might not be a bad way. If you have a reason to really want to keep everything inside an existing ASP.NET app, this might be the way to go.
The gist:
At startup, add an item to the HttpRuntime.Cache with a fixed expiration.
When cache item expires, do your work, such as WebRequest or what have you.
Re-add the item to the cache with a fixed expiration.
Although its not technically a cron job, from within a website you can get the website to call a set page at X time of the day.
I have done this a few times via a web method call, which is called on the timer from the application start.
Basic explanation is here:
Call a webpage from c# in code
First of all sorry of asking such a dumb question, I am quite a newbie in asp.net.
So, I am supposed to do something periodically, say I am owner of site heartpatients.com (hypothetically) and I want that for each of my site user who visits the site, a message to be shown after 2hrs "Take your pills". so, basically this is all my question, how am I supposed to show this message after every 2hr (or 4, 6 whatever time)after, also how can I customize time.
One more thing, say if I have this method in a WCF service, that shows this message, how can I call that service at a particular time, and that even configured by user (say someone is taking pills after 10hrs?) So how to call that service (that particular method in service) after the time specified by user passes periodically?
I hope I made my question quite clear.
Any help is appreciated.
ASP.NET generally isn't suited as a task scheduler. The nature of the web is as a request/response system. So a web application should just sit and wait for a request, generate a response, and be done.
For any kind of back-end scheduled task, I'd recommend either:
A Windows Service
A console application scheduled to run (I think Windows comes with a task scheduler)
There are pros and cons either way. For example, a Windows Service will run from boot time and has no console UI, and is generally very manageable from a server perspective. While a console application is traditionally simpler to write and debug.
These can still share code from your web application. If your business logic and data access and all that good stuff are in their own projects/assemblies then these other applications can use those assemblies just as well. (Of course, if everything in your web application is UI-bound, that's another question entirely.)
What concerns me the most is... How do you plan to show this message to a user? Is the user just sitting on your website for hours at a time and you need to remind them to take their meds? Or do you plan to send an email or something? Maybe the example you gave doesn't really explain what you're trying to do? I'm not sure.
Running tasks in the background is one thing, but it seems to me that an entire half of your overall equation (displaying a message to the user) is sort of glazed over and not really thought through.
Check out Quartz.Net: http://quartznet.sourceforge.net/
It enables you to schedule tasks to run using cron expressions:
http://en.wikipedia.org/wiki/Cron
I have a project that get daily securities end of day values from the web and store them in a database.
I want it to happen once a day at a specific hour.
What is the best approach for that kind of need?
The simplest way:
Create a console app to perform the task and use the Windows task scheduler to run it at the desired time and interval.
You could also create a Windows Service but these are harder to deploy and debug than a console app.
(easiest first)
Schedule your application using Windows Scheduler
Implement a Windows Service and use Quartz .NET for scheduling
Implement scheduling manually and just keep your application running forever
You have a LOT of options, all with pros and cons. Just to name a few :
A loop that would keep your application running in the background.
A schedule agent that would launch your application once a day.
Using Windows Scheduler.
Another third party (or your own) scheduling application.
Given so little information, anything I'd be telling you about which is best for you wouldn't be worth a byte!
Option1: You can do it in your Csharp code using the date time check inside a timer control that triggers on regular interval. This approach is bad since it requires your application running continuously.
Option2: You can use a scheduled task in your OS and run the application at the specific hour to attain the same. My company follows this to generate automatic reports
Option3: You can use a windows service.(A Simple window service tutorial)
Here is a link that will help you decide whether to choose a scheduled task or a windows service.
Check out this post http://www.codeproject.com/KB/cs/Midnight_Timer.aspx and change it from midnight to the time you require.