Installation:
composer require effiana/cron-bundle
Add to config/bundles.php:
Effiana\CronBundle\EffianaCronBundle::class => ['all' => true],
Database Migration:
bin/console effiana:migration:load --force
(Alternative: Use make:migration + doctrine:migrations:migrate if preferred.)
First Use Case: Define a job via annotation in a service:
use Effiana\CronBundle\Annotation\Cron;
class MyJobService
{
/**
* @Cron("0 0 * * *", name="daily_report")
*/
public function generateDailyReport()
{
// Job logic here
}
}
Verify jobs are registered:
bin/console cron:list
Manual Execution:
bin/console cron:run
Job Definition:
@Cron("schedule", name="job_name").config/cron.yml (if supported; check docs).CronManager in a service:
$manager->addJob(new MyJob(), '0 * * * *');
Execution:
cron:run).crontab (Linux):
* * * * * /path/to/php /path/to/app/console cron:run >> /dev/null 2>&1
cron:start (if supported) or a worker process.Logging & Monitoring:
cron:list or Symfony’s monolog.JobInterface to add custom logging:
class MyJob implements JobInterface {
public function run() {
// ...
$this->logger->info('Job executed');
}
}
Dependency Injection:
class MyJob implements JobInterface {
private $mailer;
public function __construct(\Swift_Mailer $mailer) {
$this->mailer = $mailer;
}
}
Laravel-Specific:
Artisan facade to trigger jobs:
\Artisan::call('cron:run');
CronManager to Laravel’s container in AppServiceProvider:
$this->app->bind('effiana.cron.manager', function ($app) {
return new \Effiana\CronBundle\Manager\CronManager();
});
Queue Integration:
\Queue::push(new MyJob());
JobInterface::run() to use Laravel’s queue system.Event Listeners:
$dispatcher->addListener('cron.job.before', function ($job) {
// Pre-job logic
});
Database Schema:
--force flag.Cron Syntax:
* * * * *). Test schedules with crontab.guru.0 0 * * * = midnight daily).Timezone Issues:
config.php:
'timezone' => 'UTC',
Job Isolation:
Annotation Parsing:
doctrine/annotations is installed:
composer require doctrine/annotations
Job Not Running?:
bin/console cron:list
config.php:
'debug' => true,
Logs:
cron_job_logs table:
SELECT * FROM cron_job_logs ORDER BY created_at DESC;
Custom Job Storage:
JobRepository to use a custom storage backend (e.g., Redis):
$manager->setJobRepository(new RedisJobRepository());
Job Events:
$dispatcher->addListener('cron.job.after', function ($job, $result) {
// Post-job logic
});
Command Customization:
cron:run command in a custom bundle:
class CustomCronCommand extends CronCommand {
protected function execute(InputInterface $input, OutputInterface $output) {
// Custom logic
parent::execute($input, $output);
}
}
Laravel-Specific Extensions:
// app/Console/Commands/RunCronJobs.php
class RunCronJobs extends Command {
public function handle() {
\Artisan::call('cron:run');
}
}
Idempotency:
Environment-Specific Schedules:
config/cron.php:
'schedules' => [
'development' => ['0 * * * *' => ['job1', 'job2']],
'production' => ['0 0 * * *' => ['job1']],
],
Testing:
CronManager in PHPUnit:
$mockManager = $this->createMock(CronManager::class);
$mockManager->expects($this->once())->method('runJob');
$this->app->instance('effiana.cron.manager', $mockManager);
Performance:
$batchSize = 100;
for ($i = 0; $i < $totalItems; $i += $batchSize) {
$this->processBatch($i, $batchSize);
}
How can I help you explore Laravel packages today?