Database.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\job;
  12. use think\queue\Job;
  13. use think\queue\connector\Database as DatabaseQueue;
  14. class Database extends Job
  15. {
  16. /**
  17. * The database queue instance.
  18. * @var DatabaseQueue
  19. */
  20. protected $database;
  21. /**
  22. * The database job payload.
  23. * @var Object
  24. */
  25. protected $job;
  26. public function __construct(DatabaseQueue $database, $job, $queue)
  27. {
  28. $this->job = $job;
  29. $this->queue = $queue;
  30. $this->database = $database;
  31. $this->job->attempts = $this->job->attempts + 1;
  32. }
  33. /**
  34. * 执行任务
  35. * @return void
  36. */
  37. public function fire()
  38. {
  39. $this->resolveAndFire(json_decode($this->job->payload, true));
  40. }
  41. /**
  42. * 删除任务
  43. * @return void
  44. */
  45. public function delete()
  46. {
  47. parent::delete();
  48. $this->database->deleteReserved($this->job->id);
  49. }
  50. /**
  51. * 重新发布任务
  52. * @param int $delay
  53. * @return void
  54. */
  55. public function release($delay = 0)
  56. {
  57. parent::release($delay);
  58. $this->delete();
  59. $this->database->release($this->queue, $this->job, $delay);
  60. }
  61. /**
  62. * 获取当前任务尝试次数
  63. * @return int
  64. */
  65. public function attempts()
  66. {
  67. return (int) $this->job->attempts;
  68. }
  69. /**
  70. * Get the raw body string for the job.
  71. * @return string
  72. */
  73. public function getRawBody()
  74. {
  75. return $this->job->payload;
  76. }
  77. }