Topthink.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Topthink as TopthinkQueue;
  14. class Topthink extends Job
  15. {
  16. /**
  17. * The Iron queue instance.
  18. *
  19. * @var TopthinkQueue
  20. */
  21. protected $topthink;
  22. /**
  23. * The IronMQ message instance.
  24. *
  25. * @var object
  26. */
  27. protected $job;
  28. public function __construct(TopthinkQueue $topthink, $job, $queue)
  29. {
  30. $this->topthink = $topthink;
  31. $this->job = $job;
  32. $this->queue = $queue;
  33. $this->job->attempts = $this->job->attempts + 1;
  34. }
  35. /**
  36. * Fire the job.
  37. * @return void
  38. */
  39. public function fire()
  40. {
  41. $this->resolveAndFire(json_decode($this->job->payload, true));
  42. }
  43. /**
  44. * Get the number of times the job has been attempted.
  45. * @return int
  46. */
  47. public function attempts()
  48. {
  49. return (int) $this->job->attempts;
  50. }
  51. public function delete()
  52. {
  53. parent::delete();
  54. $this->topthink->deleteMessage($this->queue, $this->job->id);
  55. }
  56. public function release($delay = 0)
  57. {
  58. parent::release($delay);
  59. $this->delete();
  60. $this->topthink->release($this->queue, $this->job, $delay);
  61. }
  62. /**
  63. * Get the raw body string for the job.
  64. * @return string
  65. */
  66. public function getRawBody()
  67. {
  68. return $this->job->payload;
  69. }
  70. }