【发布时间】:2017-08-05 18:25:40
【问题描述】:
我正在使用带有 pheanstalk php 库的 Symfony 3 框架。我使用 Linux Debian Jesse 在服务器上运行该应用程序。工作创建工作正常,如果从终端运行工作人员,它会正常工作。但是当我将命令添加到 crontab 时,我发现该命令不起作用。 /var/main/user 中没有任何登录来帮助我进行调试。如果有任何帮助,我将非常高兴。
这是我的工作命令(仅执行功能):
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("\n<info>Beanstalk worker service started</info>");
$tubes = $this->getContainer()->get('app.job_manager.power_plant')->getTubes();
if ($input->getOption('one-check')) {
// run once
foreach ($tubes as $tubeName) {
if ($tubeName != "default") {
$this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
}
}
$output->writeln("\n<info>Beanstalk worker completed check and stoped</info>");
} else {
// run forever
set_time_limit(0);
max_execution_time(0);
while (1) {
foreach ($tubes as $tubeName) {
if ($tubeName != "default") {
$this->getContainer()->get('app.queue_manager')->fetchQueue($tubeName);
}
}
}
$output->writeln("\n<info>Beanstalk worker stoped</info>");
}
}
这是我的 app.queue_manager 函数,用于从队列中获取作业并运行命令:
public function fetchQueue($tubeName)
{
if ($this->pheanstalk->getConnection()->isServiceListening()) {
while (true === is_object($job = $this->pheanstalk->watch($tubeName)->ignore('default')->reserve(self::WATCH_TIMEOUT))) {
$data = json_decode($job->getData(), true);
$this->worker($data);
$this->pheanstalk->delete($job);
}
}
}
和工人运行命令
public function worker($data)
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$parameters = [];
$parameters['command'] = $data['command'];
foreach ($data['meta'] as $key => $param) {
$parameters[$key] = $param;
}
$input = new ArrayInput($parameters);
$output = new NullOutput();
return $application->run($input, $output);
}
这是我的 crontab 不起作用:
@reboot /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start
我创建了另一个可以正常工作的 cron 选项卡。它每 15 分钟工作一次,不同之处在于没有无限循环(while(1)),所以它只通过管道一次,然后完成。但这不是我想要的。我想要一直工作的无限循环,就像我用第一个 crontab 创建它一样:
*/15 * * * * /usr/bin/php /var/www/mose-base/bin/console beanstalk:worker:start --one-check
【问题讨论】:
-
将作业启动放在其他地方——比如 rc.local。根据我的阅读,@reboot 仅在重新启动时发生,而不是完全断电。
-
我也试过这个。但它是一样的。我看到命令与 ps -eLf 一起运行,但该命令没有运行,因为作业没有被删除和执行......
-
程序是否因为没有“输入”流而失败 - 即没有控制台可以打开/读取?
-
我个人不会使用你的命令!这个例子Beanstalk producer, consumer and worker command example in symfony 似乎正在做你想做的事情。另外据我所知,您可以直接复制+粘贴命令
JobProcessorCommand类而无需更改它。 -
然后在你的 crontab 中
*/15 * * * * cd /into/your/application/path && php app/console --env=prod beanstalk:worker:start ...... and your arguments
标签: php linux symfony crontab beanstalkd