如何从实时服务器启用queue:work?

时间:2020-06-09 17:53:38

标签: laravel queue live

此命令在我的本地服务器上运行良好,因为我可以运行该命令。

php artisan queue:work

但是我如何在实时服务器上实现它? 我真的陷在这个问题上了。但是,因为我想通过邮件发送通知。因此,我将遵循使用laravel默认队列功能,因为它不会延迟,并且会自动完成工作。因此,如果您能帮助您真的很感激。

提前谢谢。

// my .env配置

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=120

my jobs table from database

//我的新奖学金课程

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 [
        //
    ];
 }
}

2 个答案:

答案 0 :(得分:0)

您应该寻找一种叫做主管的东西

检查此示例

tutorial

答案 1 :(得分:0)

关于生产的想法与开发相同,您应该运行queue:work,但是您当然不能不时地将ssh到服务器,并检查命令是否仍在运行,这就是为什么您应该使用类似{ {3}}是Laravel推荐的。

这里是supervisor 基本上,如果您的服务器是debian,则应运行以下命令来安装超级用户软件包:

SSH到服务器后

sudo apt-get install supervisor

安装完成后,使用vi vim或nano访问/etc/supervisor/conf.d并创建一个配置文件,例如: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的值大于运行时间最长的作业所消耗的秒数。否则,主管可能会在作业完成处理之前将其杀死。
  • 在此示例中,numprocs指令将指示Supervisor运行8个queue:work进程并监视所有进程,并在失败时自动重新启动它们。
  • 您应该更改命令指令的queue:work sqs部分,以反映所需的队列连接。

请注意,此信息来自上面链接的laravel文档

如果您的服务器不是debian,请参见quick introduction to supervisor in the Laravel documentation

创建配置文件后,您可以使用以下命令更新Supervisor配置并启动进程:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*