在 Lumen/Laravel 中安排作业:我这样做正确吗?
Scheduling Jobs in Lumen/Laravel: Am I doing this correctly?
根据以下教程 (https://laravel.com/docs/5.5/scheduling),我想出了以下脚本来在我的 Lumen 应用程序中安排作业:
namespace App\Console;
use App\Jobs\ClearAccessLog;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
use App\Jobs\Special\DeleteCache;
use App\Jobs\Special\FetchRemoteArticles;
use App\Jobs\Special\FetchUpdateCategories;
use App\Jobs\Special\UpdateArticles;
use Illuminate\Support\Facades\Queue;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function() {
Queue::bulk([
new FetchUpdateCategories,
new FetchRemoteArticles,
new UpdateArticles,
new DeleteCache
],null,'specialqueue');
})->daily();
// listen to
$schedule->job(new ClearAccessLog())->weekly();
// listen to the helpdocs queue
$schedule->command('php artisan queue:work --timeout=0 --memory=4096 --queue=specialqueue')->everyFiveMinutes();
// listen to default jobs
$schedule->command('php artisan queue:work --timeout=0 --memory=4096')->daily();
}
}
根据我服务器的文档,我只需要像这样注册一个 cronjob:
crontab -e
然后添加:
* * * * * php /path-to-my-project/artisan schedule:run >> /dev/null 2>&1
无论如何我注意到如果我执行
php artisan queue:work --timeout=0 --memory=4096 --queue=specialqueue
通过 SSH 这会一直监听直到命令停止,因此我不确定调度程序是否会每五分钟启动一个新的后台任务,而其他任务仍在 运行?
我怎样才能只启动这个命令一次?一般来说,您是否看到一些明显的错误(我是这个主题的新手)?
您按计划启动队列工作人员是不正确的。队列工作者是守护进程,它监听是否有任何工作可以工作,如果没有,那么它将休眠直到下一个工作可用。您正在尝试做的是创建没有意义的无限队列工作人员。
您应该查看手册 Laravel Queues。如果你被允许安装 Supervisor,让 supervisor 控制你的 queue worker。 Supervisor 帮助您监控队列工作人员,在任何情况下它会因某些错误而失败,它会尝试重新启动。
否则,您应该通过ssh 在后台启动queue worker,并将该进程与当前用户分离。但是queue worker不会在任何故障的情况下自动重启。
根据以下教程 (https://laravel.com/docs/5.5/scheduling),我想出了以下脚本来在我的 Lumen 应用程序中安排作业:
namespace App\Console;
use App\Jobs\ClearAccessLog;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
use App\Jobs\Special\DeleteCache;
use App\Jobs\Special\FetchRemoteArticles;
use App\Jobs\Special\FetchUpdateCategories;
use App\Jobs\Special\UpdateArticles;
use Illuminate\Support\Facades\Queue;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function() {
Queue::bulk([
new FetchUpdateCategories,
new FetchRemoteArticles,
new UpdateArticles,
new DeleteCache
],null,'specialqueue');
})->daily();
// listen to
$schedule->job(new ClearAccessLog())->weekly();
// listen to the helpdocs queue
$schedule->command('php artisan queue:work --timeout=0 --memory=4096 --queue=specialqueue')->everyFiveMinutes();
// listen to default jobs
$schedule->command('php artisan queue:work --timeout=0 --memory=4096')->daily();
}
}
根据我服务器的文档,我只需要像这样注册一个 cronjob:
crontab -e
然后添加:
* * * * * php /path-to-my-project/artisan schedule:run >> /dev/null 2>&1
无论如何我注意到如果我执行
php artisan queue:work --timeout=0 --memory=4096 --queue=specialqueue
通过 SSH 这会一直监听直到命令停止,因此我不确定调度程序是否会每五分钟启动一个新的后台任务,而其他任务仍在 运行?
我怎样才能只启动这个命令一次?一般来说,您是否看到一些明显的错误(我是这个主题的新手)?
您按计划启动队列工作人员是不正确的。队列工作者是守护进程,它监听是否有任何工作可以工作,如果没有,那么它将休眠直到下一个工作可用。您正在尝试做的是创建没有意义的无限队列工作人员。
您应该查看手册 Laravel Queues。如果你被允许安装 Supervisor,让 supervisor 控制你的 queue worker。 Supervisor 帮助您监控队列工作人员,在任何情况下它会因某些错误而失败,它会尝试重新启动。
否则,您应该通过ssh 在后台启动queue worker,并将该进程与当前用户分离。但是queue worker不会在任何故障的情况下自动重启。