Redis.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Redis as RedisQueue;
  14. class Redis extends Job
  15. {
  16. /**
  17. * The redis queue instance.
  18. * @var RedisQueue
  19. */
  20. protected $redis;
  21. /**
  22. * The database job payload.
  23. * @var Object
  24. */
  25. protected $job;
  26. public function __construct(RedisQueue $redis, $job, $queue)
  27. {
  28. $this->job = $job;
  29. $this->queue = $queue;
  30. $this->redis = $redis;
  31. }
  32. /**
  33. * Fire the job.
  34. * @return void
  35. */
  36. public function fire()
  37. {
  38. $this->resolveAndFire(json_decode($this->getRawBody(), true));
  39. }
  40. /**
  41. * Get the number of times the job has been attempted.
  42. * @return int
  43. */
  44. public function attempts()
  45. {
  46. return json_decode($this->job, true)['attempts'];
  47. }
  48. /**
  49. * Get the raw body string for the job.
  50. * @return string
  51. */
  52. public function getRawBody()
  53. {
  54. return $this->job;
  55. }
  56. /**
  57. * 删除任务
  58. *
  59. * @return void
  60. */
  61. public function delete()
  62. {
  63. parent::delete();
  64. $this->redis->deleteReserved($this->queue, $this->job);
  65. }
  66. /**
  67. * 重新发布任务
  68. *
  69. * @param int $delay
  70. * @return void
  71. */
  72. public function release($delay = 0)
  73. {
  74. parent::release($delay);
  75. $this->delete();
  76. $this->redis->release($this->queue, $this->job, $delay, $this->attempts() + 1);
  77. }
  78. }