NotificationWarmUserCache.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Jobs\NotificationPipeline;
  3. use App\Services\NotificationService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Log;
  11. class NotificationWarmUserCache implements ShouldBeUnique, ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. /**
  15. * The profile ID to warm cache for.
  16. *
  17. * @var int
  18. */
  19. public $pid;
  20. /**
  21. * The number of times the job may be attempted.
  22. *
  23. * @var int
  24. */
  25. public $tries = 3;
  26. /**
  27. * The number of seconds to wait before retrying the job.
  28. * This creates exponential backoff: 10s, 30s, 90s
  29. *
  30. * @var array
  31. */
  32. public $backoff = [10, 30, 90];
  33. /**
  34. * The number of seconds after which the job's unique lock will be released.
  35. *
  36. * @var int
  37. */
  38. public $uniqueFor = 3600; // 1 hour
  39. /**
  40. * The maximum number of unhandled exceptions to allow before failing.
  41. *
  42. * @var int
  43. */
  44. public $maxExceptions = 2;
  45. /**
  46. * Create a new job instance.
  47. *
  48. * @param int $pid The profile ID to warm cache for
  49. * @return void
  50. */
  51. public function __construct(int $pid)
  52. {
  53. $this->pid = $pid;
  54. }
  55. /**
  56. * Get the unique ID for the job.
  57. */
  58. public function uniqueId(): string
  59. {
  60. return 'notifications:profile_warm_cache:'.$this->pid;
  61. }
  62. /**
  63. * Execute the job.
  64. */
  65. public function handle(): void
  66. {
  67. try {
  68. NotificationService::warmCache($this->pid, 100, true);
  69. } catch (\Exception $e) {
  70. Log::error('Failed to warm notification cache', [
  71. 'profile_id' => $this->pid,
  72. 'exception' => get_class($e),
  73. 'message' => $e->getMessage(),
  74. 'attempt' => $this->attempts(),
  75. ]);
  76. throw $e;
  77. }
  78. }
  79. }