OrderJob.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace crmeb\jobs;
  3. use crmeb\interfaces\JobInterface;
  4. use think\facade\Db;
  5. use think\queue\Job;
  6. class OrderJob implements JobInterface
  7. {
  8. public function fire(Job $job, $data): void
  9. {
  10. $doDefaultJod = $data['method'] ?? null;
  11. $beforeMethod = $data['beforeMethod'] ?? null;
  12. $jobData = $data['data'] ?? null;
  13. $errorTimes = $data['errorTimes'] ?? 0;
  14. $release = $data['release'] ?? 0;
  15. if (method_exists($this, $beforeMethod)) {
  16. $isJobStillNeedToBeDone = $this->{$beforeMethod}($jobData);
  17. if ($isJobStillNeedToBeDone) {
  18. $job->delete();
  19. return;
  20. }
  21. }
  22. if (method_exists($this, $doDefaultJod)) {
  23. $isJobDone = $this->{$doDefaultJod}($jobData);
  24. if ($isJobDone) {
  25. $job->delete();
  26. return;
  27. } else {
  28. if ($job->attempts() > $errorTimes && $errorTimes) {
  29. $job->delete();
  30. return;
  31. } else {
  32. $job->release($release);
  33. }
  34. }
  35. }
  36. }
  37. public function doDefaultJod($data): bool
  38. {
  39. return Db::name('cache')->insert(['key'=>'test_'.rand(10,30),'result'=>json_encode($data)]);
  40. }
  41. }