How to create an agent in your module?

Hello. I'm developing a module and I need during the installation of the module to create an agent. To create an agent using CAgent::AddAgent() in this method you need to pass the class and the function that will start the agent. Where you want to position them in the module structure?
June 14th 19 at 20:32
3 answers
June 14th 19 at 20:34
Solution
And in what actually a question? Do as the documentation

1) When the module is installed, add the agent:
\CAgent::AddAgent( "\\Your\\Module\\SuperClass::superAgent();", "your.module", "N", 10 * 24 * 3600, "", "Y");

where
"\\Your\\Module\\SuperClass::superAgent();" - the php code to be executed (semicolon at the end of strictly!!!)
"your.module" - the name of your module
"N" - frequency
10 * 24 * 3600 = frequency start
"first review date (leave empty)
"Y" is the activity of the agent

2) When removal - cleansing agents
\CAgent::RemoveModuleAgents("your.module");
3) If the files are located in the canons d7 ( /your.module/lib/superclass.php ), then nothing more needs to be done, if it is necessary to register include.php the autoloader for your class agent
please help me to understand
write
prntscr.com/hgwlfj
in include.php prntscr.com/hgwlq9
class prntscr.com/hgwmu5
and when you install the module writes
Fatal error: Call to undefined function MinPrice() in /home/bitrix/ext_www/bitrix.smsdesign.com.ua/bitrix/modules/main/classes/mysql/agent.php(161) : eval()'d code on line 1

moreover, when I inserted the class once in include.php without CModule::AddAutoloadClasses after the installation of the module agent is created, but not re-run - nikita.Stracke commented on June 14th 19 at 20:37
You do not understand a bit. Agent - a static function that is executed and returns either an empty string (nothing, false) or itself (a string) to execute via eval().

Ie file /modules/sms.shop/classes/smsshop.php:

class SmsShop
{
 public static function minPrice()
{

 // Some useful action

 return "";

 // If you need to call the function again 
 // i.e., not a single agent, and permanent
 // return "SmsShop::minPrice();"
}
}


And, accordingly, the agent should not be MinPrice (); and SmsShop::minPrice(); (pay attention to commas).

But I would use the new syntax (d7), if it allows.
In this case, You would be able to give understandable names.

Example: for a module to d7, the agent \Sms\Shop\Agent::checkMinPrice(); (where in this case the Sms code and vendor, Shop - the name of the module Agent class name), would have to be empty include.php and only the file /modules/sms.shop/lib/agent.php formatted according to the rule:

namespace Sms\Shop;

class Agent
{
 public static function minPrice()
{

 // Some useful action

 return "";

 // If you need to call the function again 
 // i.e., not a single agent, and permanent
 // return "\\Sms\\Shop\\Agent::minPrice();"
}
}


Note: our agents have limitations, remember this. - Roel.OConnell30 commented on June 14th 19 at 20:40
June 14th 19 at 20:36
Not important in fact. Check just to function - the agent was available when the module is connected.
June 14th 19 at 20:38
Where you want to position them in the module structure?

At least include.php.
If placed in include.php the function will run on each hit. I need once a week. - nikita.Stracke commented on June 14th 19 at 20:41
the location function does not mean that it will be called when the hit. That's if it's cause - then Yes - Roel.OConnell30 commented on June 14th 19 at 20:44
,
If placed in include.php the function will
available when you connect your module. - Solon.Howell commented on June 14th 19 at 20:47

Find more questions by tags 1C-Bitrix