将定时任务系统从lumen5.7升级到8.0后,执行php artisan xxx:xxx命令返回的错误码是1
class BuyOppCommand extends Command
{
protected $name = 'bidOpp:buyOpp';
protected $description = '竞价-购买商机';
public function handle()
{
return true;
}
}
这是我们在lumen5.7中常用的console的写法,这种写法在5.7中,artisan最后是以exit(0)结束.
但是在lumen8.0中,同样的写法,最后会以exit(1)结束.
在查询了console执行的源码过后,发现lumen5.7和8.0两个版本中对于返回码的处理方式有细微的不同
lumen5.7
$statusCode = $this->execute($input, $output);
return is_numeric($statusCode) ? (int) $statusCode : 0;
protected function execute(InputInterface $input, OutputInterface $output)
{
return $this->laravel->call([$this, 'handle']);
}
lumen8.0
$statusCode = $this->execute($input, $output);
return is_numeric($statusCode) ? (int) $statusCode : 0;
protected function execute(InputInterface $input, OutputInterface $output)
{
$method = method_exists($this, 'handle') ? 'handle' : '__invoke';
return (int) $this->laravel->call([$this, $method]);
}
从上面的代码中可以看出来,在lumen8.0中 如果handle中返回的是true,会被强制类型转换成int,true对应的int值为1,所以artisan执行完成后,就以exit(1)结束了.
如果要确保任务以exit(0)结束,可以考虑在handle中 return 0,或者修改源码, 但是不建议修改外部引用包的代码.