Created
November 7, 2025 00:43
-
-
Save devhammed/2f43ae2bd613597c51a8dd7ac730446b to your computer and use it in GitHub Desktop.
Laravel Background Polling Queue Job is a job that checks for the status of something in any source like external API or database and re-queue itself if the item is not at the desirable status yet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Jobs; | |
| use Illuminate\Bus\Queueable; | |
| use Illuminate\Contracts\Queue\ShouldQueue; | |
| use Illuminate\Queue\InteractsWithQueue; | |
| use Illuminate\Queue\SerializesModels; | |
| class BackgroundPollingJob implements ShouldQueue | |
| { | |
| use InteractsWithQueue, Queueable, SerializesModels; | |
| public $itemId; | |
| public function __construct($itemId) | |
| { | |
| $this->itemId = $itemId; | |
| } | |
| public function handle(): void | |
| { | |
| $response = $this->checkStatus(); | |
| switch ($response['status']) { | |
| case 'success': | |
| $this->handleSuccess($response); | |
| break; | |
| case 'failed': | |
| $this->handleFailed($response); | |
| break; | |
| default: | |
| self::dispatch($this->itemId) | |
| ->delay(now()->addSeconds(30)); | |
| break; | |
| } | |
| } | |
| protected function checkStatus(): array | |
| { | |
| // Send HTTP request here | |
| } | |
| protected function handleSuccess(array $response): void | |
| { | |
| // Handle success here | |
| } | |
| protected function handleFailed(array $response): void | |
| { | |
| // Handle failed here | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment