php
50 lines · 8 steps
A stable priority job queue in PHP
Wrapping SplPriorityQueue with a descending serial gives you priority ordering that stays FIFO for ties.
Explained by
highlit
1<?php
2
3namespace App\Queue;
4
5use Closure;
6use SplPriorityQueue;
7
8final class PriorityJobQueue
9{
10 private SplPriorityQueue $queue;
11 private int $serial = PHP_INT_MAX;
12
13 public function __construct()
14 {
15 $this->queue = new SplPriorityQueue();
16 $this->queue->setExtractFlags(SplPriorityQueue::EXTR_DATA);
17 }
18
19 public function push(Closure $job, int $priority = 0): void
20 {
21 $this->queue->insert($job, [$priority, $this->serial--]);
22 }
23
24 public function isEmpty(): bool
25 {
26 return $this->queue->isEmpty();
27 }
28
29 public function count(): int
30 {
31 return $this->queue->count();
32 }
33
34 public function drain(): array
35 {
36 $results = [];
37
38 while (!$this->queue->isEmpty()) {
39 $job = $this->queue->extract();
40
41 try {
42 $results[] = $job();
43 } catch (\Throwable $e) {
44 $results[] = new FailedJob($e);
45 }
46 }
47
48 return $results;
49 }
50}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A composite priority of [priority, serial] breaks ties deterministically so equal-priority jobs keep insertion order.
- 2Counting the serial downward makes earlier inserts rank higher when priorities match, preserving FIFO within a tier.
- 3Catching Throwable around each job lets the whole batch drain even when individual jobs fail.
Related explainers
typescript
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, DeepPartial } from 'typeorm'; import { User } from './entities/user.entity';
Building a CRUD service in NestJS
crud
dependency-injection
repository-pattern
Intermediate
7 steps
php
<?php namespace App\Http\Controllers\Api;
Cursor pagination in a Laravel API
pagination
cursor
keyset
Intermediate
9 steps
typescript
type AsyncMethod = (...args: any[]) => Promise<any>; function LogExecutionTime(thresholdMs = 0) { return function <T extends AsyncMethod>(
A method decorator that times async calls
decorators
higher-order-functions
async
Advanced
9 steps
typescript
import { Controller, Get, Param, Query, Redirect, NotFoundException } from '@nestjs/common'; import { LinksService } from './links.service'; @Controller('links')
Handling HTTP redirects in NestJS
redirects
routing
decorators
Intermediate
7 steps
php
<?php namespace App\Support;
Normalizing phone numbers to E.164 in PHP
input-validation
normalization
regular-expressions
Intermediate
8 steps
php
final class Route { private string $regex; private array $paramNames = [];
Compiling route patterns into regex in PHP
routing
regex
parsing
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-stable-priority-job-queue-in-php-explained-php-bb69/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.