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

How do I enable queue:work from live server?

这个在我的本地服务器上工作正常,因为我可以 运行 命令。

php artisan queue:work

但是如何在实时服务器上实现它? 我真的被那个问题困住了。但是因为我想通过邮件发送通知。因此,我将继续使用 laravel 默认队列功能,因为它不会造成任何延迟并自动完成它的工作。所以,如果您能提供帮助,我们将不胜感激。

提前致谢..

//我的.env配置

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

my jobs table from database

//我的新奖学金Class

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

你应该找一个叫做 supervisor 的东西

查看此示例

tutorial

生产的想法与开发相同,你应该 运行 queue:work 但当然你不能时不时地 ssh 到你的服务器并检查命令是否仍然 运行ning,这就是为什么您应该按照 Laravel.

的建议使用类似 supervisor 的原因

这是一个quick introduction to supervisor in the Laravel documentation 基本上,如果你的服务器是 debian,你应该通过 运行ning 安装 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 文档中的注释:

  • You should ensure that the value of stopwaitsecs is greater than the number of seconds consumed by your longest running job. Otherwise, Supervisor may kill the job before it is finished processing.
  • In this example, the numprocs directive will instruct Supervisor to run 8 queue:work processes and monitor all of them, automatically restarting them if they fail.
  • You should change the queue:work sqs portion of the command directive to reflect your desired queue connection.

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

如果您的服务器不是 debian,请参阅 official documentation of suppervisor

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*