Agent Initialization Scripts

<< <%SKIN-STRTRANS-SYNTOC%> >>

Navigation:  Scripting >

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:

initializationScript

 

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

 

The following example initializes a global counter.

 

using System;

using Sequentum.ContentGrabber.Api;

public class Script

{        

 public static bool InitializeAgent(AgentInitializationArguments 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.

 

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 was made to global data or the agent are 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 Content Grabber 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.