Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
agent.php
1<?php
2
4
11
12Loc::loadMessages(__FILE__);
13
19class Agent
20{
34 public static function start($feedType, $exportId, $startPosition = "", $once = false, $execNumber = 1)
35 {
36 if (empty($exportId))
37 throw new ArgumentNullException('exportId');
38 $exportId = \EscapePHPString($exportId);
39
40 if (!in_array($feedType, Vk::getExportTypes()))
41 throw new ArgumentOutOfRangeException('feedType');
42
43 $result = "";
44 $vk = Vk::getInstance();
45 $vk->log(
46 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
47 "VK_AGENT__FEED_STARTED",
48 'FEED_' . $feedType,
49 "VKontakte export of " . $feedType . " started from agent. Export profile: " . $exportId . ", start position: " . $startPosition
50 );
51
52 try
53 {
54// if we run ALL export - first we must add ALBUMS. After this we create PRODUCTS agent
55 if ($feedType == 'ALL')
56 $feedTypeCurr = 'ALBUMS';
57 else
58 $feedTypeCurr = $feedType;
59
60 $timelimit = $vk->getTimelimit($exportId);
61 $vkFeed = Manager::createFeed($feedTypeCurr, $exportId, $timelimit, $startPosition);
62 $vkFeed->processData($exportId);
63 }
64
65 catch (TimeIsOverException $e)
66 {
67 $endPosition = $e->getEndPosition();
68// control of slow export
69 if ($startPosition == $endPosition)
70 {
71 $execNumber++;
72 if ($execNumber >= 3)
73 {
74 \CAdminNotify::Add(array(
75 'MESSAGE' => Loc::getMessage("SALE_VK__TOO_MUCH_TIMES_NOTIFY"),
76 'MODULE_ID' => 'sale',
77 'TAG' => 'vk_agent_much_times_notify',
78 'NOTIFY_TYPE' => \CAdminNotify::TYPE_ERROR,
79 ));
80 }
81 }
82 else
83 {
84 $execNumber = 1;
85 }
86 $result = self::createAgentNameForAdd($feedType, $exportId, $endPosition, $once, $execNumber);
87 $vk->log(
88 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
89 "VK_AGENT__FEED_TIMELIMIT",
90 'FEED_' . $feedType,
91 "VKontakte export of " . $feedType . " for profile " . $exportId . " takes too long and was finished at position '" . $startPosition . "'. To continue will be create agent."
92 );
93 }
94
95 catch (ExecuteException $e)
96 {
97 $msg = $e->getFullMessage() ? $e->getFullMessage() : Loc::getMessage("SALE_VK__UNKNOWN_ERROR");
98 $vk->log(TradingPlatform\Logger::LOG_LEVEL_ERROR, "VK_FEED__FEED_ERRORS", 'FEED_' . $feedType, $msg);
99 }
100
101 catch (\Exception $e)
102 {
103 $vk->log(
104 TradingPlatform\Logger::LOG_LEVEL_ERROR,
105 "VK_AGENT__FEED_ERRORS", 'FEED_' . $feedType,
106 "VKontakte export of " . $feedType . " for profile " . $exportId . " finished with some errors. " .
107 $e->getMessage()
108 );
109 }
110
111// if ALL export - we catch end of one cycle and run next type (ALBUMS => PRODUCTS)
112// if ALBUM part end normal (not with timer) - run agent to PRODUCTS export
113 if ($feedType == 'ALL' && !isset($endPosition))
114 {
115// for ALL export we open PRODUCTS journal after ALBUMS export end. Need for correctly show statistic to user
116 $journal = new Journal($exportId, 'PRODUCTS');
117 $journal->start();
118
119 $result = self::createAgentNameForAdd('PRODUCTS', $exportId, '', $once);
120 $vk->log(
121 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
122 "VK_FEED__FEED_ALBUM_PART_FINISH",
123 'FEED_' . $feedType,
124 "VKontakte export of ALBUMS for profile " . $exportId . " was finished successfull. PRODUCTS export will be continue on agent."
125 );
126 }
127
128// all OK - create new agent with null start position
129 elseif (strlen($result) <= 0 && !$once)
130 {
131 $result = self::createAgentNameForAdd($feedType, $exportId, "", $once);
132 }
133
134 $vk->log(
135 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
136 "VK_AGENT__FEED_FINISH",
137 'FEED_' . $feedType,
138 "VKontakte export of " . $feedType . " for profile " . $exportId . " was finished."
139 );
140 return $result;
141 }
142
143
155 public static function add($feedType, $exportId, $startPosition = "", $interval, $once = false)
156 {
157 if ($interval <= 0)
158 return 0;
159
160 if (empty($exportId))
161 throw new ArgumentNullException('exportId');
162
163 $exportId = \EscapePHPString($exportId);
164
165 $timeToStart = ConvertTimeStamp(strtotime(date('Y-m-d H:i:s', time() + $interval)), 'FULL');
166
167 $result = \CAgent::AddAgent(
168 self::createAgentNameForAdd($feedType, $exportId, $startPosition, $once),
169 'sale',
170 "N",
171 $interval,
172 $timeToStart,
173 "Y",
174 $timeToStart
175 );
176
177 if ($result)
178 {
179 $vk = Vk::getInstance();
180 $vk->log(
181 TradingPlatform\Logger::LOG_LEVEL_DEBUG,
182 "VK_AGENT__NEW_AGENT",
183 'FEED_' . $feedType,
184 "New agent was crated for VKontakte export " . $feedType . ". Agent ID: " . $result . "."
185 );
186 }
187
188 return $result;
189 }
190
201 protected static function createAgentNameForAdd($feedType, $exportId, $startPosition, $once = false, $execNumber = 1)
202 {
203 return 'Bitrix\Sale\TradingPlatform\Vk\Agent::start("' . $feedType . '","' . $exportId . '","' . $startPosition . '",' . ($once ? 'true' : 'false') . ',' . $execNumber . ');';
204 }
205
206
217 public static function update($exportId, $feedType, $interval, $once = false)
218 {
219 $result = false;
220 $interval = intval($interval);
221
222// check existing AGENTS
223 $dbRes = \CAgent::GetList(
224 array(),
225 array(
226 'NAME' => self::createAgentNameForAdd($feedType, $exportId, "", $once),
227 )
228 );
229
230
231// current agent existing - UPDATE
232 if ($agent = $dbRes->Fetch())
233 {
234 if ($interval <= 0)
235 {
236 self::deleteAgent($agent["ID"]);
237 }
238
239 else
240 {
241 \CAgent::Update(
242 $agent["ID"],
243 array('AGENT_INTERVAL' => $interval,)
244 );
245
246 $result = $agent["ID"];
247 }
248 }
249
250// agent not exist - CREATE
251 else
252 {
253 if ($interval > 0)
254 $result = self::add($feedType, $exportId, "", $interval, $once);
255 }
256
257 return $result;
258 }
259
260
264 public static function deleteAll()
265 {
266 $vk = Vk::getInstance();
267 $settings = $vk->getSettings();
268
269 foreach ($settings as $exportSettings)
270 {
271 self::deleteAgent($exportSettings["AGENT"]["ID"]);
272 }
273 }
274
275
279 public static function deleteAgent($agentId)
280 {
281 return \CAgent::Delete($agentId);
282 }
283
284
293 public static function getExistingOnceAgent($feedType, $exportId)
294 {
295 $dbRes = \CAgent::GetList(
296 array(),
297 array(
298 'NAME' => self::createOnceAgentName($feedType, $exportId),
299 )
300 );
301
302 $agents = array();
303 while ($agent = $dbRes->Fetch())
304 $agents[$agent["ID"]] = $agent["ID"];
305
306 return $agents;
307 }
308
309
318 public static function getRunningPereodicalAgents($feedType, $exportId)
319 {
320 $dbRes = \CAgent::GetList(
321 array(),
322 array(
323 'NAME' => self::createRunningPereodicalAgentName($feedType, $exportId),
324 )
325 );
326
327 $agents = array();
328 while ($agent = $dbRes->Fetch())
329 $agents[$agent["ID"]] = $agent["ID"];
330
331 return $agents;
332 }
333
340 protected static function createOnceAgentName($feedType, $exportId)
341 {
342 return 'Bitrix\Sale\TradingPlatform\Vk\Agent::start("' . $feedType . '","' . $exportId . '",%true%';
343 }
344
345
353 protected static function createRunningPereodicalAgentName($feedType, $exportId)
354 {
355 return 'Bitrix\Sale\TradingPlatform\Vk\Agent::start("' . $feedType . '","' . $exportId . '","_%"%';
356 }
357}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static createRunningPereodicalAgentName($feedType, $exportId)
Definition agent.php:353
static add($feedType, $exportId, $startPosition="", $interval, $once=false)
Definition agent.php:155
static getExistingOnceAgent($feedType, $exportId)
Definition agent.php:293
static createOnceAgentName($feedType, $exportId)
Definition agent.php:340
static update($exportId, $feedType, $interval, $once=false)
Definition agent.php:217
static start($feedType, $exportId, $startPosition="", $once=false, $execNumber=1)
Definition agent.php:34
static getRunningPereodicalAgents($feedType, $exportId)
Definition agent.php:318
static createAgentNameForAdd($feedType, $exportId, $startPosition, $once=false, $execNumber=1)
Definition agent.php:201