I am writing a AddInTask that will run notes agents ( for multiple reasons I can not run the agent scheduled in the Amgr)
I want to do
lo moonserver agent1
lo moonserver agent2
and then
tell moonserver agent1 quit to stop that single instance of the moonserver addintask.
I tried to do this but I get
“Exception thrown! Error: Object is not open”
On the second instance when I include the part in
else if (addin.HaveSecondsElapsed(10))
{ …}
(without it the task can run multiple times)
//===========================================================================
//
// Module: MOONSERVER.CPP
//
// Description:
//
// Execute the MOON agents in an endless loop.
//
// SYNTAX: (at the Lotus server console) - to start >load MOONSERVER
// - to stop >tell MOONSERVER quit
//
//===========================================================================
#include
using namespace std;
#include “lncppapi.h” // Notes C++ API headers.
//#define MOONDATABASE “applications\MQInterface\MQINTERFACE.nsf”
#define AGENTTORUN “MQMail”
// Forward declarations:
void OurMOONSERVER(LNServerAddin &addin, LNString &taskname, LNDatabase &db, LNNotesSession &session, char * AgentToRun);
void DisplayMsg ( LNServerAddin &addin, LNString &taskname, char * AgentToRun, char * msg);
void GetMoonServerStatus(LNDatabase &db,char * ParmdocName, LNString &text);
//---------------------------------------------------------------------------
//
// MAIN
//
//---------------------------------------------------------------------------
int main(int argc, char *argv)
{
int ProgramStatus = 0;
LNNotesSession session;
LNServerAddin addin;
LNString buffer;
LNString taskname;
LNString moonserverstatus;
LNString MoonDatabase; // = “applications\MQInterface\MQINTERFACE.nsf”;
char * AgentToRun; // = “MQMail”;
// Throw all errors encountered during execution.
LNSetThrowAllErrors(TRUE);
try
{
LNDatabase db;
LNMessageQueue mqueue;
session.Init(argc, argv);
// read the agent to start (first parameter)
if (argc >= 1) {
AgentToRun = argv[1];
} else {
AgentToRun = "";
}
buffer = "MOON ADDIN to run the agent ";
buffer.Append(AgentToRun);
session.GetServerAddin("MOONSERVER",buffer, &addin);
taskname = addin.GetDefaultStatusLineTaskName();
DisplayMsg (addin,taskname,AgentToRun, buffer.GetTextPtr());
// First get our database file.
MoonDatabase = session.GetEnvironmentString("moonfilepath");
buffer = "'moonfilepath' ini setting: ";
buffer.Append(MoonDatabase);
DisplayMsg (addin,taskname,AgentToRun, buffer.GetTextPtr());
if(LNStringCompare(MoonDatabase, "") == 0)
{
DisplayMsg (addin,taskname,AgentToRun, "Exception - moonfilepath parameter is missing in notes.ini!");
}
else
{
session.GetDatabase(MoonDatabase, &db);
// Create a message queue for this addin task.
buffer = TASK_QUEUE_PREFIX;
buffer.Append(taskname);
try {
session.CreateMessageQueue(buffer,&mqueue);
}
catch (LNSTATUS lnerror) { // the messagequeue is probably in use
session.GetMessageQueue(buffer,&mqueue);
DisplayMsg (addin,taskname,AgentToRun, "Multiple instances started");
}
// LOOP to start the notes agent
// addin.Idle() serves two functions. First it relinquishes
// processor control to Domino allowing other tasks to run.
// Secondly, addin.Idle() will return TRUE, when it has recived
// a quit command from Domino meaning a user has typed
// "tell myaddin quit" at the server console.
if(LNStringCompare(AgentToRun, "") == 0)
{
DisplayMsg (addin,taskname,AgentToRun, "Exception - AgentToRun parameter is missing!");
DisplayMsg (addin,taskname,AgentToRun, "Syntax Load MOONSERVER agentname");
DisplayMsg (addin,taskname,AgentToRun, "e.g. load moonserver MQMAIL");
}
else
{
while (!addin.Idle())
{
if (addin.HaveMinutesElapsed(1)) // make sure to wait at least a second to make sure the task does not take all resources
{
//DisplayMsg (addin,taskname,"Starting the agent");
addin.SetDefaultStatusLineText("Running ...");
db.Open();
OurMOONSERVER(addin,taskname,db,session, AgentToRun);
// now check if we can continue or the agent flagged to stop
GetMoonServerStatus(db,AgentToRun,moonserverstatus);
if(LNStringCompare(moonserverstatus, "STOP") == 0)
{
buffer = "status in parmdoc set to ";
buffer.Append(moonserverstatus);
throw(buffer.GetTextPtr());
}
db.Close();
addin.SetDefaultStatusLineText("Idle");
//DisplayMsg (addin,taskname,"Agent finished");
}
else if (addin.HaveSecondsElapsed(10))
{
LNString message;
LNINT nRetLen;
const LNINT nMaxMsgLen = MQ_MAX_MSGSIZE;
char commandbuffer[nMaxMsgLen+1];
addin.SetDefaultStatusLineText("Processing tell commands");
// Process messages if any are in the message queue.
while (mqueue.GetMessageCount() > 0)
{
// GetNextMessage automatically de-queues the message
// and decrements the current message count.
mqueue.GetNextMessage( nMaxMsgLen, commandbuffer, &nRetLen);
// Do stuff with the messages...
message = "Received console command: ";
message.Append( LNString((const char *)commandbuffer, nRetLen) );
DisplayMsg (addin,taskname,AgentToRun, message.GetTextPtr());
}
// Append the status line text to the log file.
addin.SetDefaultStatusLineText("Idle");
}
} // End while (!addin.Idle())
}
}
} // END Try
catch (LNSTATUS lnerror)
{
char ErrorBuf[LNERROR_MESSAGE_LENGTH];
LNString errormessage;
LNGetErrorMessage(lnerror, ErrorBuf, LNERROR_MESSAGE_LENGTH);
errormessage = "Exception thrown! Error: ";
errormessage.Append(ErrorBuf);
DisplayMsg (addin,taskname,AgentToRun, errormessage.GetTextPtr());
ProgramStatus = 1;
}
catch (const char *pErrorMessage)
{
LNString errormessage;
errormessage = "Error thrown! Error: ";
errormessage.Append(pErrorMessage);
DisplayMsg (addin,taskname,AgentToRun, errormessage.GetTextPtr());
ProgramStatus = 2;
}
catch (...)
{
DisplayMsg (addin,taskname,AgentToRun, "Exception thrown! Error unknown!: ");
ProgramStatus = 3;
}
// Terminate the API
buffer = " ";
DisplayMsg (addin,taskname,AgentToRun, "Terminating");
session.Term();
return(ProgramStatus);
} // END main()
//---------------------------------------------------------------------------
// Name: GetMoonServerStatus
// Description: Retrieves the value of ‘MoonServerStatus’ on the parmdoc
// Inputs: db - handle to the opened database
// ParmdocName - key to find the parmdoc in the AgentParms view
// Output: text - text in the MoonServerStatus field
//---------------------------------------------------------------------------
void GetMoonServerStatus(LNDatabase &db,char * ParmdocName, LNString &text)
{
LNViewFolder View;
LNVFEntry ViewEntry;
LNDocument ParmDoc;
LNText MoonServerStatus;
db.GetViewFolder("AgentParms", &View);
View.Open();
View.Find(ParmdocName,&ViewEntry);
ViewEntry.GetDocument(&ParmDoc);
ParmDoc.Open();
ParmDoc.GetItem("MoonServerStatus", &MoonServerStatus);
MoonServerStatus.GetText(&text);
ParmDoc.Close();
View.Close();
}
//---------------------------------------------------------------------------
// Name: DisplayMsg
// Description: Display the passed message on the console.
// Inputs: addin - this addin application handle
// taskname - LNDatabase for the database that contains the agent
// msg - LNNotesSession for this Notes session
//---------------------------------------------------------------------------
void DisplayMsg (LNServerAddin &addin, LNString &taskname, char * AgentToRun, char * msg)
{
LNString buffer;
buffer = taskname;
buffer.Append(" add-in ");
buffer.Append(AgentToRun);
buffer.Append(" : ");
buffer.Append(msg);
addin.AppendLogMessage(buffer);
}
//---------------------------------------------------------------------------
//
// Name:
// OurMOONSERVER
//
// Description:
// Local function called by the main add-in task loop.
//
// Inputs:
// db - LNDatabase for the database that contains the agent
// session - LNNotesSession for this Notes session
//
// Outputs:
// If successful, the database is updated.
//
//---------------------------------------------------------------------------
void OurMOONSERVER(LNServerAddin &addin, LNString &taskname, LNDatabase &db, LNNotesSession &session, char * AgentToRun)
{
LNDocument Doc;
LNText AddInTextFld;
LNDatetime Dt;
LNDatetimes TimeFld;
LNString Author;
try
{
LNAgent agent;
// if(db.AgentExists(AgentToRun)) // Get background agent
// {
db.GetAgent(AgentToRun, &agent);
// }
// DisplayMsg (addin,taskname,"Opening the agent");
agent.Open(); // Open the agent's note
Author = agent.GetSigner();
// DisplayMsg (addin,taskname,Author.GetTextPtr());
agent.Execute(LNAGENTEXECUTEFLAGS_REOPEN_DB, LNAGENTOUTPUTOPTION_NONE, 0); // Run agent
} // END try
catch (LNSTATUS lnerror)
{
char ErrorBuf[LNERROR_MESSAGE_LENGTH];
LNGetErrorMessage(lnerror, ErrorBuf, LNERROR_MESSAGE_LENGTH);
// Close everything that may be open at this point and then re-throw.
Doc.Close();
db.Close();
throw lnerror;
}
} // END OurMOONSERVER()
// END MOONSERVER.cpp