1414use Carbon \Carbon ;
1515use stdClass ;
1616use think \Db ;
17+ use think \db \ConnectionInterface ;
1718use think \db \Query ;
1819use think \queue \Connector ;
1920use think \queue \InteractsWithTime ;
@@ -47,7 +48,7 @@ class Database extends Connector
4748 */
4849 protected $ retryAfter = 60 ;
4950
50- public function __construct (Db $ db , $ table , $ default = 'default ' , $ retryAfter = 60 )
51+ public function __construct (ConnectionInterface $ db , $ table , $ default = 'default ' , $ retryAfter = 60 )
5152 {
5253 $ this ->db = $ db ;
5354 $ this ->table = $ table ;
@@ -57,7 +58,9 @@ public function __construct(Db $db, $table, $default = 'default', $retryAfter =
5758
5859 public static function __make (Db $ db , $ config )
5960 {
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 );
6164 }
6265
6366 public function size ($ queue = null )
@@ -105,9 +108,9 @@ function ($job) use ($queue, $data, $availableAt) {
105108 /**
106109 * 重新发布任务
107110 *
108- * @param string $queue
111+ * @param string $queue
109112 * @param StdClass $job
110- * @param int $delay
113+ * @param int $delay
111114 * @return mixed
112115 */
113116 public function release ($ queue , $ job , $ delay )
@@ -119,9 +122,9 @@ public function release($queue, $job, $delay)
119122 * Push a raw payload to the database with a given delay.
120123 *
121124 * @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
125128 * @return mixed
126129 */
127130 protected function pushToDatabase ($ queue , $ payload , $ delay = 0 , $ attempts = 0 )
@@ -160,7 +163,8 @@ public function pop($queue = null)
160163 protected function getNextAvailableJob ($ queue )
161164 {
162165
163- $ job = $ this ->db ->name ($ this ->table )
166+ $ job = $ this ->db
167+ ->name ($ this ->table )
164168 ->lock (true )
165169 ->where ('queue ' , $ this ->getQueue ($ queue ))
166170 ->where (function (Query $ query ) {
@@ -190,10 +194,13 @@ protected function getNextAvailableJob($queue)
190194 */
191195 protected function markJobAsReserved ($ job )
192196 {
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+ ]);
197204
198205 return $ job ;
199206 }
0 commit comments