宝塔中使用Supervisor给laravel队列添加进程守护

宝塔是有提供Supervisor的,只不过需要单独安装,

 

 在宝塔软件中安装好 Supervisor 之后,添加守护进程

 

添加成功后,会自启动

 

 注意事项
1、如果 Supervisor 启动失败,查看一下 php 中相关函数是否被禁用
2、php artisan queue:work –tries=3 这边建议加上 --tries 参数
3、启动用户是 www

转载:https://blog.csdn.net/csdn876280441/article/details/121010951

-------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Workerman\Worker;
use Workerman\Mqtt\Client;
class Subscribe extends Command
{
    /**
     * The name and signature of the console command.
     *  接收消息的类
     *
     * @var string
     */
    //artisan命令 php artisan  mqtt
    protected $signature = 'mqtt {action=start}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'start subscribe';
    protected $client_id = 'backend-mqtt-client-';
    protected $MQTT_SERVER_HOST="xxx.cxxx.com";
    protected $MQTT_SERVER_PORT="1883";
    protected $MQTT_USER_NAME="emxxxx";
    protected $MQTT_USER_PASSWORD="fsVxxxx";
    protected $MQTT_TOPIC_NAME="BOX/+/event/property/post/ppi/#";
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        global $argv;
        $arg = $this->argument('action');
        $argv [1] = $arg;
        $worker = new Worker();
        $worker->onWorkerStart = function () {
            $mqtt = new Client('mqtt://' .$this->MQTT_SERVER_HOST. ":" . $this->MQTT_SERVER_PORT, array(
            'username' => $this->MQTT_USER_NAME,
            'password' => $this->MQTT_USER_PASSWORD,
            'debug' => true,
            'keepalive' => 60,
            'client_id' => $this->client_id . mt_rand(0, 999),
//                'will' => [
//                    'topic' => 'testtopic' ,
//                    'content' => 0,
//                    'qos' => 2,
//                    'retain' => true,
//                ]
        ));
            $mqtt->onConnect = function ($mqtt) {
                // BOX/01235Cxxxx4135482/event/property/post/ppi/6xxxx7779bc0c442xxx
                $mqtt->subscribe($this->MQTT_TOPIC_NAME, ['qos' => 2]);
            };
            $mqtt->onMessage = function ($topic, $content) {

                $deviceIds = $this->getGatewayId($topic);
                $payload = $this->getPayload($content);
                fputs(fopen('1.txt','a+'),json_encode($deviceIds).PHP_EOL);
                fputs(fopen('2.txt','a+'),json_encode($payload).PHP_EOL);
//                $this->info('收到消息' . $content);
//                $this->info("sub: $topic, $content");
            };
            $mqtt->connect();
        };
        Worker::runAll();


    }

    public function getPayload($content)
    {
        $payload = json_decode($content, true);
        if ($payload) {
            return $payload;
        }
        return false;
        Log::error("mqtt content 错误:" . $content);
    }

    public function getGatewayId($topic)
    {
        $arr = explode('/', $topic);
        return [
            "gatewayId" => $arr[1],
            "subDeviceId" => last($arr),
        ];
    }
}

 

【laravel7.x中文文档】队列

 ------------------------------------------------------

业务场景:项目里边有很多视频资源需要上传到抖音资源库,通过队列一条一条上传。
(1)创建任务【生成任务类】
在你的应用程序中,队列的任务类都默认放在 app/Jobs 目录下。如果这个目录不存在,那当你运行 make:job Artisan 命令时目录就会被自动创建。你可以用以下的 Artisan 命令来生成一个新的队列任务

php artisan make:job DyUploadResource

任务类结构
任务类的结构很简单,一般来说只会包含一个让队列用来调用此任务的 handle 方法。

<?php

namespace App\Jobs;

use App\Models\HtQuestLibraryQuest;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
class DyUploadResource implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数。
     *
     * @var int
     */
    public $tries = 5;

    /**
     * 任务失败前允许的最大异常数
     *
     * @var int
     */
    public $maxExceptions = 3;

    /**
     * 任务运行的超时时间。
     *
     * @var int
     */
    public $timeout = 600;

    /**
     * 课程信息
     *
     * @var int
     */
    public $product_data;

    /**
     * 抖音资源上传
     *
     * @return void
     */
    public function __construct(array $product_data)
    {
        $this->product_data = $product_data;

        if(empty($product_data)){

            exit();
        }
    }

    /**
     * Execute the job.
     * 里边写代码逻辑
     * @return bool
     */
    public function handle(): bool
    {
        
    }

}

(2)分发任务【如何使用】

<?php

namespace App\Http\Controllers;

use App\Jobs\ProcessPodcast;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PodcastController extends Controller
{
     /**
     * 抖音资源课程上传
     */
    public function dyUploadResource(Request $request)
    {
          // dispatch 用于分发,onQueue 用户起名,监听使用  
          // ->delay(Carbon::now()->addMinutes(10))  延迟分发
        DyUploadResourceMiddle::dispatch($product_data)->onQueue('dy_resource_get_status');
        
    }
}

(3)监听队列
php artisan queue:work --queue=dy_resource_get_status

开启后队列会执行redis已入队的值

 

 

redis队列任务:

 

 转载:https://blog.csdn.net/weixin_42905245/article/details/123253382

 

-------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 转载 https://www.lmlphp.com/user/62714/article/item/637440/

 

posted @ 2022-07-08 15:40  搬砖小伙子  阅读(1714)  评论(0编辑  收藏  举报