Wednesday, December 3, 2008

Simple way to ensure scheduled agents run on the correct server

This tip is probably only valid for agents that run by document selection.

We recently added clustering to one of our environments. This resulted in some replication conflicts/problems due to agents modifying (new and modified) documents on two servers.

The simplest solution to the problem I could come up with was to add a computed field, originating_server, with value=@ServerName.

Then in the agent:
'Get unprocessed-collection..
Set doc = col.getFirstDocument()
While Not doc Is Nothing
If Join( Evaluate( |(originating_server = @ServerName)|, doc ) ) = 0 Then
'Document modified on another server
'Do processing
End If
Call session.updateProcessedDoc( doc )
Set doc = col.getNextDocument( doc )

Update: To avoid leading people on a harder track than need be. If your app is located on a single server/single cluster, use Run on -> [Your server] in the Agent Schedule settings.

Or, as DiDu suggests, check if the agent is running on the Administration Server/stop it if not. Set the Administration Server in the ACL-dialogue, under Advanced.

The apps I had problems with are all from the same template, running in two distinct environments (separate NAB/Notes domain).


Dietmar said...

We often run agents on "All servers" but let them escape early if currentserver <> Administration server of the current database. This way, an admin is able to change the server the agent is running on by simply assigning another administration server in the ACL. Because there can be only one admin server by design, it never will happen that the agent runs on more than one server.

Tommy Valand said...

With the procedure in question/environment I work with, this wouldn't work. The application-template/applications I had trouble with are on three servers.

Two of the servers are in a cluster, and one is by itself.

For the administration server-solution to work I'd have to maintain two templates, which would be horrid to maintain in the long run.

I considered elaborating about the problem in the blogpost, but decided not to.. Too much information.

David Leedy said...


I understand not wanting to give too much information but I don't see why you would have to maintain 2 templates... What I usually do is set the agent to run on all servers and have a profile document with a field for "Active Server". Then if the agent is running on something that's not the active server it just stops processing. The same template is used for all servers.

What I do lose here immediate failover of the active server goes down. It's never really been a problem as the notes servers are so reliable but we could probably add logic to the agent to see of the active server was "up". And if not then run the code...

Tommy Valand said...

@LaserDude: If I understand you correctly, you're describing one environment.

The apps in question reside in two environments. Each environment has a separate NAB. Each environment has it's Active/Administrative server.