Introduction
Ever wanted to create a web application that sends reminder e-mails every week? Or that cleans up a remote images or articles folder? Or to backup a certain FTP folder every night or week? This document describes a ScheduledTask
class written by David van Leerdam for use in (for example) a Windows Service that you can run to accomplish these tasks for you.
Background
When you're hosting your own applications, scheduling events for your web server is not a real challenge. But it is an issue when your (for example, ASP.NET or PHP) application is hosted by a third party which does not allow running of home grown timers, schedulers, Windows Services etc. A possible solution is to run the (web) scheduler on a remote machine, for example, a machine at home or at the office, which is always online. This is also great for remotely creating backups of websites through FTP.
Using the code
The solution consists of two parts:
- An application, a Windows Service (or whatever) that references the WebScheduler assembly (see downloads).
- The WebScheduler assembly containing the
ScheduledTask
class.
For part 1, I assume you will be creating a Windows Service (see downloads for sample code). All the code my Windows Service contains is:
Protected Overrides Sub OnStart(ByVal args() As String)
Try
sites = ScheduledTask.GetAll()
For i As Integer = 0 To sites.Count - 1
Dim st As ScheduledTask = sites(i)
st.Start()
Next
Catch ex As Exception
EventLog.WriteEntry("WebScheduler", _
"An exception occurred. " & ex.ToString())
Return
End Try
End Sub
Protected Overrides Sub OnStop()
Try
For i As Integer = 0 To sites.Count - 1
Dim st As ScheduledTask = sites(i)
st.Cancel()
Next
Catch ex As Exception
EventLog.WriteEntry("WebScheduler", _
"An exception occurred. " & ex.ToString())
Return
End Try
End Sub
The result of the GetAll()
method is an ArrayList
containing all the tasks defined in the config file.
Tasks are defined using a config file (currently hard coded to reference c:\WebScheduler.config). This example contains a task definition for creating a backup of mydomain.nl every night at 0:45 A.M.:
="1.0" ="utf-8"
<config>
<tasks>
<task id="ftp.mydomain.nl"
url="ftp://ftp.mydomain.nl/"
scheduledFor="everyDay"
startTime="00:45:00"
saveTo="c:\backup\websites\mydomain.nl\"
recursive="true">
<credentials
username="davidl"
password="p4ssw0rd" />
</task>
</tasks>
</config>
Note that any configured path to a folder (i.e. not a file) needs to end with '\'. This is applicable to both local and remote paths.
Points of Interest
A few changes are required to make this component more reliable:
- What happens when the config file is updated while a task is being executed is to be researched.
- Further attempts should be made when a request or a complete task fails.
All suggestions, bug reports, code updates, refactorings etc. are welcome. Please leave me a message at the bottom of this page so that I can update the article.
Consuming application
I will keep a list of applications that consume this component or parts of it. Please leave me a message so that I can list your application!
- My own SchedulingService application, see downloads.
History
- 19 October 2005: Updated the article with version 0.2 of the code. Some major changes include a better FTP component (version 1.2.2 of edtFTP.NET), config format adjustments, and bug fixes. Minor adjustments where made in terms of performance and refactoring.
- 1 February 2005: Posted the article with version 0.1 of the code.