Php 池逻辑未按计划执行(Laravel 8.20.1)
我的控制器中有一个功能,使用池逻辑。当通过路由调用函数时,一切都能完美执行;但是,当从计划(在app/Console/Kernel.php中)调用时,池不会执行 在使用MAMP进行本地测试时,相同的代码不会出现问题,但不会在生产环境中运行 以下是我的代码的简化版本: HomeController.phpPhp 池逻辑未按计划执行(Laravel 8.20.1),php,laravel,pool,Php,Laravel,Pool,我的控制器中有一个功能,使用池逻辑。当通过路由调用函数时,一切都能完美执行;但是,当从计划(在app/Console/Kernel.php中)调用时,池不会执行 在使用MAMP进行本地测试时,相同的代码不会出现问题,但不会在生产环境中运行 以下是我的代码的简化版本: HomeController.php namespace App\Http\Controllers; use Illuminate\Http\Request; use Spatie\Async\Pool; use Server;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Async\Pool;
use Server;
use Ping;
use Setting;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function executePing()
{
$servers = Server::where('deleted', 0)->get();
$concurrency = Setting::where('type', 'concurrency')->value('value');
$ping_timeout = Setting::where('type', 'ping_timeout')->value('value');
$pool = Pool::create()->concurrency($concurrency)->timeout($ping_timeout);
//Save Pings
foreach ($servers as $server)
{
echo "Starting Pool...";
$pool->async(function () use ($server) {
$new_ping = new Ping; //Line added after editing my question
//Execute Ping
echo "Ping Executed.";
})->then(function ($output) {
//Handle success
})->catch(function (Throwable $exception) {
//Handle exception
});
}
await($pool);
$data['data'] = "Executed";
echo json_encode($data);
}
}
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
//
];
protected function schedule(Schedule $schedule)
{
$schedule->call('App\Http\Controllers\HomeController@executePing')->everyMinute()->runInBackground()->evenInMaintenanceMode();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php' );
}
}
app/Console/Kernel.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Async\Pool;
use Server;
use Ping;
use Setting;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function executePing()
{
$servers = Server::where('deleted', 0)->get();
$concurrency = Setting::where('type', 'concurrency')->value('value');
$ping_timeout = Setting::where('type', 'ping_timeout')->value('value');
$pool = Pool::create()->concurrency($concurrency)->timeout($ping_timeout);
//Save Pings
foreach ($servers as $server)
{
echo "Starting Pool...";
$pool->async(function () use ($server) {
$new_ping = new Ping; //Line added after editing my question
//Execute Ping
echo "Ping Executed.";
})->then(function ($output) {
//Handle success
})->catch(function (Throwable $exception) {
//Handle exception
});
}
await($pool);
$data['data'] = "Executed";
echo json_encode($data);
}
}
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
//
];
protected function schedule(Schedule $schedule)
{
$schedule->call('App\Http\Controllers\HomeController@executePing')->everyMinute()->runInBackground()->evenInMaintenanceMode();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php' );
}
}
运行php artisan计划时的输出:运行
Running scheduled command: App\Http\Controllers\HomeController@executePing
Starting Pool...
因为我使用的是外部库,所以很难找到类似的问题。如果有人能知道为什么会发生这种情况,甚至能找到我使用的图书馆的替代品,我们将不胜感激
重要编辑:
我注意到日志文件缺少权限(愚蠢的错误)。我修正了它,得到了错误。
发生的情况是,当从池中调用我的所有类时,它们都无法被识别。我不能声明任何新对象或执行数据库查询。在上面的示例中(此更新后编辑),我得到:
直言不讳,该扩展是否已安装在生产环境中?是的,它已在php中安装并启用。注意:我编辑了我的问题,并提供了更多详细信息。我犯了一个愚蠢的错误,没有检查日志文件的写入权限。现在我有一个错误,简单地说:当从schedule(Kernel.php)运行时,“池中”的所有类都无法识别。