選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

52 行
1.1KB

  1. <?php
  2. namespace App\Jobs;
  3. use App\Util\DBUtil;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Log;
  10. abstract class BaseJob implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. protected DBUtil $db;
  14. public function handle()
  15. {
  16. $this->db = DBUtil::instance();
  17. try {
  18. $this->db->beginTransaction();
  19. $this->logConfig();
  20. $this->handleJob();
  21. $this->db->commit();
  22. } catch (Exception $e) {
  23. $this->db->rollBack();
  24. throw $e;
  25. }
  26. }
  27. /**
  28. * ジョブを再試行する前に待機する秒数を計算
  29. */
  30. public function backoff(): int
  31. {
  32. return 5;
  33. }
  34. abstract protected function handleJob();
  35. private function logConfig()
  36. {
  37. Log::withContext([
  38. '__job_class__' => static::class,
  39. ]);
  40. }
  41. }