Database.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\queue\connector;
  12. use Carbon\Carbon;
  13. use stdClass;
  14. use think\Db;
  15. use think\db\Query;
  16. use think\queue\Connector;
  17. use think\queue\InteractsWithTime;
  18. use think\queue\job\Database as DatabaseJob;
  19. class Database extends Connector
  20. {
  21. use InteractsWithTime;
  22. protected $db;
  23. /**
  24. * The database table that holds the jobs.
  25. *
  26. * @var string
  27. */
  28. protected $table;
  29. /**
  30. * The name of the default queue.
  31. *
  32. * @var string
  33. */
  34. protected $default;
  35. /**
  36. * The expiration time of a job.
  37. *
  38. * @var int|null
  39. */
  40. protected $retryAfter = 60;
  41. public function __construct(Db $db, $table, $default = 'default', $retryAfter = 60)
  42. {
  43. $this->db = $db;
  44. $this->table = $table;
  45. $this->default = $default;
  46. $this->retryAfter = $retryAfter;
  47. }
  48. public static function __make(Db $db, $config)
  49. {
  50. return new self($db, $config['table'], $config['queue'], $config['retry_after'] ?? 60);
  51. }
  52. public function size($queue = null)
  53. {
  54. $this->db->name($this->table)
  55. ->where('queue', $this->getQueue($queue))
  56. ->count();
  57. }
  58. public function push($job, $data = '', $queue = null)
  59. {
  60. return $this->pushToDatabase($queue, $this->createPayload($job, $data));
  61. }
  62. public function pushRaw($payload, $queue = null, array $options = [])
  63. {
  64. return $this->pushToDatabase($queue, $payload);
  65. }
  66. public function later($delay, $job, $data = '', $queue = null)
  67. {
  68. return $this->pushToDatabase($queue, $this->createPayload($job, $data), $delay);
  69. }
  70. public function bulk($jobs, $data = '', $queue = null)
  71. {
  72. $queue = $this->getQueue($queue);
  73. $availableAt = $this->availableAt();
  74. return $this->db->name($this->table)->insertAll(collect((array) $jobs)->map(
  75. function ($job) use ($queue, $data, $availableAt) {
  76. return [
  77. 'queue' => $queue,
  78. 'attempts' => 0,
  79. 'reserved_at' => null,
  80. 'available_at' => $availableAt,
  81. 'created_at' => $this->currentTime(),
  82. 'payload' => $this->createPayload($job, $data),
  83. ];
  84. }
  85. )->all());
  86. }
  87. /**
  88. * 重新发布任务
  89. *
  90. * @param string $queue
  91. * @param StdClass $job
  92. * @param int $delay
  93. * @return mixed
  94. */
  95. public function release($queue, $job, $delay)
  96. {
  97. return $this->pushToDatabase($queue, $job->payload, $delay, $job->attempts);
  98. }
  99. /**
  100. * Push a raw payload to the database with a given delay.
  101. *
  102. * @param \DateTime|int $delay
  103. * @param string|null $queue
  104. * @param string $payload
  105. * @param int $attempts
  106. * @return mixed
  107. */
  108. protected function pushToDatabase($queue, $payload, $delay = 0, $attempts = 0)
  109. {
  110. return $this->db->name($this->table)->insertGetId([
  111. 'queue' => $this->getQueue($queue),
  112. 'attempts' => $attempts,
  113. 'reserved_at' => null,
  114. 'available_at' => $this->availableAt($delay),
  115. 'created_at' => $this->currentTime(),
  116. 'payload' => $payload,
  117. ]);
  118. }
  119. public function pop($queue = null)
  120. {
  121. $queue = $this->getQueue($queue);
  122. return $this->db->transaction(function () use ($queue) {
  123. if ($job = $this->getNextAvailableJob($queue)) {
  124. $job = $this->markJobAsReserved($job);
  125. return new DatabaseJob($this->app, $this, $job, $this->connection, $queue);
  126. }
  127. });
  128. }
  129. /**
  130. * 获取下个有效任务
  131. *
  132. * @param string|null $queue
  133. * @return StdClass|null
  134. */
  135. protected function getNextAvailableJob($queue)
  136. {
  137. $job = $this->db->name($this->table)
  138. ->lock(true)
  139. ->where('queue', $this->getQueue($queue))
  140. ->where(function (Query $query) {
  141. $query->where(function (Query $query) {
  142. $query->whereNull('reserved_at')
  143. ->where('available_at', '<=', $this->currentTime());
  144. });
  145. //超时任务重试
  146. $expiration = Carbon::now()->subSeconds($this->retryAfter)->getTimestamp();
  147. $query->whereOr(function (Query $query) use ($expiration) {
  148. $query->where('reserved_at', '<=', $expiration);
  149. });
  150. })
  151. ->order('id', 'asc')
  152. ->find();
  153. return $job ? (object) $job : null;
  154. }
  155. /**
  156. * 标记任务正在执行.
  157. *
  158. * @param stdClass $job
  159. * @return stdClass
  160. */
  161. protected function markJobAsReserved($job)
  162. {
  163. $this->db->name($this->table)->where('id', $job->id)->update([
  164. 'reserved_at' => $job->reserved_at = $this->currentTime(),
  165. 'attempts' => ++$job->attempts,
  166. ]);
  167. return $job;
  168. }
  169. /**
  170. * 删除任务
  171. *
  172. * @param string $id
  173. * @return void
  174. */
  175. public function deleteReserved($id)
  176. {
  177. $this->db->transaction(function () use ($id) {
  178. if ($this->db->name($this->table)->lock(true)->find($id)) {
  179. $this->db->name($this->table)->where('id', $id)->delete();
  180. }
  181. });
  182. }
  183. protected function getQueue($queue)
  184. {
  185. return $queue ?: $this->default;
  186. }
  187. }