zhaojs
2023-10-07 74f6db362e1aacb440eacce84e9433de1368a51a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
 
namespace think\queue;
 
use Closure;
use think\Process;
 
class Listener
{
 
    /**
     * @var string
     */
    protected $commandPath;
 
    /**
     * @var int
     */
    protected $sleep = 3;
 
    /**
     * @var int
     */
    protected $maxTries = 0;
 
    /**
     * @var string
     */
    protected $workerCommand;
 
    /**
     * @var \Closure|null
     */
    protected $outputHandler;
 
    /**
     * @param  string $commandPath
     */
    public function __construct($commandPath)
    {
        $this->commandPath   = $commandPath;
        $this->workerCommand =
            '"' . PHP_BINARY . '" think queue:work --queue="%s" --delay=%s --memory=%s --sleep=%s --tries=%s';
    }
 
    /**
     * @param  string $queue
     * @param  string $delay
     * @param  string $memory
     * @param  int    $timeout
     * @return void
     */
    public function listen($queue, $delay, $memory, $timeout = 60)
    {
        $process = $this->makeProcess($queue, $delay, $memory, $timeout);
 
        while (true) {
            $this->runProcess($process, $memory);
        }
    }
 
    /**
     * @param \Think\Process $process
     * @param  int           $memory
     */
    public function runProcess(Process $process, $memory)
    {
        $process->run(function ($type, $line) {
            $this->handleWorkerOutput($type, $line);
        });
 
        if ($this->memoryExceeded($memory)) {
            $this->stop();
        }
    }
 
    /**
     * @param  string $queue
     * @param  int    $delay
     * @param  int    $memory
     * @param  int    $timeout
     * @return \think\Process
     */
    public function makeProcess($queue, $delay, $memory, $timeout)
    {
        $string  = $this->workerCommand;
        $command = sprintf($string, $queue, $delay, $memory, $this->sleep, $this->maxTries);
 
        return new Process($command, $this->commandPath, null, null, $timeout);
    }
 
    /**
     * @param  int    $type
     * @param  string $line
     * @return void
     */
    protected function handleWorkerOutput($type, $line)
    {
        if (isset($this->outputHandler)) {
            call_user_func($this->outputHandler, $type, $line);
        }
    }
 
    /**
     * @param  int $memoryLimit
     * @return bool
     */
    public function memoryExceeded($memoryLimit)
    {
        return (memory_get_usage() / 1024 / 1024) >= $memoryLimit;
    }
 
    /**
     * @return void
     */
    public function stop()
    {
        die;
    }
 
    /**
     * @param  \Closure $outputHandler
     * @return void
     */
    public function setOutputHandler(Closure $outputHandler)
    {
        $this->outputHandler = $outputHandler;
    }
 
    /**
     * @return int
     */
    public function getSleep()
    {
        return $this->sleep;
    }
 
    /**
     * @param  int $sleep
     * @return void
     */
    public function setSleep($sleep)
    {
        $this->sleep = $sleep;
    }
 
    /**
     * @param  int $tries
     * @return void
     */
    public function setMaxTries($tries)
    {
        $this->maxTries = $tries;
    }
}