laravel - 如何启用队列:从实时服务器工作?
问题描述
这个在我的本地服务器上运行良好,因为我可以运行该命令。
php artisan queue:work
但是我如何在实时服务器上实现它呢? 我真的被这个问题困住了。但是因为我想通过邮件发送通知。所以,我正在关注使用 laravel 默认队列功能,因为它不会延迟并自动完成它的工作。所以,如果你能帮助真的很感激。
提前谢谢..
//我的 .env 配置
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=120
//我的新奖学金课程
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class NewScholarship extends Notification implements ShouldQueue
{
use Queueable;
public $scholarship;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($scholarship)
{
$this->scholarship = $scholarship;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello, Subscriber!')
->subject('New Scholarship has been published')
->line('New Scholarship info has been published to our website')
->line('Titled as: '.$this->scholarship->title)
->line('To have a look click the button below')
->action('View details', url('s/details',$this->scholarship->slug))
->line('Thank you for your subscription!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
解决方案
生产的想法与开发相同,您应该运行 queue:work 但当然您不能时不时地通过 ssh 连接到您的服务器并检查该命令是否仍在运行,这就是为什么您应该使用类似supervisor推荐的东西拉拉维尔。
这是Laravel 文档中对 supervisor 的快速介绍 基本上,如果你的服务器是 debian,你应该通过运行来安装 supervisor 包:
sudo apt-get install supervisor
在你 ssh 到你的服务器之后
安装完成后,使用 vi vim 或 nano 访问/etc/supervisor/conf.d
并创建配置文件 ex:laravel-worker.conf
下面是一个示例配置
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3 #"home/forge/app.com/artisan" is the path to your project root
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log
stopwaitsecs=3600
laravel 文档中的注释:
- 您应该确保 stopwaitsecs 的值大于运行时间最长的作业所消耗的秒数。否则,Supervisor 可能会在作业完成处理之前将其终止。
- 在这个例子中, numprocs 指令将指示 Supervisor 运行 8 个 queue:work 进程并监控所有这些进程,如果它们失败则自动重新启动它们。
- 您应该更改命令指令的 queue:work sqs 部分以反映您所需的队列连接。
请注意,此信息来自上面链接的 laravel 文档
如果你的服务器不是debian请看supervisor的官方文档
创建配置文件后,您可以使用以下命令更新 Supervisor 配置并启动进程:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
推荐阅读
- spring-boot - 使用 Thymeleaf 将数组传递给外部 Javascript
- python - 如何在熊猫数据框python中将时间序列排名表转换为单个排名表
- python - PYTHON:ValueError:未知的网址类型:'comments_42.html'
- spring - 如何让 Eclipse 在停止应用程序后自动释放端口?
- python - 为什么在最终的 softmax 层之前移除全连接层后,我的卷积神经网络的准确率会提高?
- python-3.x - Python 的 asyncio.Event() 跨不同的类
- sql - CDATA SFTP 连接器无文件传输
- docker - Docker 容器无法与 Nvidia nsight 系统一起使用
- sql-server - Group By Clause 不能通过 Spring Boot 的 Postman 工作
- php - 从 PHP 代码 HY000/1049 访问 MySQL 数据库时出错