laravel queue (asynchronous queue) queue management tools Supervisor

Code

 // perform an asynchronous queue redis implementation of this method over and over again


  GenerateWalletAddress::dispatch($user->id)->onQueue('getnewaddress')->onConnection('redis');

Task Method Example

 

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\UsersWallet;

class GenerateWalletAddress implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $userid;
    public function __construct($userid)
    {
        //
        $this->userid = $userid;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        try {
            defined('IS_FORMAL_HOST') or define('IS_FORMAL_HOST', 1);
            $address = new UsersWallet;
            $address->add_address($this->userid, 1);
        } catch (\Exception $e) {
            debug_log('script/address.log', $e->getMessage());
        }
    }
}

 

Execute the command line

php artisan queue:work redis --queue getnewaddress --tries=1

注: 调用getnewaddress任务执行redis queue队列 
    tries 失败次数一次

Here is the Supervisor 

This official document to the laravel

Supervisor configuration

Installation Supervisor

Supervisor is a process to monitor the Linux operating system, it can  queue:work automatically restart the time to hang up. Supervisor installed on Ubuntu, you can use the following command:

sudo apt-get install supervisor

{Reminder} If you think configured Supervisor nearly impossible, consider using  Laravel Forge , you will automatically install and configure Laravel project Supervisor.

Configuring Supervisor

Supervisor configuration file is usually located in  /etc/supervisor/conf.d the directory. In this directory, you can create any number of profiles to control how the supervisor will monitor your progress. For example, create a  laravel-worker.conf file to make it launch and monitor a  queue:work process:

[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
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log

In this example, the numprocs command will run eight designated Supervisor  queue:work process and monitor them, if they hang up automatically restart them. You should change the  command options  queue:work sqs section to indicate that you need to queue connection.

Start Supervisor

After the profile is created, you can use the following command to update the Supervisor configuration and start the process:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*

 

After installing talk about usage

My configuration file is located /etc/supervisor/conf.d

Write configuration files cat htapp_getnewaddress.conf

[program:htapp_getnewaddress]
process_name=%(program_name)s_%(process_num)02d
command= php /data/wwwroot/test.hamdantoken.io/HamdanTokenApp/artisan queue:work redis --queue=getnewaddress  --sleep=3 --tries=1 --daemon
autostart=true
autorestart=true
user=root
numprocs=3
redirect_stderr=true
stdout_logfile=/data/wwwlogs/htapp_getnewaddress.log

注: 配置任务方法 program


    

Edit your profile laravel-worker.conf

Add Boot

command= php /data/wwwroot/test.hamdantoken.io/HamdanTokenApp/artisan queue:work redis --queue=getnewaddress  --sleep=3 --tries=1 --daemon
            队列方法             错误重发一次

supervisorctl update

Reload the configuration htapp_getnewaddress

supervisorctl update htapp_getnewaddress

This method starts 

supervisorctl status htapp_getnewaddress

See all queue processes

ps -ef | grep queue

result 

 

Okay, so three of the queue management process Supervisor

 

Published 264 original articles · won praise 46 · views 370 000 +

Guess you like

Origin blog.csdn.net/qq_27229113/article/details/103202097