Some checks failed
Telegram Mini App Shop Builder / Compute version metadata (push) Has been cancelled
Telegram Mini App Shop Builder / Run Frontend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run Backend tests (push) Has been cancelled
Telegram Mini App Shop Builder / Run PHP_CodeSniffer (push) Has been cancelled
Telegram Mini App Shop Builder / Build module. (push) Has been cancelled
Telegram Mini App Shop Builder / release (push) Has been cancelled
59 lines
2.1 KiB
PHP
59 lines
2.1 KiB
PHP
<?php
|
||
|
||
namespace App\Handlers;
|
||
|
||
use Acme\ECommerceFramework\Config\Settings;
|
||
use Acme\ECommerceFramework\Http\Request;
|
||
use Acme\ECommerceFramework\Scheduler\SchedulerService;
|
||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
|
||
class CronHandler
|
||
{
|
||
private Settings $settings;
|
||
private SchedulerService $schedulerService;
|
||
|
||
public function __construct(Settings $settings, SchedulerService $schedulerService)
|
||
{
|
||
$this->settings = $settings;
|
||
$this->schedulerService = $schedulerService;
|
||
}
|
||
|
||
/**
|
||
* Запуск планировщика по HTTP (для cron-job.org и аналогов).
|
||
* Требует api_key в query, совпадающий с настройкой cron.api_key.
|
||
*/
|
||
public function runSchedule(Request $request): JsonResponse
|
||
{
|
||
$mode = $this->settings->get('cron.mode', 'disabled');
|
||
if ($mode !== 'cron_job_org') {
|
||
return new JsonResponse(['error' => 'Scheduler is not in cron-job.org mode'], Response::HTTP_FORBIDDEN);
|
||
}
|
||
|
||
$apiKey = $request->get('api_key', '');
|
||
$expectedKey = $this->settings->get('cron.api_key', '');
|
||
if ($expectedKey === '' || $apiKey === '' || !hash_equals($expectedKey, $apiKey)) {
|
||
return new JsonResponse(['error' => 'Invalid or missing API key'], Response::HTTP_UNAUTHORIZED);
|
||
}
|
||
|
||
// Увеличиваем лимит времени выполнения при запуске по HTTP, чтобы снизить риск timeout
|
||
$limit = 300; // 5 минут
|
||
if (function_exists('set_time_limit')) {
|
||
@set_time_limit($limit);
|
||
}
|
||
if (function_exists('ini_set')) {
|
||
@ini_set('max_execution_time', (string) $limit);
|
||
}
|
||
|
||
$result = $this->schedulerService->run(true);
|
||
$data = [
|
||
'success' => true,
|
||
'executed' => count($result->executed),
|
||
'failed' => count($result->failed),
|
||
'skipped' => count($result->skipped),
|
||
];
|
||
|
||
return new JsonResponse($data);
|
||
}
|
||
}
|