necromant2005/gearman-stats
Laravel package for fetching and parsing Gearman server statistics. Provides an easy API to query job queues, workers, and status metrics from gearmand, helping you monitor workload and troubleshoot background job processing in your app.
Installation
composer require necromant2005/gearman-stats
Ensure gearman/gearman is also installed (dependency).
Basic Usage Initialize the client and start collecting stats:
use Necromant2005\GearmanStats\GearmanStats;
$stats = new GearmanStats();
$stats->start(); // Begin tracking stats
First Use Case Wrap Gearman job execution to log metrics:
$client = new \GearmanClient();
$client->addServer();
$stats->trackJob('process_data', function() use ($client) {
$client->addTask('process_data', 'data');
return $client->runTasks();
});
Middleware for Job Tracking Create middleware to auto-wrap Gearman calls:
// app/Http/Middleware/GearmanStatsMiddleware.php
public function handle($request, Closure $next) {
$stats = app(GearmanStats::class);
$stats->trackJob('webhook_processing', function() use ($request) {
// Gearman logic here
});
return $next($request);
}
Queue Job Wrapper
Extend Laravel’s Job class to include stats:
class GearmanJob implements ShouldQueue {
use Dispatchable, InteractsWithQueue;
public function handle() {
$stats = app(GearmanStats::class);
$stats->trackJob('email_sending', fn() => $this->sendEmail());
}
}
Batch Processing Track performance of bulk operations:
$stats->startBatch('user_import');
foreach ($users as $user) {
$client->addTask('import_user', $user);
}
$stats->endBatch(); // Logs total time + per-job metrics
Thread Safety
GearmanStats is not thread-safe. Avoid concurrent calls to start()/endBatch().Memory Leaks
flush() periodically or use endBatch() explicitly.Gearman Worker Context
gearman/worker logging for full visibility.$stats->setLogLevel(GearmanStats::LOG_DEBUG);
trackJob() in a try-catch to log unhandled errors:
try {
$stats->trackJob('fragile_task', fn() => $this->riskyOperation());
} catch (\Exception $e) {
$stats->logError($e->getMessage());
}
Custom Metrics
Extend the StatsCollector interface to add fields:
$stats->extend(function($collector) {
$collector->set('custom_metric', $this->calculateCustomValue());
});
Output Formats
Override getStats() to return JSON/CSV:
$stats->setFormatter(function($data) {
return json_encode($data);
});
Storage Backends Replace the default logger with a database writer:
$stats->setLogger(new DatabaseLogger());
How can I help you explore Laravel packages today?