我有一个使用Laravel任务调度程序的任务,而不是完全激活。
它会触发调用名为Task::checkSpiders()
的公共方法。
这会命中一个API(工作正常),然后应该相应地更新本地DB。
如果我这样创建一条路线......
Route::get('/test', function () {
$test = Task::checkSpiders();
}
它运行得非常好。
当我从CRON
运行时,它会抛出这个错误......
#0 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(55): PDO->__construct('mysql:host=loca...', 'forge', '', Array)
#1 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=loca...', Array, Array)
#2 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(61): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#3 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
#4 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(920): call_user_func(Object(Closure))
#5 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(788): Illuminate\Database\Connection->getPdo()
#6 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(673): Illuminate\Database\Connection->reconnectIfMissingConnection()
#7 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(479): Illuminate\Database\Connection->run('update `tasks` ...', Array, Object(Closure))
#8 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php(422): Illuminate\Database\Connection->affectingStatement('update `tasks` ...', Array)
#9 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2072): Illuminate\Database\Connection->update('update `tasks` ...', Array)
#10 /var/www/laravel/app/Task.php(60): Illuminate\Database\Query\Builder->update(Array)
#11 /var/www/laravel/app/Task.php(85): App\Task::updateTask('a5b829767a6811e...', 'Finished', '2016-09-14 10:4...', '2016-09-14 11:5...')
#12 /var/www/laravel/app/Console/Kernel.php(34): App\Task::checkSpiders()
#13 [internal function]: App\Console\Kernel->App\Console\{closure}()
#14 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Object(Closure), Array)
#15 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Scheduling/CallbackEvent.php(61): Illuminate\Container\Container->call(Object(Closure), Array)
#16 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php(61): Illuminate\Console\Scheduling\CallbackEvent->run(Object(Reflexions\DockerLaravel\DockerApplication))
#17 [internal function]: Illuminate\Console\Scheduling\ScheduleRunCommand->fire()
#18 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array(Array, Array)
#19 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
#20 /var/www/laravel/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 /var/www/laravel/vendor/symfony/console/Application.php(794): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 /var/www/laravel/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Console\Scheduling\ScheduleRunCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 /var/www/laravel/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#25 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#26 /var/www/laravel/artisan(36): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#27 {main}
要插入数据库的代码(我认为不相关,但无论如何我都会加入..)
$tasks = DB::table('tasks')
->where('id', $id)
->whereNull('end')
->update([
'status' => $status,
'start' => $start,
'end' => $end,
]);
另外,我只是在Docker中使用Supervisord在Laravel中管理CRON。
任何帮助非常感谢!
答案 0 :(得分:1)
愚蠢的错误;
CRON在不同的用户下运行,因此从.env加载的环境变量不可用。
我通过记录CRON中运行的任务中的ENV变量来检查这一点。
像这样...
Log::info(json_encode(get_defined_vars()));
返回[]
一个空数组。
我已经将变量硬编码到配置中,并且一切都运行良好。
或者我可以使用...
在PHP中设置ENV变量(在其他地方使用它们)<?php
putenv("KEY=value");
?>