PHP code example of cboxdk / laravel-queue-autoscale
1. Go to this page and download the library: Download cboxdk/laravel-queue-autoscale library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Cbox\LaravelQueueAutoscale\Events\WorkersScaled;
use Cbox\LaravelQueueAutoscale\Events\SlaBreachPredicted;
Event::listen(WorkersScaled::class, function (WorkersScaled $event) {
Log::info("Scaled {$event->queue}: {$event->from} → {$event->to} workers");
Log::info("Reason: {$event->reason}");
});
Event::listen(SlaBreachPredicted::class, function (SlaBreachPredicted $event) {
// Alert when SLA breach is predicted
Notification::route('slack', env('SLACK_ALERT_WEBHOOK'))
->notify(new SlaBreachAlert($event->decision));
});
'sla_defaults' => [
// Maximum time a job should wait before being picked up (seconds)
'max_pickup_time_seconds' => 30,
// Minimum workers to maintain (even if queue is empty)
'min_workers' => 1,
// Maximum workers allowed for this queue
'max_workers' => 10,
// Cooldown period between scaling operations (seconds)
'scale_cooldown_seconds' => 60,
],
'prediction' => [
// How far ahead to forecast (seconds)
'forecast_horizon_seconds' => 60,
// When to trigger backlog drain (0-1, e.g., 0.8 = 80% of SLA time)
'breach_threshold' => 0.8,
],
'resource_limits' => [
// Maximum CPU usage percentage
'max_cpu_percent' => 90,
// Number of CPU cores to reserve
'reserve_cpu_cores' => 0.5,
// Maximum memory usage percentage
'max_memory_percent' => 85,
// Estimated memory per worker (MB)
'worker_memory_mb_estimate' => 128,
],
use Cbox\LaravelQueueAutoscale\Contracts\ScalingStrategyContract;
use Cbox\LaravelQueueAutoscale\Configuration\QueueConfiguration;
class CustomStrategy implements ScalingStrategyContract
{
public function calculateTargetWorkers(object $metrics, QueueConfiguration $config): int
{
// Your custom logic here
return (int) ceil($metrics->processingRate * 2);
}
public function getLastReason(): string
{
return 'Custom strategy: doubled the processing rate';
}
public function getLastPrediction(): ?float
{
return null; // Optional: estimated pickup time
}
}
use Cbox\LaravelQueueAutoscale\Contracts\ScalingPolicy;
use Cbox\LaravelQueueAutoscale\Scaling\ScalingDecision;
class NotifySlackPolicy implements ScalingPolicy
{
public function beforeScaling(ScalingDecision $decision): void
{
if ($decision->shouldScaleUp()) {
Slack::notify("About to scale up {$decision->queue}");
}
}
public function afterScaling(ScalingDecision $decision, bool $success): void
{
if (!$success) {
Slack::notify("Failed to scale {$decision->queue}");
}
}
}
use Cbox\LaravelQueueAutoscale\Events\WorkersScaled;
Event::listen(WorkersScaled::class, function (WorkersScaled $event) {
Metrics::gauge('queue.workers', $event->to, [
'queue' => $event->queue,
'action' => $event->action, // 'scaled_up' or 'scaled_down'
]);
});
use Cbox\LaravelQueueAutoscale\Events\SlaBreachPredicted;
Event::listen(SlaBreachPredicted::class, function (SlaBreachPredicted $event) {
$decision = $event->decision;
// Alert when pickup time is predicted to exceed SLA
if ($decision->isSlaBreachRisk()) {
PagerDuty::alert("SLA breach predicted for {$decision->queue}");
}
});
'log_channel' => 'stack', // Use your preferred channel
use Cbox\LaravelQueueMetrics\QueueMetrics;
// The ONLY source of queue data for autoscaling
$allQueues = QueueMetrics::getAllQueuesWithMetrics();
foreach ($allQueues as $queue) {
echo "Queue: {$queue->connection}/{$queue->queue}\n";
echo "Processing Rate: {$queue->processingRate} jobs/sec\n";
echo "Backlog: {$queue->depth->pending} jobs\n";
echo "Oldest Job: {$queue->depth->oldestJobAgeSeconds}s\n";
echo "Trend: {$queue->trend->direction}\n";
}