1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Job.php
См. документацию.
1<?php
2
3namespace Bitrix\Seo\Sitemap;
4
5use Bitrix\Main\Result;
6use Bitrix\Main\SystemException;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Text\Converter;
9use Bitrix\Main\Type\DateTime;
10use Bitrix\Seo\Sitemap\Internals\JobTable;
11use Bitrix\Seo\Sitemap\Internals\SitemapTable;
12use Bitrix\Seo\Sitemap\Type\Step;
13
14Loc::loadMessages(__DIR__ . '/../../admin/seo_sitemap.php');
15
19class Job
20{
21 protected const AGENT_FUNCTION = 'doJobAgent';
22 protected const AGENT_INTERVAL = 1;
23 protected const AGENT_DELAY = 60;
24
25 protected const LOCK_MAX_INTERVAL = 300;
26
30 protected const PROGRESS_WIDTH = 500;
31
35 public const STATUS_REGISTER = 'R';
36 public const STATUS_PROCESS = 'P';
37 public const STATUS_FINISH = 'F';
38 public const STATUS_ERROR = 'E';
39
44 protected int $id;
45
50 protected int $sitemapId;
51
55 protected int $step;
56 protected array $state;
57 protected string $status;
58 protected string $statusMessage = '';
59
63 protected bool $isLocked = false;
65
70 protected function __construct(int $sitemapId)
71 {
72 $this->sitemapId = $sitemapId;
73
74 // init from table
76 if ($job)
77 {
78 $this->id = (int)$job['ID'];
79 $this->status = $job['STATUS'];
80 $this->statusMessage = $job['STATUS_MESSAGE'];
81 $this->step = (int)$job['STEP'];
82 $this->state = $job['STATE'] ?? [];
83
84 $this->isLocked = $job['RUNNING'] === 'Y';
85 $this->dateModify = $job['DATE_MODIFY'] ? new DateTime($job['DATE_MODIFY']) : null;
86
87 if (!self::checkSitemapExists($sitemapId))
88 {
89 $this->finish();
90
91 throw new SystemException('Sitemap for current job is not exists.');
92 }
93 }
94 else
95 {
96 throw new SystemException('Job for current sitemap is not exists.');
97 }
98 }
99
108 protected static function checkSitemapExists(int $sitemapId): bool
109 {
110 $sitemap = SitemapTable::query()
111 ->setSelect(['ID'])
112 ->where('ID', $sitemapId)
113 ->exec()
114 ->fetch()
115 ;
116
117 return (bool)$sitemap;
118 }
119
125 public static function findJob(int $sitemapId): ?Job
126 {
127 try
128 {
130
131 return $job ? new self($sitemapId) : null;
132 }
133 catch (SystemException $e)
134 {
135 return null;
136 }
137 }
138
145 public static function addJob(int $sitemapId): ?Job
146 {
148 if ($exists)
149 {
150 return new self($sitemapId);
151 }
152
153 $res = JobTable::add(
154 [
155 'SITEMAP_ID' => $sitemapId,
156 'RUNNING' => 'N',
157 'STATUS' => Job::STATUS_REGISTER,
158 'STATUS_MESSAGE' => '',
159 'STEP' => Step::getFirstStep(),
160 'STATE' => [],
161 ]
162 );
163
164 if ($res->isSuccess())
165 {
166 return new self($sitemapId);
167 }
168
169 return null;
170 }
171
177 protected static function getDataBySitemap(int $sitemapId): ?array
178 {
179 if ($sitemapId > 0)
180 {
181 $job = JobTable::query()
182 ->setSelect(['ID', 'RUNNING', 'STATUS', 'STATUS_MESSAGE', 'STEP', 'STATE', 'DATE_MODIFY'])
183 ->where('SITEMAP_ID', $sitemapId)
184 ->exec()
185 ->fetch()
186 ;
187
188 return $job ?: null;
189 }
190
191 return null;
192 }
193
199 public static function markToRegenerate(int $sitemapId): bool
200 {
201 try
202 {
203 if ($sitemapId > 0)
204 {
205 $existsJob = self::findJob($sitemapId);
206 if (!$existsJob)
207 {
209 }
210
212
213 return (bool)self::addAgent($sitemapId);
214 }
215 }
216 catch (SystemException $e)
217 {
218 return false;
219 }
220
221 return false;
222 }
223
224 public static function clearBySitemap(int $sitemapId)
225 {
227 $job?->finish();
228 }
229
235 protected static function findAgent(int $sitemapId): ?array
236 {
237 $funcName = self::getAgentName($sitemapId);
238 $res = \CAgent::getList(
239 [],
240 [
241 'MODULE_ID' => 'seo',
242 'NAME' => $funcName,
243 ]
244 );
245 $exists = $res->Fetch();
246
247 return $exists ?: null;
248 }
249
255 protected static function addAgent(int $sitemapId): int
256 {
257 $funcName = self::getAgentName($sitemapId);
258 $nextExec = \ConvertTimeStamp(time() + \CTimeZone::GetOffset() + self::AGENT_DELAY, "FULL");
259
260 return \CAgent::addAgent(
261 $funcName,
262 'seo',
263 'N',
264 self::AGENT_INTERVAL,
265 '',
266 'Y',
267 $nextExec
268 );
269 }
270
276 protected static function deleteAgent(int $sitemapId): bool
277 {
278 $agent = self::findAgent($sitemapId);
279 if ($agent && $agent['RUNNING'] === 'N')
280 {
281 return \CAgent::Delete($agent['ID']);
282 }
283
284 return true;
285 }
286
287 protected static function getAgentName(int $sitemapId): string
288 {
289 return __CLASS__ . '::' . self::AGENT_FUNCTION . '(' . $sitemapId . ');';
290 }
291
297 public static function doJobAgent(int $sitemapId): string
298 {
300
301 if ($job)
302 {
303 $result = $job->doStep();
304 if ($result->isSuccess())
305 {
307 }
308 }
309
310 return '';
311 }
312
317 public function doStep(): Result
318 {
319 $result = new Result();
320
321 // skip if job running now
322 if (
323 $this->checkLock()
324 || !$this->lock()
325 )
326 {
327 return $result;
328 }
329
330 $generator =
331 (new Generator($this->sitemapId))
332 ->init($this->step, $this->state)
333 ;
334 if ($generator->run())
335 {
336 $this->state = $generator->getState();
337 $this->statusMessage = $generator->getStatusMessage();
338 $this->step = $generator->getStep();
339
340 if ($this->step <= Step::STEPS[Step::STEP_INIT])
341 {
342 $this->status = self::STATUS_REGISTER;
343 }
344 elseif ($this->step >= Step::STEPS[Step::STEP_INDEX])
345 {
346 $this->status = self::STATUS_FINISH;
347 $this->finish();
348 }
349 else
350 {
351 $this->status = self::STATUS_PROCESS;
352 }
353
354 $this->save();
355 }
356 $this->unlock();
357
358 return $result;
359 }
360
361 protected function lock(): bool
362 {
363 $res = JobTable::update(
364 $this->id,
365 [
366 'RUNNING' => 'Y',
367 ]
368 );
369 if ($res->isSuccess())
370 {
371 $this->isLocked = true;
372
373 return true;
374 }
375
376 return false;
377 }
378
379 protected function unlock(): bool
380 {
381 $res = JobTable::update(
382 $this->id,
383 [
384 'RUNNING' => 'N',
385 ]
386 );
387
388 if ($res->isSuccess())
389 {
390 $this->isLocked = false;
391
392 return true;
393 }
394
395 return false;
396 }
397
398 protected function checkLock(): bool
399 {
400 if ($this->isLocked)
401 {
402 if ($this->dateModify)
403 {
404 $secondsDiff = (new DateTime())->getDiff($this->dateModify)->s;
405 if ($secondsDiff > self::LOCK_MAX_INTERVAL)
406 {
407 return !$this->unlock();
408 }
409 }
410
411 return true;
412 }
413
414 return false;
415 }
416
421 protected function save(): bool
422 {
423 $res = JobTable::update(
424 $this->id,
425 [
426 'STATUS' => $this->status,
427 'STATUS_MESSAGE' => $this->statusMessage,
428 'STATE' => $this->state,
429 'STEP' => $this->step,
430 ]
431 );
432
433 return $res->isSuccess();
434 }
435
440 protected function finish(): bool
441 {
442 self::deleteAgent($this->sitemapId);
443
444 $res = JobTable::delete(
445 $this->id
446 );
447
448 return $res->isSuccess();
449 }
450
455 public function getData(): array
456 {
457 return [
458 'status' => $this->status,
459 'statusMessage' => $this->statusMessage,
460 'formattedStatusMessage' => $this->getFormattedStatusMessage(),
461 'step' => $this->step,
462 ];
463 }
464
465 protected function getFormattedStatusMessage(): string
466 {
467 require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/interface/admin_lib.php');
468
469 $title = Loc::getMessage('SEO_SITEMAP_RUN_TITLE') . " (ID {$this->sitemapId})";
470 if ($this->step < Step::getLastStep())
471 {
472 $msg = new \CAdminMessage([
473 "TYPE" => "PROGRESS",
474 "HTML" => true,
475 "MESSAGE" => $title,
476 "DETAILS" => "#PROGRESS_BAR#<div style=\"width: "
477 . self::PROGRESS_WIDTH
478 . "px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-top: 20px;\">"
479 . Converter::getHtmlConverter()->encode($this->statusMessage)
480 . "</div>",
481 "PROGRESS_TOTAL" => 100,
482 "PROGRESS_VALUE" => $this->step,
483 "PROGRESS_TEMPLATE" => '#PROGRESS_PERCENT#',
484 "PROGRESS_WIDTH" => self::PROGRESS_WIDTH,
485 ]);
486 }
487 else
488 {
489 $msg = new \CAdminMessage([
490 "TYPE" => "OK",
491 "MESSAGE" => $title,
492 "DETAILS" => $this->statusMessage,
493 ]);
494 }
495
496 return $msg->show();
497 }
498}
static getHtmlConverter()
Определения converter.php:13
Определения Job.php:20
lock()
Определения Job.php:361
const STATUS_ERROR
Определения Job.php:38
const STATUS_REGISTER
Определения Job.php:35
static deleteAgent(int $sitemapId)
Определения Job.php:276
static findJob(int $sitemapId)
Определения Job.php:125
const STATUS_FINISH
Определения Job.php:37
static addJob(int $sitemapId)
Определения Job.php:145
int $sitemapId
Определения Job.php:50
int $id
Определения Job.php:44
checkLock()
Определения Job.php:398
string $status
Определения Job.php:57
static getDataBySitemap(int $sitemapId)
Определения Job.php:177
static addAgent(int $sitemapId)
Определения Job.php:255
int $step
Определения Job.php:55
static getAgentName(int $sitemapId)
Определения Job.php:287
getData()
Определения Job.php:455
static doJobAgent(int $sitemapId)
Определения Job.php:297
finish()
Определения Job.php:440
__construct(int $sitemapId)
Определения Job.php:70
bool $isLocked
Определения Job.php:63
const AGENT_DELAY
Определения Job.php:23
static markToRegenerate(int $sitemapId)
Определения Job.php:199
getFormattedStatusMessage()
Определения Job.php:465
const STATUS_PROCESS
Определения Job.php:36
const AGENT_FUNCTION
Определения Job.php:21
const AGENT_INTERVAL
Определения Job.php:22
array $state
Определения Job.php:56
DateTime $dateModify
Определения Job.php:64
doStep()
Определения Job.php:317
string $statusMessage
Определения Job.php:58
static clearBySitemap(int $sitemapId)
Определения Job.php:224
const LOCK_MAX_INTERVAL
Определения Job.php:25
static findAgent(int $sitemapId)
Определения Job.php:235
static checkSitemapExists(int $sitemapId)
Определения Job.php:108
const PROGRESS_WIDTH
Определения Job.php:30
unlock()
Определения Job.php:379
save()
Определения Job.php:421
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$title
Определения pdf.php:123
getDiff($X, $Y)
Определения include.php:190