Създадох команда в Symfony 4, която работи добре, когато се изпълнява от CLI, но не се изпълнява, когато се изпълнява от cron. Командата на конзолата се изпълнява и не се генерират грешки.
Дори хвърлих един в execute
и той не се проваля/греши:
public function execute(InputInterface $input, OutputInterface $output): void
{
throw new \Exception('I am doing something');
}
Пълната ми команда изглежда така и е свързана автоматично:
<?php declare(strict_types = 1);
namespace CRMInterface\Command\Customer;
use CRMInterface\Service\Customer\CustomerSyncService;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CustomerSyncCommand extends Command
{
const COMMAND_NAME = 'crm:sync:customer';
/**
* @var CustomerSyncService
*/
private $syncService;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param CustomerSyncService $syncService
* @param LoggerInterface $logger
*/
public function __construct(CustomerSyncService $syncService, LoggerInterface $logger)
{
parent::__construct(self::COMMAND_NAME);
$this->syncService = $syncService;
$this->logger = $logger;
}
protected function configure()
{
$this
->setName(self::COMMAND_NAME)
->setDescription('Processes outstanding portal sync tasks');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
public function execute(InputInterface $input, OutputInterface $output): void
{
$this->logger->info('Syncing customers...');
try {
$this->syncService->sync();
$this->logger->info('Customer sync complete.');
} catch (\Exception $e) {
$this->logger->error('Customer sync failed: ' . $e->getMessage());
}
}
}
Моята cron работа е както следва:
*/3 * * * * www-data cd /var/www/html && /usr/local/bin/php bin/console crm:sync:customer --env=prod
Тази настройка работи в приложение Symfony 3 и приложение Symfony 2.8, което изпълнявам, но не и с 4, и това ме дразни.
Моят bin/console
е както следва - извадих нещата, свързани с APP_ENV, защото бяха излишни в моя случай и се проваляха поради липсата на env vars в cron.
#!/usr/bin/env php
<?php
use CRMInterface\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
set_time_limit(0);
require __DIR__.'/../vendor/autoload.php';
if (!class_exists(Application::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}
$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], $_ENV['APP_ENV'] ?? 'dev');
$debug = ($_ENV['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption(['--no-debug', '']);
if ($debug) {
umask(0000);
if (class_exists(Debug::class)) {
Debug::enable();
}
}
$kernel = new Kernel($env, $debug);
$application = new Application($kernel);
$application->run($input);
Може ли някой да ме насочи в правилната посока защо командата се изпълнява, но не започва да се изпълнява?
Това е почти като че ли просто изпълнява bin/console
без командата... може ли да е нещо общо с мързеливото зареждане?
bin/console crm:sync:customer --env=prod
ръчно? - person Tomasz   schedule 24.10.2018grep syslog
за cron грешки, но виждам, че в реда на crontab имате(...) www-data cd && (...)
Вярвам, че там се проваля поради недефинирана команда - person Tomasz   schedule 24.10.2018*/3 * * * * /usr/local/bin/php /var/www/html/bin/console crm:sync:customer --env=prod
- person Tomasz   schedule 24.10.2018bin/console
добре, тъй като идва в мониторинга. Когато добавя израз в този файл за хвърляне на изключение, ако името на командата еcrm:sync:customer
, тогава това изключение се хвърля... - person OK sure   schedule 24.10.2018