Skip to main content

Agent Initialization Scripts

An agent initialization script is run before the agent starts. The script can be used to initialize data before an agent is run. The script is also often used to set agent properties, such as database connection parameters, but we don't recommend you do this since the Agent class is undocumented and unsupported.

You can add an agent initialization script to an agent by clicking the menu Agent > Initialization Script:

agentInitScript.png

Agent initialization scripts can be written in C# or VB.NET.

The following example initializes a global counter.

  • C#

  • PythonPython

using System;
using Sequentum.ContentGrabber.Api;
public class Script
{
public static bool InitializeAgent(AgentInitializationArguments args)
{
args.GlobalData.AddOrUpdateData("counter", 1);
return true;
}
}

import clr
from Sequentum.ContentGrabber.Api import *

def InitializeAgent(args):
args.GlobalData.AddOrUpdateData("counter", 1)
return True

The following example makes changes to an agent before it's run. The changes to the agent are not permanent, but only in effect at runtime. When you are debugging an agent, the debugger works directly on the agent in the editor, so any changes to an agent are permanent. We, therefore, recommend you use the args.IsDebug variable to turn off any changes to an agent during debugging. 

NOTE: To use the following code in the agent, use this additional library. By default the library is not added in Agent Initialization Script.
C# : using Sequentum.ContentGrabber.Commands;
Python: from Sequentum.ContentGrabber.Commands import *

  • C#

  • PythonPython

import clr
from Sequentum.ContentGrabber.Api import *
from Sequentum.ContentGrabber.Commands import *

def InitializeAgent(args):
if(args.IsDebug):
return True
# Set the file path and download folder if we're exporting to CSV
elif(args.Agent.ExportDestination.ExportTargetType == ExportTargetType.Csv):
if(args.GlobalData.HasData("CsvFilePath")):
args.Agent.ExportDestination.CsvExport.UseDefaultPath = False
args.Agent.ExportDestination.CsvExport.FilePath = args.GlobalData.GetString("CsvFilePath")

elif (args.GlobalData.HasData("DownloadFolder")):
args.Agent.ExportDestination.UseDefaultDownloadPath = False
args.Agent.ExportDestination.DownloadDirectoryName = args.GlobalData.GetString("DownloadFolder")

# Set the database connection if we're exporting to a SQL Server database
elif (args.Agent.ExportDestination.ExportTargetType == ExportTargetType.SqlServer):
if (args.GlobalData.HasData("DatabaseName")):
args.Agent.ExportDestination.DatabaseExport.DatabaseConnectionName = args.GlobalData.GetString("DatabaseName")


#Set the database connection and SQL for the input data provider in the Agent command elif (args.GlobalData.HasData("DatabaseName") and args.GlobalData.HasData("SQL")):
args.Agent.DataProvider.DatabaseProvider.DatabaseConnectionName = args.GlobalData.GetString("DatabaseName")
args.Agent.DataProvider.DatabaseProvider.Sql = args.GlobalData.GetString("SQL")
return True

using System;
using Sequentum.ContentGrabber.Api;
using Sequentum.ContentGrabber.Commands;
public class Script
{
public static bool InitializeAgent(AgentInitializationArguments args)
{
//Don't make changes to the agent while debugging
if(args.IsDebug)
return true;
//Set the file path and download folder if we're exporting to CSV
if(args.Agent.ExportDestination.ExportTargetType == ExportTargetType.Csv)
{
if(args.GlobalData.HasData("CsvFilePath"))
{
args.Agent.ExportDestination.CsvExport.UseDefaultPath = false;
args.Agent.ExportDestination.CsvExport.FilePath = args.GlobalData.GetString("CsvFilePath");
}

if(args.GlobalData.HasData("DownloadFolder"))
{
args.Agent.ExportDestination.UseDefaultDownloadPath = false;
args.Agent.ExportDestination.DownloadDirectoryName = args.GlobalData.GetString("DownloadFolder");
}
}

//Set the database connection if we're exporting to a SQL Server database
else if(args.Agent.ExportDestination.ExportTargetType == ExportTargetType.SqlServer)
{
if(args.GlobalData.HasData("DatabaseName"))
{
args.Agent.ExportDestination.DatabaseExport.DatabaseConnectionName = args.GlobalData.GetString("DatabaseName");
}
}

//Set the database connection and SQL for the input data provider in the Agent command
if(args.GlobalData.HasData("DatabaseName") && args.GlobalData.HasData("SQL"))
{
args.Agent.DataProvider.DatabaseProvider.DatabaseConnectionName = args.GlobalData.GetString("DatabaseName");
args.Agent.DataProvider.DatabaseProvider.Sql = args.GlobalData.GetString("SQL");
}

return true;
}
}

If you continue an agent, the initialization script is not run, but any changes that were made to global data or the agent are retained.

NOTE: The script must have a static method with the following signature:

public static bool InitializeAgent(AgentInitializationArguments args)

All logging in an agent initialization script is always written to file and never to database. This is because the script runs before the agent starts, and the internal database is not available at that time.

An instance of the AgentInitializationArguments class is provided by Sequentum Enterprise and has the following functions and properties:

Property or Function

Description

Agent

The current agent that is executing the script.

ScriptUtils ScriptUtilities

A script utility class with helper methods. See Script Utilities for more information.

bool IsDebug

True if the agent is running in debug mode.

IInputData InputDataCache

All input data available to the agent command.

void WriteDebug(string debugMessage, DebugMessageType messageType = DebugMessageType.Information)

Writes log information to the agent log. This method has no effect if agent logging is disabled, or if called during design time.

void WriteDebug(string debugMessage, bool showMessageInDesignMode, DebugMessageType messageType = DebugMessageType.Information)

Writes log information to the agent log. This method has no effect if agent logging is disabled, or if called during design time.

void Notify(bool alwaysNotify)

Triggers notification at the end of an agent run. If alwaysNotify is set to false, this method only triggers a notification if the agent has been configured to send notifications on critical errors.

void Notify(string message, bool alwaysNotify)

Triggers notification at the end of an agent run, and adds the message to the notification email. If alwaysNotify is set to false, this method only triggers a notification if the agent has been configured to send notifications on critical errors.

GlobalDataDictionary GlobalData

Global data dictionary that can be used to store data that needs to be available in all scripts and after agent restarts.

Input Parameters are also stored in this dictionary.

IConnection GetDatabaseConnection(string connectionName)

Returns the specified database connection. The database connection must have been previously defined for the agent or be a shared connection for all agents on the computer. Your script is responsible for opening and closing the connection by calling the OpenDatabase and CloseDatabase methods.

 

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.