繁体   English   中英

Symfony 命令在由 cron 运行时无法执行

[英]Symfony command not reaching execution when run by cron

我在 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 相关的东西,因为在我的情况下它是多余的,并且由于 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 ……这可能与延迟加载有关吗?

对于那些在执行命令时遇到类似问题的人,请检查 cron 是否可以访问您的应用程序使用的所有环境变量。

在这种情况下,有一个异常被抛出,但被 Symfony Application类捕获。 Environment variable not found: "ELASTICSEARCH_INDEX". . 不幸的是,它就像跑了一样继续前进。 (可能会在 repo 上产生问题)。

对于基于 Ubuntu 的 apache 容器上的 Docker,解决方案相当简单 - 在入口点脚本中添加一行以将环境变量写入/etc/environment文件:

FROM php:7.2
EXPOSE 80 443
CMD ["sh", "./.docker/run.sh"]

运行.sh:

#!/usr/bin/env bash

printenv | grep -v "no_proxy" >> /etc/environment \
    && /etc/init.d/cron start \
    && apache2-foreground

与上一个答案类似,但请确保将 cron 作业添加到正确的用户。

示例: crontab -u www-data -e 通过更新或添加编辑 cron 作业

*/3 * * * * /usr/local/bin/php /var/www/html/bin/console crm:sync:customer --env=prod

您可以在命令上添加一些测试,例如以下内容

0 * * * * 如果 [ -f /var/www/html/bin/console ]; 然后 /var/www/html/bin/console crm:sync:customer --env=prod >> /var/log/symfony/crm.log; 否则回声'控制台未找到'

/var/log/symfony/crm.log; fi >/dev/null 2>&1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM