Subject: RE: I changed the agent schedule, and it ran immediately
I used this method for years, until Notes 6 came out. Before when you signed a database, it only signed those design notes which had been modified. Now when you sign, it resigns all design notes. Also, I have gotten into the habit of always doing a Recompile All LotusScript before moving to production, which does change your agent, causing it to run immediately.
Because we have agents that cannot run more than once a day, I created the following function which determines if it is OK for the agent to run, based on data in the agent comments. The side of this is that in the designer, I can see when and how often an agent is supposed to run without opening it.
Hope this helps.
Ron Reed
Function OKToRun(nsTemp As NotesSession) As Boolean
'This function attempts to verify that a scheduled agent is running within its scheduled
'time. An agent that is only supposed to run once-a-day may get run a second time if the
'database design is refreshed with a newly signed template.
'
'For this function to work, the schedule information must be placed in the agent's comment
'section using the following format: Server, time (or time range), days (frequency). Do not
'use spaces in the time or time range (6am, 7:30pm, 20:00, 7am-9pm, 7:00-21:00). When
'specifying time in 24 hour format, minutes must be included (6:00, 21:00). Days may be
'either numeric (1, 15), ordinal (1st, 15th) or "Daily", "Weekdays", or a day of the week
'(Monday, Wednesday). When specifying the day, only the first three characters are
'significant and is case insensitive.
'
'Examples of valid comments:
' Notes5, 12:30am on 1st.
' Notes5, 18:30 on 1st.
' Notes3, 4:15am on 15th.
' Notes3, 4am, Daily.
' Notes5, 9:30pm, Daily
' Notes5, 10:00pm, Daily
' Notes3, 6:55am-4:45pm every 3 hours, Weekdays. (Does not check that it has been 3 hours since agent last run.)
' Notes5, 6:30am on Monday.
' Notes5, 9:30pm, Tuesdays.
' Notes5, 21:30, Tuesdays.
'
'The check is only for scheduled agents running on the server. If a scheduled agent is
'started manually (not running on the server), it is assumed the user knows what they are
'doing and allows the agent to run. No error or warning message is displayed.
'
'Parameters:
' nsTemp = NotesSession object.
'
'Calls:
'
'Constants:
'
'Public Variables:
'
'Views:
'
'A sample call would be:
' 'In agent's Initialize section. Agent's comment: Notes5, 6:30am, Daily.
' If OKToRun(sessionCur) Then
' ...agent code here...
' Else
' Print "Canceled. Attempt to run agent outside of scheduled time."
' End If
Dim dtBeg As NotesDateTime
Dim dtEnd As NotesDateTime
Dim dtNow As New NotesDateTime(Now())
Dim bOKToRun As Boolean
Dim dBeg As Double
Dim dEnd As Double
Dim dNow As Double
Dim iTemp As Integer
Dim sTemp As String
Dim vComment As Variant
bOKToRun = True
If nsTemp.IsOnServer Then 'Don't check if running manually.
If nsTemp.CurrentAgent.Comment <> "" Then 'Don't check if no comments.
bOKToRun = False 'Assume it is not supposed to run.
vComment = Split(nsTemp.CurrentAgent.Comment, " ")
'First check to make sure it is running on correct day. Days (frequency) should be
'the last thing specified in the comments, and therefore the last element in the
'array vComment. Days can be a specific
sTemp = vComment(Ubound(vComment))
If Isnumeric(Left(sTemp, 1)) Then 'Specific days, i.e. 1st, 15th, 30th.
If Isnumeric(Mid(sTemp, 2, 1)) Then iTemp = Cint(Left(sTemp, 2)) Else iTemp = Cint(Left(sTemp, 1))
If Day(dtNow.DateOnly) = iTemp Then bOKToRun = True
Else 'Non-specific days, i.e. Daily, Weekdays, Monday.
Select Case Lcase(Left(sTemp, 3))
Case "dai" : bOKToRun = True
Case "wee" : If Weekday(dtNow.DateOnly) >= 2 And Weekday(dtNow.DateOnly) <= 6 Then bOKToRun = True
Case "sun" : If Weekday(dtNow.DateOnly) = 1 Then bOKToRun = True
Case "mon" : If Weekday(dtNow.DateOnly) = 2 Then bOKToRun = True
Case "tue" : If Weekday(dtNow.DateOnly) = 3 Then bOKToRun = True
Case "wed" : If Weekday(dtNow.DateOnly) = 4 Then bOKToRun = True
Case "thu" : If Weekday(dtNow.DateOnly) = 5 Then bOKToRun = True
Case "fri" : If Weekday(dtNow.DateOnly) = 6 Then bOKToRun = True
Case "sat" : If Weekday(dtNow.DateOnly) = 7 Then bOKToRun = True
End Select
End If
If bOKToRun Then
'Running on correct day. Now check agent is running in alloted time. Time
'should be the second element in vComment.
bOKToRun = False 'Again, assume the worst.
sTemp = vComment(1) & ","
iTemp = Instr(1, sTemp, ",")
sTemp = Left(sTemp, iTemp - 1) 'Remove comma.
iTemp = Instr(1, sTemp, "-")
If iTemp Then
'Handles time ranges, i.e. 7:30am-8pm.
Set dtBeg = New NotesDateTime(Left(sTemp, iTemp - 1))
Set dtEnd = New NotesDateTime( Mid(sTemp, iTemp + 1))
Else
'Single time specified. Ending time will be 5 minutes from beginning time.
Set dtBeg = New NotesDateTime(sTemp)
Set dtEnd = New NotesDateTime(sTemp)
Call dtEnd.AdjustMinute(5)
End If
'Check to make sure the current time is within the beginning and ending
'time specified.
dBeg = Round(Timevalue(dtBeg.TimeOnly), 13)
dEnd = Round(Timevalue(dtEnd.TimeOnly), 13)
dNow = Round(Timevalue(dtNow.TimeOnly), 13)
If dNow >= dBeg And dNow <= dEnd Then bOKToRun = True
End If
End If
End If
OKToRun = bOKToRun
End Function