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

Jobboy Driver Doctrine Laravel Package

dansan/jobboy-driver-doctrine

JobBoy Doctrine Driver integrates the JobBoy queue/worker system with Doctrine, providing a Doctrine-backed driver and repository support for storing and processing jobs. Designed to work with JobBoy bundles and Doctrine ORM/DBAL setups.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install Dependencies

    composer require dansan/jobboy-driver-doctrine dansan/jobboy
    
  2. Configure JobBoy with Doctrine Add the Doctrine driver to your JobBoy configuration in config/jobboy.php:

    'drivers' => [
        'doctrine' => [
            'enabled' => true,
            'connection' => 'default', // Your Doctrine connection name
            'queue_table' => 'job_queue', // Customize if needed
        ],
    ],
    
  3. First Use Case: Dispatching a Job

    use Dansan\JobBoy\JobBoy;
    use Dansan\JobBoy\Job;
    
    $job = new Job('App\Jobs\ProcessData', ['data' => 'example']);
    JobBoy::dispatch($job); // Uses Doctrine driver if configured
    
  4. Verify Queue Table Check your database for the job_queue table (or your custom table name). It should now store jobs.


Implementation Patterns

Workflow: Job Dispatching & Consumption

  1. Dispatching Jobs

    // Single job
    JobBoy::dispatch(new ProcessDataJob($payload));
    
    // Batch dispatch
    JobBoy::dispatchMany([$job1, $job2, $job3]);
    
  2. Consuming Jobs Use JobBoy's built-in consumer or integrate with Doctrine events:

    // In a Symfony command or controller
    $jobs = JobBoy::getRepository()->findPending();
    foreach ($jobs as $job) {
        $job->run(); // Execute the job
        $job->markAsDone(); // Update status
    }
    
  3. Delayed Jobs

    $job = new Job('App\Jobs\DelayedTask', ['data' => 'delayed']);
    $job->setDelay(3600); // Delay in seconds
    JobBoy::dispatch($job);
    

Integration Tips

  • Doctrine Events: Listen for preFlush/postFlush to trigger job processing:
    $entityManager->getEventManager()->addEventListener(
        \Doctrine\ORM\Events::preFlush,
        function () {
            JobBoy::process(); // Process pending jobs
        }
    );
    
  • Custom Job Classes: Extend Dansan\JobBoy\Job for reusable logic:
    class EmailJob extends Job {
        public function run() {
            // Custom logic
        }
    }
    
  • Retry Logic: Use JobBoy's retry mechanism:
    $job->setMaxRetries(3);
    

Gotchas and Tips

Pitfalls

  1. Transaction Conflicts

    • Jobs dispatched within a Doctrine transaction may not be immediately visible in the queue table.
    • Fix: Commit the transaction before dispatching or use flush() explicitly.
  2. Missing Queue Table

    • If the table doesn’t exist, jobs will silently fail.
    • Fix: Run migrations or create the table manually:
      CREATE TABLE job_queue (
          id INT AUTO_INCREMENT PRIMARY KEY,
          job_class VARCHAR(255) NOT NULL,
          payload TEXT,
          status ENUM('pending', 'processing', 'done', 'failed') DEFAULT 'pending',
          created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
          updated_at DATETIME ON UPDATE CURRENT_TIMESTAMP,
          delay INT DEFAULT 0,
          max_retries INT DEFAULT 0,
          retries INT DEFAULT 0
      );
      
  3. Serialization Issues

    • Complex objects (e.g., closures, resources) in job payloads will fail.
    • Fix: Use serializable data or implement __serialize()/__unserialize() in your job class.

Debugging

  • Log Job Status: Add logging to track job lifecycle:
    JobBoy::getLogger()->info('Job dispatched', ['job_id' => $job->getId()]);
    
  • Check Doctrine Events: Ensure your event listeners are registered and triggered.

Extension Points

  1. Custom Statuses Extend the status column in the queue table and update the driver:

    // In a custom repository
    public function findPending() {
        return $this->createQueryBuilder('j')
            ->where('j.status = :status')
            ->orWhere('j.status = :customStatus')
            ->setParameter('status', 'pending')
            ->setParameter('customStatus', 'custom_pending')
            ->getQuery()
            ->getResult();
    }
    
  2. Priority Queues Add a priority column and sort jobs accordingly:

    // In your consumer
    $jobs = $this->getRepository()->findPending()->orderBy('priority', 'ASC');
    
  3. Doctrine Event Subscribers Create a subscriber to auto-process jobs on specific events:

    use Doctrine\Common\EventSubscriber;
    use Doctrine\ORM\Event\LifecycleEventArgs;
    
    class JobProcessingSubscriber implements EventSubscriber {
        public function getSubscribedEvents() {
            return ['postPersist', 'postUpdate'];
        }
    
        public function postPersist(LifecycleEventArgs $args) {
            JobBoy::process();
        }
    }
    
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.
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
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle