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

Resque Bundle Laravel Package

bcc/resque-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require bcc/resque-bundle
    

    Add to config/bundles.php:

    return [
        // ...
        BCC\ResqueBundle\BCCResqueBundle::class => ['all' => true],
    ];
    
  2. Configuration Edit config/packages/bcc_resque.yaml:

    resque:
        redis:
            host: '127.0.0.1'
            port: 6379
            password: null
        queues: ['default', 'high_priority']
    
  3. First Job Create a job class:

    namespace App\Jobs;
    
    use BCC\ResqueBundle\Job\JobInterface;
    
    class ProcessUser implements JobInterface
    {
        public function perform($data)
        {
            // Your logic here
        }
    }
    
  4. Enqueue a Job

    use BCC\ResqueBundle\Job\Job;
    
    $job = new Job('App\Jobs\ProcessUser', ['data' => $userData]);
    $job->enqueue();
    
  5. Run Worker

    php bin/console resque:worker default
    

First Use Case: Background Email Processing

// In your controller
$job = new Job('App\Jobs\SendWelcomeEmail', ['userId' => $user->id]);
$job->enqueue();

// In your job
public function perform($data)
{
    Mail::to($data['userId'])->send(new WelcomeEmail());
}

Implementation Patterns

Job Design Patterns

  1. Dependency Injection Use constructor injection for services:

    class ProcessOrder implements JobInterface
    {
        private $orderService;
    
        public function __construct(OrderService $orderService)
        {
            $this->orderService = $orderService;
        }
    
        public function perform($data)
        {
            $this->orderService->process($data['orderId']);
        }
    }
    
  2. Job Chaining Enqueue follow-up jobs:

    public function perform($data)
    {
        if ($data['needsFollowUp']) {
            $followUpJob = new Job('App\Jobs\FollowUpTask', $data);
            $followUpJob->enqueue();
        }
    }
    
  3. Job Retry Logic Implement shouldRetry() and retryDelay():

    public function shouldRetry($failedAttempts, $data)
    {
        return $failedAttempts < 3;
    }
    
    public function retryDelay($failedAttempts, $data)
    {
        return 60 * $failedAttempts; // Exponential backoff
    }
    

Workflow Patterns

  1. Dynamic Queue Assignment

    $job = new Job('App\Jobs\HeavyTask', $data);
    $job->setQueue($priority ? 'high_priority' : 'default');
    $job->enqueue();
    
  2. Bulk Processing

    foreach ($users as $user) {
        $job = new Job('App\Jobs\ProcessUser', ['userId' => $user->id]);
        $job->enqueue();
    }
    
  3. Worker Management

    • Run multiple workers:
      php bin/console resque:worker default --count=4
      
    • Monitor queues:
      php bin/console resque:stats
      

Integration Tips

  1. Symfony Events Listen to job events:

    // config/services.yaml
    BCC\ResqueBundle\EventListener\JobListener:
        tags:
            - { name: kernel.event_listener, event: resque.job.enqueued, method: onJobEnqueued }
    
  2. Doctrine Integration Use transactions in jobs:

    public function perform($data)
    {
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->beginTransaction();
        try {
            // Process data
            $entityManager->commit();
        } catch (\Exception $e) {
            $entityManager->rollBack();
            throw $e;
        }
    }
    
  3. API Job Triggering Create a controller endpoint:

    #[Route('/jobs/process', name: 'app.job.process', methods: ['POST'])]
    public function triggerJob(Request $request)
    {
        $job = new Job('App\Jobs\ProcessData', $request->request->all());
        $job->enqueue();
    
        return new JsonResponse(['status' => 'queued']);
    }
    

Gotchas and Tips

Common Pitfalls

  1. Redis Connection Issues

    • Ensure Redis is running and accessible.
    • Check config/packages/bcc_resque.yaml for correct credentials.
    • Use resque:stats to verify connection.
  2. Job Serialization

    • Only serializable data can be passed to jobs. Avoid passing:
      • Closures
      • Resources
      • Non-serializable objects
    • Use json_encode()/json_decode() for complex data.
  3. Worker Crashes

    • Workers may crash if they don’t handle exceptions. Always wrap logic in try-catch:
      public function perform($data)
      {
          try {
              // Job logic
          } catch (\Exception $e) {
              // Log error
              throw $e; // Re-throw to trigger retry
          }
      }
      
  4. Queue Stuck Jobs

    • Use resque:remove to clean stuck jobs:
      php bin/console resque:remove App\Jobs\StuckJob
      

Debugging Tips

  1. Logging Enable job logging in config/packages/bcc_resque.yaml:

    resque:
        logging: true
    

    Check logs at var/log/resque.log.

  2. Job Inspection

    • List queued jobs:
      php bin/console resque:list
      
    • Inspect a specific job:
      php bin/console resque:inspect App\Jobs\ProcessUser
      
  3. Worker Debugging

    • Run worker in debug mode:
      php bin/console resque:worker default --debug
      
    • Use Xdebug for deeper inspection.

Configuration Quirks

  1. Redis Prefix Customize Redis keys with:

    resque:
        redis:
            prefix: 'myapp_resque_'
    
  2. Worker Timeout Adjust worker timeout (default: 25s):

    resque:
        worker_timeout: 60
    
  3. Environment-Specific Config Use environment variables:

    resque:
        redis:
            host: '%env(RESQUE_REDIS_HOST)%'
            password: '%env(RESQUE_REDIS_PASSWORD)%'
    

Extension Points

  1. Custom Job Classes Extend BCC\ResqueBundle\Job\Job for custom behavior:

    class CustomJob extends Job
    {
        public function __construct(string $class, array $args, array $options = [])
        {
            parent::__construct($class, $args, $options);
            $this->setQueue('custom_queue');
        }
    }
    
  2. Event Subscribers Create custom events:

    class CustomJobEvent extends JobEvent
    {
        public function __construct(JobInterface $job, string $eventName)
        {
            parent::__construct($job, $eventName);
        }
    }
    
  3. Middleware Add middleware to jobs:

    $job = new Job('App\Jobs\ProcessData', $data);
    $job->addMiddleware(new LoggingMiddleware());
    $job->enqueue();
    

Performance Tips

  1. Batch Processing Process jobs in batches to reduce Redis overhead:

    $batchSize = 20;
    $batch = array_slice($items, 0, $batchSize);
    foreach ($batch as $item) {
        $job = new Job('App\Jobs\ProcessItem', ['item' => $item]);
        $job->enqueue();
    }
    
  2. Queue Prioritization Use multiple queues for priority management:

    resque:
        queues:
            - { name: 'critical', priority: 1 }
            - { name: 'default', priority: 2 }
            - { name: 'low', priority: 3 }
    
  3. Worker Concurrency Adjust worker concurrency based on system resources:

    php bin/console resque:worker default --concurrency=5
    
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.
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon