laravel的console问题

将定时任务系统从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,或者修改源码, 但是不建议修改外部引用包的代码.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值