14
14
use Carbon \Carbon ;
15
15
use stdClass ;
16
16
use think \Db ;
17
+ use think \db \ConnectionInterface ;
17
18
use think \db \Query ;
18
19
use think \queue \Connector ;
19
20
use think \queue \InteractsWithTime ;
@@ -47,7 +48,7 @@ class Database extends Connector
47
48
*/
48
49
protected $ retryAfter = 60 ;
49
50
50
- public function __construct (Db $ db , $ table , $ default = 'default ' , $ retryAfter = 60 )
51
+ public function __construct (ConnectionInterface $ db , $ table , $ default = 'default ' , $ retryAfter = 60 )
51
52
{
52
53
$ this ->db = $ db ;
53
54
$ this ->table = $ table ;
@@ -57,7 +58,9 @@ public function __construct(Db $db, $table, $default = 'default', $retryAfter =
57
58
58
59
public static function __make (Db $ db , $ config )
59
60
{
60
- return new self ($ db , $ config ['table ' ], $ config ['queue ' ], $ config ['retry_after ' ] ?? 60 );
61
+ $ connection = $ db ->connect ($ config ['connection ' ] ?? null );
62
+
63
+ return new self ($ connection , $ config ['table ' ], $ config ['queue ' ], $ config ['retry_after ' ] ?? 60 );
61
64
}
62
65
63
66
public function size ($ queue = null )
@@ -105,9 +108,9 @@ function ($job) use ($queue, $data, $availableAt) {
105
108
/**
106
109
* 重新发布任务
107
110
*
108
- * @param string $queue
111
+ * @param string $queue
109
112
* @param StdClass $job
110
- * @param int $delay
113
+ * @param int $delay
111
114
* @return mixed
112
115
*/
113
116
public function release ($ queue , $ job , $ delay )
@@ -119,9 +122,9 @@ public function release($queue, $job, $delay)
119
122
* Push a raw payload to the database with a given delay.
120
123
*
121
124
* @param \DateTime|int $delay
122
- * @param string|null $queue
123
- * @param string $payload
124
- * @param int $attempts
125
+ * @param string|null $queue
126
+ * @param string $payload
127
+ * @param int $attempts
125
128
* @return mixed
126
129
*/
127
130
protected function pushToDatabase ($ queue , $ payload , $ delay = 0 , $ attempts = 0 )
@@ -160,7 +163,8 @@ public function pop($queue = null)
160
163
protected function getNextAvailableJob ($ queue )
161
164
{
162
165
163
- $ job = $ this ->db ->name ($ this ->table )
166
+ $ job = $ this ->db
167
+ ->name ($ this ->table )
164
168
->lock (true )
165
169
->where ('queue ' , $ this ->getQueue ($ queue ))
166
170
->where (function (Query $ query ) {
@@ -190,10 +194,13 @@ protected function getNextAvailableJob($queue)
190
194
*/
191
195
protected function markJobAsReserved ($ job )
192
196
{
193
- $ this ->db ->name ($ this ->table )->where ('id ' , $ job ->id )->update ([
194
- 'reserve_time ' => $ job ->reserve_time = $ this ->currentTime (),
195
- 'attempts ' => ++$ job ->attempts ,
196
- ]);
197
+ $ this ->db
198
+ ->name ($ this ->table )
199
+ ->where ('id ' , $ job ->id )
200
+ ->update ([
201
+ 'reserve_time ' => $ job ->reserve_time = $ this ->currentTime (),
202
+ 'attempts ' => ++$ job ->attempts ,
203
+ ]);
197
204
198
205
return $ job ;
199
206
}
0 commit comments