Code snippet – Disable agent using external file

Yesterday I was asked to create a way to let us disable agents running on a Domino server in an easy way before the Domino server comes back from a crash.

The reason for this request is that for a while we have been having one particular agent crash, taking the whole Domino server down with it. It only happens occasionally, and seems to be related to the document being processed. When the server comes up after a crash like that, a consistence check is done, then the agent manager launches the agent again, causing the server to go down again. I added code to the offending agent, so it would flag the document before processing and un-flag after processing is done. This way, when the agent encounters an already flagged document, it will be skipped as it was processed during a previous crash.

For some reason this did not work yesterday morning, when one of those rare corrupted(?) documents was encountered. The logic in the code was faulty, because the document was of a new type, so it was never flagged as being processed. The same document was processed over and over again, taking the server down every time.

So I simply created two functions, put them in a global script library where I keep utility functions used in many places, and added 3 lines of code to each agent where I wanted this functionality.

The first function is simply to check if a specified file exists. I am using en error handler to catch any error (for example missing directory).

Function FileExists(filename As String) As Boolean
    On Error GoTo errHandler
    If Dir$(filename)<>"" Then
        FileExists = True
    Else
        FileExists = False
    End If
exitFunction:
    Exit Function
errhandler:
    FileExists = False
    Resume exitFunction
End Function

The second function is the one where I check for the existance of a file named the same as the agent, with an extension of .disabled. If that file does not exist, I check for a file with the extension .enabled. If that file is missing, I simply create a blank file with that name. This way, the first time any agent is executed, the file will be created for us, and I don’t have to sit and manually create them all.

Function DisableAgent() As Boolean
    Dim session As New NotesSession
    Dim agentname As String
    Dim filename As String 

    agentname = session.CurrentAgent.Name
    filename = "D:\NotesAgentControlFiles\" + agentname + ".disabled"
    If FileExists(filename) Then
        DisableAgent= True
    Else
        filename = "D:\NotesAgentControlFiles\" + agentname + ".enabled"
        If Not FileExists(filename) Then	
            Open filename For Output As #1
            Print #1, ""
            Close #1
            Print "Created control file " & filename
        End If
        DisableAgent= False
    End If
End Function

Finally, in each agent I want to be able to disable like this, I add this code in the beginning:

'*** Check if disable-file exists, exit in that case
If DisableAgent() Then
    Exit Sub
End If

Just a few lines of code, but hopefully it will save someone a few minutes of work. Of course, you ca use this technique for many other things, your imagination is the limit.

Leave a Reply