Handling is required. You must implement function
named OnTimer in the service script. Sample prototype:
Function OnTimer
' Do the main tasks
OnTimer = "Everything is ok ..."
End Function
(See also the preinstalled sample script). This function is
called each time the interval specified elapses - see the
Interval configuration option. The
returned value is logged to the EventLog (if not disabled - see
DiableEventLog configuration entry), to the log file (see
ReportFile configuration entry) and shown in the application
window if Script Service is running in application mode. The
value must be string in order to be logged, if you return
something else (for example Boolean value or nothing) it will be
ignored.
The implementation may vary, but in general this event is
supposed to give the script a chance to do some scheduled work.
In other words OnTimer function is the only required element in
the service script. You may ignore it and just return if you are
designing a script that will provide only on-duty services (e.g.
set of functions to be called from various applications). In
most cases it is required because this gives to the script
chance to do something on its own decision. If you need complex
scheduling you can implement it by checking the current time (Now
function in VBScript) and perform certain tasks if certain
conditions, you choose, are met. Such a code can be implemented
easily in VBScript or JScript. Leaving the detailed logic to the
script allows you to create any scheduling scheme you want. Your
scheduling scheme may need inspection of a DB and many other
data sources there is no way to implement something that will cover
all the possible needs in the service binary file. In the same
time it is a matter of 2-3 lines of code to make time
calculations in a script language thus you need only to set the
interval to value that will be appropriate for the scheduling
scheme. For example if you need to do something 2-3 times per
day Interval can be set to one hour and this will be enough -
the script will check the system time and see which task (if
any) must be done this time. If you need to perform the tasks as
soon as possible you will probably prefer smaller interval - 5
minutes for example.
One example. Suppose you want to perform something after
midnight:
Dim lastTimeTaskPerformed
lastTimeTaskPerformed = Null
Function OnTimer
' Other code, checking for other tasks for example
' ...
If Not IsNull(lastTimeTaskPerformed) And Day(Now) = Day(lastTimeTaskPerformed) Then
' Do nothing
Else
' Do the task
lastTimeTaskPerformed = Now
End If
' ...
End Function
This will perform the task only once per day - at the first
call of the OnTimer for that day. |