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.
Install Dependencies
composer require dansan/jobboy-driver-doctrine dansan/jobboy
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
],
],
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
Verify Queue Table
Check your database for the job_queue table (or your custom table name). It should now store jobs.
Dispatching Jobs
// Single job
JobBoy::dispatch(new ProcessDataJob($payload));
// Batch dispatch
JobBoy::dispatchMany([$job1, $job2, $job3]);
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
}
Delayed Jobs
$job = new Job('App\Jobs\DelayedTask', ['data' => 'delayed']);
$job->setDelay(3600); // Delay in seconds
JobBoy::dispatch($job);
preFlush/postFlush to trigger job processing:
$entityManager->getEventManager()->addEventListener(
\Doctrine\ORM\Events::preFlush,
function () {
JobBoy::process(); // Process pending jobs
}
);
Dansan\JobBoy\Job for reusable logic:
class EmailJob extends Job {
public function run() {
// Custom logic
}
}
$job->setMaxRetries(3);
Transaction Conflicts
flush() explicitly.Missing Queue Table
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
);
Serialization Issues
__serialize()/__unserialize() in your job class.JobBoy::getLogger()->info('Job dispatched', ['job_id' => $job->getId()]);
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();
}
Priority Queues
Add a priority column and sort jobs accordingly:
// In your consumer
$jobs = $this->getRepository()->findPending()->orderBy('priority', 'ASC');
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();
}
}
How can I help you explore Laravel packages today?