I have a custom agent that imports content from an external server. This process can take from 5 minutes to 2 hours. Agent can be started by scheduler or user. I want to prevent two same agents from running at the same time. Currently I have a flag in the DB that determines agent status. I set it to 1 when agent starts and to 0 just before agent stops. At the beginning of agent run I check flag and if it is 0 then agent starts.
It happens that there is an undefined event during import when agent stops and setting flag to 0 doesn't happen. As a result scheduler or user can't start next agent, because flag remains 1.
Is there a better way to monitor if agent is currently running? Any system variable or method?
Code:
public class ImportAgent
{
public void run()
{
if(agentRunning == 0)
{
//1. Setting agentRunning to 1
try
{
//2. Importing content
}
catch(Exception e)
{
//Error
}
finally
{
//3. Setting agentRunning to 0
}
}
else
{
//Agent is running. Waiting till next agent run
}
}
}
Config:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<scheduling>
<agent type="Name.Name.ImportAgent" method="Run" interval="00:30:00"></agent>
</scheduling>
</sitecore>
</configuration>