I am looking for a way to have a script run every day at 5am to delete the contents of a Temp folder. The following is the method I am considering. I would appreciate any thoughts on this or suggestions for other methods. I want to keep everything local, so that there are no external dependencies from outside my account on Discount ASP hosting.
Have a textfile containing the datetime of the next desired run (5:00am tomorrow).
Have a Datetime cache value that expires after (one hour?)
When someone hits the website and the cache is expired, reload the datetime into cache
If the Datetime has passed, run the script to be "scheduled" and add 24 hours to the DateTime in the file
Your comments are appreciated.
You are on the right way. Here is a good article how to achieve this.
Also, based on your comment, why not use session end event to purge? Also, you can hook to application end as well, just in case.
You could also create a web service to perform the task, then have a scheduled task call the web service periodically.
This isn't a good idea, as you are depending on input that might never come to execute an operation which has a need to be performed on a regular basis.
Because of that, you need input from outside of the site (since the site is triggered by requests) to trigger your event, in other words, a scheduler.
You should be using a Scheduled Task for this. If you aren't, then you should have some other process which will send the event to the site (expose a web method perhaps) which on the schedule you desire.
Related
Every night my HTTPContext.Current.Cache is cleared. I want to Warm up the site during the night at time X and fill the cache with data.
Since I use HTTPContext, this has to run within the website. I am currently starting the caching by a simple button click on the site that then asynchronously fills the cache in a few minutes time frame.
I have searched for a solution where the options didn't really do the job as easily as I think it could be:
System.Timers are polling constantly. It also doesn't seem to have an option to set a specific time I want it to run?
Quartz seemed very overkill, but could probably do the job. Although a bit to read into to get it working.
RegisterWaitForSingleObject Could also work, but only a timer here as well? Also not sure if you can check if one object is already created and therefor risk of creating many of them. Nor change the timer after the object is created.
Skimmed through them relatively fast, so could be wrong. What's your take on running scheduled http request from within the site?
[EDIT] typo.
Here's what I'd do-
Create a page within the site with anonymous access, and put the code to start the caching (which is written on button click right now) ON THE PAGE LOAD event.
Create a simple console application in C#, which makes HTTPRequest to that page (You can see examples for System.Net.WebClient).
Schedule the .exe of this console application to run at certain time (like 4 AM) on Windows Scheduler on the server.
Situation: I need to send notification emails every Monday at 9 a.m to all employees, remind them to due the last week report.
I basically know how to "send email in ASP.NET" (I implemented the contact us form on our website, so "sending email service" basically will be executed when people hit submit button) but I had a hard time to figure out how to make it automatically execute at a certain time, maybe I need a scheduler or something like that, I don't use any database here?
Somebody who have done it before could give a right direction?
For scheduling tasks I always recommend Quartz.NET. However, you should know that there are certain issues when you try to implement scheduler in asp.net application (in my opinion it is better to implement it in windows service). If you implement it in asp.net application remember about application pool idle time (set it to high value or disable automatic pool recycling) since pool must be active when the scheduler needs to execute the task.
You should create a windows service which will do the job
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
I am basically creating a site for recruiters. One of the functionality in my application requires posting to Facebook periodically. The posting frequency can be from 0(Never) to 4(High)
For Eg. If a recruiter has 4 open jobs and he has posting frequency set to 4, each job should be posted as per it's turn: 1st job on 1st day, 2nd job on 2nd, 3rd job on 3rd etc, on 5th day again 1st job (round robin fashion).
Had he set the posting frequency to 2, two jobs would be posted daily (thus each job would be posted every 2 days)
My only question is what type of threading should I create for this since this is all dynamic!! Also, any guidelines on what type of information should I store in database?
I need just a general strategy to solve this problem. No code..
I think you need to seperate it from your website, I mean its better to run the logic for posting jobs in a service hosted on IIS ( I am not sure such a thing exists or not, but I guess there is).
Also you need to have table for job queue to remember which jobs need to be posted, then your service would pick them up and post them one by one.
To decide if this is the time for posting a job you can define a timer with a configurable interval to check if there is any job to post or not.
Make sure that you keep the verbose log details if posting fails. It is important because it is possible that Facebook changes its API or your API key becomes invalid or anything else then you need to know what happened.
Also I strongly suggest to have a webpage for reporting the status of jobs-to-post queue, if they failed what was the causes of problem.
If you program runs non-stop, you can just use one of the Timer classes available in .NET framework, without the need to go for full-blown concurrency (e.g. via Task Parallel Library).
I suspect, though, that you'll need more than that - some kind of mechanism to detect which jobs were successfully posted and which were "missed" due program not running (or network problems etc.), so they can be posted the next time the program is started (or network becomes available). A small local database (such as SQLite or MS SQL Server Compact) should serve this purpose nicely.
If the requirements are as simple as you described, then I wouldn't use threading at all. It wouldn't even need to be a long-running app. I'd create a simple app that would just try to post a job and then exit immediately. However, I would scheduled it to run once every given period (via Windows Task Scheduler).
This app would check first if it hasn't posted any job yet for the given posting frequency. Maybe put a "Last-Successful-Post-Time" setting in your datastore. If it's allowed to post, the app would just query the highest priority job and then post it to Facebook. Once it successfully posts to Facebook, that job would then be downgraded to the lowest priority.
The job priority could just be a simple integer column in your data store. Lower values mean higher priorities.
Edit:
I guess what I'm suggesting is if you have clear boundaries in your requirements, I would suggest breaking your project into multiple applications. This way there is a separation of concerns. You wouldn't then need to worry how to spawn your Facebook notification process inside your web site code.
I want to copy one row from table A to table B per hour on my asp.net website. How can I do that? Do I need win32 application? I would like to avoid that.
This sounds like you want to create a stored procedure and use the Sql Server Jobs scheduler to run it every hour. See here for details of setting that up
Or if you are using Sql Express see this question "How to run a stored procedure every day in SQL Server Express Edition?"
Ideally you wouldn't do this using IIS etc, unless there is some reason you don't have access to the database.
You can use Timer.
If you use SQL Server you can also schedule a job on the DB directly.
I would agree with brodie. Alternativly create a SSIS package and run either as a SQL Job or a scheduled task from command line(using DTEXEC).
http://msdn.microsoft.com/en-us/library/ms365302.aspx - Shows you how to create a basic solution.
http://decipherinfosys.wordpress.com/2008/09/17/scheduling-ssis-packages-with-sql-server-agent/ - Shows you how to schedule your task.
For your exercise either would do the trick. I would suggest using either that you feel more familiar with. But certainly wouldnt suggest creating a timer driven service.
You could create a webpage that accomplishes this and use a free service like pingdom.com to call that page every hour.
EDIT:
Mine is the easy route, probably not the best solution but quick and simple
You can check this out also:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
see this example for creating a new thread in global asax, http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax
Be aware though if the website goes down the process will not continue to run, if this is a problem a sql batch job or windows service would be better suited, and if the task is resource intensive it would be better to do this on another machine anyway as the global asax method will take away processing resources from your website