Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Gearman Stats Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require necromant2005/gearman-stats
    

    Ensure gearman/gearman is also installed (dependency).

  2. Basic Usage Initialize the client and start collecting stats:

    use Necromant2005\GearmanStats\GearmanStats;
    
    $stats = new GearmanStats();
    $stats->start(); // Begin tracking stats
    
  3. 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();
    });
    

Implementation Patterns

Workflow Integration

  1. 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);
    }
    
  2. 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());
        }
    }
    
  3. 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
    

Gotchas and Tips

Pitfalls

  1. Thread Safety

    • GearmanStats is not thread-safe. Avoid concurrent calls to start()/endBatch().
    • Fix: Use a singleton or request-scoped binding in Laravel.
  2. Memory Leaks

    • Long-running batches may accumulate stats in memory.
    • Fix: Call flush() periodically or use endBatch() explicitly.
  3. Gearman Worker Context

    • Stats collected on the client side won’t reflect worker performance.
    • Workaround: Pair with gearman/worker logging for full visibility.

Debugging

  • Enable Verbose Logging
    $stats->setLogLevel(GearmanStats::LOG_DEBUG);
    
  • Check for Silent Failures Wrap trackJob() in a try-catch to log unhandled errors:
    try {
        $stats->trackJob('fragile_task', fn() => $this->riskyOperation());
    } catch (\Exception $e) {
        $stats->logError($e->getMessage());
    }
    

Extension Points

  1. Custom Metrics Extend the StatsCollector interface to add fields:

    $stats->extend(function($collector) {
        $collector->set('custom_metric', $this->calculateCustomValue());
    });
    
  2. Output Formats Override getStats() to return JSON/CSV:

    $stats->setFormatter(function($data) {
        return json_encode($data);
    });
    
  3. Storage Backends Replace the default logger with a database writer:

    $stats->setLogger(new DatabaseLogger());
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky