laravel + Horizon
composer require laravel/horizon
php artisan horizon:install
这会在config/horizon.php文件中配置Horizon的主要设置。这里可以配置数据库或redis等信息
启动:
php artisan horizon
supervisor
Supervisor是一个用Python开发的进程管理工具,它可以很方便地用来启动、重启、关闭进程(不仅仅是Python进程)。除了对单个进程的控制,还可以同时启动、关闭多个进程。例如,在服务器出现问题导致所有应用程序都被杀死时,可以使用Supervisor同时启动所有应用程序,而不是一个一个地敲命令启动
sudo apt update && sudo apt install supervisor
配置Supervisor 在/etc/supervisor/conf.d/目录下创建一个配置文件,例如laravel-worker.conf,并添加以下内容(即将laravel的horizon挂载到supervisor中)
[program:horizon]
process_name=%(program_name)s
command=php /var/www/html/laravel10/artisan horizon
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/html/laravel10/storage/logs/horizon.log
[program:laravel-scheduler]
process_name=%(program_name)s
command=php /var/www/html/laravel10/artisan schedule:run
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/html/laravel10/storage/logs/scheduler.log
加载Supervisor配置
运行以下命令来重新加载Supervisor配置,并启动Horizon进程:
supervisorctl reread
supervisorctl update
supervisorctl start horizon
supervisorctl start laravel-scheduler
重启
supervisorctl restart laravel-scheduler
验证Horizon进程状态
使用以下命令检查Horizon进程是否正在运行:
sudo supervisorctl status horizon
Supervisor配置文件中设置了autostart=true
,Horizon进程将会自动在系统启动时启动。
启用并启动服务:
sudo systemctl enable supervisor.service
sudo systemctl start supervisor.service
例:Supervisor管理的Horizon进程
/usr/bin/python3 /usr/bin/supervisord -c /etc/supervisor/conf.d/laravel-worker.conf
检查自动启动horizon的状态 ps -ef | grep 'horizon'
Schedule+Task
创建一个task任务文件:
php artisan make:command CommandName1
php artisan make:command CommandName2
php artisan make:command CommandName3
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CommandName1 extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name1';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// 你的任务逻辑
$this->info('任务执行了!');
return 0;
}
}
接下来,在 app/Console/Kernel.php
中注册这个命令,并设置调度:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// 每天2点执行的任务
$schedule->command('command:name1')
->dailyAt('02:00');
// 每周三执行的任务
$schedule->command('command:name2')
->weeklyOn(3, '02:00'); // 每周三的2点执行
// 每个月1号执行的任务
$schedule->command('command:name3')
->monthlyOn(1, '02:00'); // 每个月1号的2点执行
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
如果一个任务在多个时间点执行的话:
protected function schedule(Schedule $schedule)
{
$schedule->command('command:name')
->dailyAt('02:00') // 每天2点执行
->when(function () {
return now()->dayOfWeek === 3; // 每周三执行
})
->when(function () {
return now()->day === 1; // 每个月的1日执行
});
}
结束。