Browse Source

tested beanstalk-queue

mx 3 years ago
parent
commit
54663cc59e

+ 6 - 0
README.md

@@ -68,3 +68,9 @@ see [official doc](https://www.workerman.net/doc/webman/middleware.html)
 
 ### Redis Queue
 
+### I18N
+
+### Websocket
+
+### Crontab
+

+ 0 - 1
app/controller/Index.php

@@ -32,5 +32,4 @@ class Index
         Cache::set('cache', 123);
         return json(['code' => 0, 'msg' => 'ok']);
     }
-
 }

+ 10 - 0
app/controller/User.php

@@ -5,6 +5,8 @@ namespace app\controller;
 use support\Request;
 use app\model\User as UserModel;
 use Webman\RedisQueue\Client;
+use Vanderw\BeanstalkQueue\Client as BS;
+
 
 class User
 {
@@ -36,4 +38,12 @@ class User
 
         return json($data);
     }
+
+    public function bs(Request $request)
+    {
+        
+        BS::send('sms_consumer', ['text'=>'hello, you win']);
+        
+        return json(['code' => 0, 'msg' => 'good']);
+    }
 }

+ 34 - 0
app/queue/beanstalk/SmsConsumer.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace app\queue\beanstalk;
+
+use Vanderw\BeanstalkQueue\Consumer;
+use Vanderw\BeanstalkQueue\Client;
+
+class SmsConsumer implements Consumer
+{
+    // 要消费的队列名
+    public $tube = 'sms_consumer';
+    // reserve 操作的超時秒數
+    public $reserve_timeout =10;
+    // 指示退出訂閱
+    public $quit = false;
+    // 连接名,对应 plugin/webman/beanstalk-queue/beanstalk.php 里的连接`
+    public $connection = 'default';
+
+    // 消费
+    public function consume($job)
+    {
+        $data = $job->getData();
+        if (is_null($data)) {
+            // Log
+            return;
+        }
+        // processing
+
+        $data = json_decode($data, true);
+        print_r($data);
+        
+        Client::delete($job);
+    }
+}

+ 3 - 1
composer.json

@@ -36,13 +36,15 @@
     "webman/console": "^1.0",
     "webman/redis-queue": "^1.1",
     "symfony/translation": "^5.4",
-    "workerman/crontab": "^1.0"
+    "workerman/crontab": "^1.0",
+    "pda/pheanstalk": "^4.0"
   },
   "suggest": {
     "ext-event": "For better performance. "
   },
   "autoload": {
     "psr-4": {
+      "Vanderw\\BeanstalkQueue\\" : "vendor/vanderw/beanstalk-queue/src",
       "": "./",
       "App\\": "./app"
     },

+ 56 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "ba4d806e38ece83c2804652c3cb847a2",
+    "content-hash": "6915fee4834cac0251f47409404ca293",
     "packages": [
         {
             "name": "cakephp/core",
@@ -1166,6 +1166,61 @@
             },
             "time": "2018-02-13T20:26:39+00:00"
         },
+        {
+            "name": "pda/pheanstalk",
+            "version": "v4.0.4",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/pheanstalk/pheanstalk.git",
+                "reference": "1a43eb97a53144a2e692bce2ea2be721cc9913a4"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/pheanstalk/pheanstalk/zipball/1a43eb97a53144a2e692bce2ea2be721cc9913a4",
+                "reference": "1a43eb97a53144a2e692bce2ea2be721cc9913a4",
+                "shasum": ""
+            },
+            "require": {
+                "ext-mbstring": "*",
+                "php": ">=7.1.0"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "^7"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Pheanstalk\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Paul Annesley",
+                    "email": "paul@annesley.cc",
+                    "homepage": "http://paul.annesley.cc/",
+                    "role": "Developer"
+                },
+                {
+                    "name": "Sam Mousa",
+                    "email": "sam@mousa.nl",
+                    "role": "Maintainer"
+                }
+            ],
+            "description": "PHP client for beanstalkd queue",
+            "homepage": "https://github.com/pheanstalk/pheanstalk",
+            "keywords": [
+                "beanstalkd"
+            ],
+            "support": {
+                "issues": "https://github.com/pheanstalk/pheanstalk/issues",
+                "source": "https://github.com/pheanstalk/pheanstalk/tree/v4.0.4"
+            },
+            "time": "2021-11-19T15:00:20+00:00"
+        },
         {
             "name": "psr/cache",
             "version": "1.0.1",

+ 5 - 0
config/plugin/vanderw/beanstalk-queue/app.php

@@ -0,0 +1,5 @@
+<?php
+
+return [
+    'enable' => true,
+];

+ 14 - 0
config/plugin/vanderw/beanstalk-queue/beanstalk.php

@@ -0,0 +1,14 @@
+<?php
+
+return [
+    'default' => [
+        'ip' => '127.0.0.1',
+        'port' => 11300,
+        'timeout' => 10, // s
+        'options' => [
+            'auth' => '123456', // 密码,可选参数
+            'delay'  => 2,      // 延遲秒數
+            'retry_after' => 5, // 秒後重试
+        ]
+    ],
+];

+ 7 - 0
config/plugin/vanderw/beanstalk-queue/command.php

@@ -0,0 +1,7 @@
+<?php
+
+use Vanderw\BeanstalkQueue\Command\MakeConsumerCommand;
+
+return [
+    MakeConsumerCommand::class
+];

+ 12 - 0
config/plugin/vanderw/beanstalk-queue/process.php

@@ -0,0 +1,12 @@
+<?php
+
+return [
+    'consumer'  => [
+        'handler'     => Vanderw\BeanstalkQueue\Process\Consumer::class,
+        'count'       => 1, // 可以设置多进程同时消费
+        'constructor' => [
+            // 消费者类目录
+            'consumer_dir' => app_path() . '/queue/beanstalk'
+        ]
+    ]
+];