Views: 11564
Last Modified: 12.09.2014

Example of an Agent Creation

If you have to add agents dynamically, use API agents. If you have to add one or two agents, it is easier to add them manually.

An agent is created using the button Add an Agent on the page Settings > System settings > Agents:

Some comments on the parameters which meanings may be unclear from the name:

  • Date of the last run - if the agent is periodical, the time of the last run will be displayed when editing the agent;
  • Date and time of the next run – the work start time of the agent; if the agent is non-periodical, it will be executed once at this time;
  • Module - this module will be connected automatically when the agent is executed (in particular, the file /bitrix/modules/module ID/include.php will be connected) making it possible to use the functions of this module in the agent;
  • Agent function - name of the function that will be executed when the agent is launched (in our example, testAgent());
  • User ID – the agent will be launched following the hit of a specific user;

The function itself will look as follows:

function testAgent()
{
        mail('mail@gmail.com', 'Agent', 'Agent');
        return "testAgent();";
}

The function should be added to the file /bitrix/php_interface/init.php.

Simple Examples of Agents

<?
// add the agent of the module “Statistics”
CAgent::AddAgent(
    "CStatistic::CleanUpStatistics_2();", // function name
    "statistic",                          // module identifier
    "N",                                  // the agent is not critical to the amount of launches
    86400,                                // launch interval - 1 day
    "07.04.2005 20:03:26",                // the date of first check for launch
    "Y",                                  // the agent is active
    "07.04.2005 20:03:26",                // the date of first launch
    30);
?>
<?
// add the agent of the module “Technical Support”
CAgent::AddAgent(
    "CTicket::AutoClose();",  // function name
    "support",                // module identifier
    "N",                      // the agent is not critical to the amount of launches
    86400,                    // launch interval - 1 day
    "",                       // the date of first check - current
    "Y",                      // the agent is active
    "",                       // the date of first launch - current
    30);
?>
<?
// add arbitrary agent which does not belong to an module
CAgent::AddAgent("My_Agent_Function();");
?>

<?
// file /bitrix/php_interface/init.php

function My_Agent_Function()
{
   // perform any actions
   return "My_Agent_Function();";
}
?>
<?
// add an arbitrary agent belonging to the module
// with the identifier my_module

CAgent::AddAgent(
   "CMyModule::Agent007(1)", 
   "my_module", 
   "Y", 
    86400);
?>

<?
// this agent will be launched exactly 7 times with a frequency of once per 24 hours
// after that it will be deleted from the table of agents.

Class CMyModule
{
   function Agent007($cnt=1)
   {
      echo "Hello!";
      if($cnt>=7)
         return "";
      return "CMyModule::Agent007(".($cnt+1).")";
   }
}
?>



Courses developed by Bitrix24