Installation:
composer require beats/beats-bundle
Add to config/bundles.php:
return [
// ...
Beats\BeatsBundle\BeatsBundle::class => ['all' => true],
];
First Use Case:
# config/services.yaml
services:
App\Service\MyBeat:
tags: ['beats.beat']
Beats\BeatsBundle\Beat\BeatInterface in your service:
use Beats\BeatsBundle\Beat\BeatInterface;
class MyBeat implements BeatInterface {
public function run() {
// Your logic here
}
}
config/beats.yaml:
beats:
my_beat:
class: App\Service\MyBeat
schedule: "*/5 * * * *" # Cron syntax
Trigger Manually (for testing):
php bin/console beats:run my_beat
Scheduling Beats:
@hourly, */10 * * * *, etc.) in beats.yaml.beats:
weekly_report:
class: App\Service\WeeklyReportBeat
schedule: "0 3 * * 1"
Dependency Injection:
class MyBeat implements BeatInterface {
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function run() {
$this->logger->info('Beat running!');
}
}
Event-Driven Triggers:
Beats\BeatsBundle\Event\BeatEvent to dispatch events before/after execution:
use Beats\BeatsBundle\Event\BeatEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MyBeatSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
'beats.my_beat.pre_run' => 'onPreRun',
'beats.my_beat.post_run' => 'onPostRun',
];
}
}
Command-Line Integration:
php bin/console beats:run all
php bin/console beats:list
Logging and Output:
public function run() {
echo "Running beat at " . date('Y-m-d H:i:s') . "\n";
}
Outdated Package:
symfony/dependency-injection, symfony/console).No Built-in Persistence:
Cron Syntax Only:
No Retry Mechanism:
public function run() {
try {
// Risky operation
} catch (Exception $e) {
// Log and retry (e.g., via Symfony Messenger)
}
}
Laravel Integration Quirks:
$app->register(new Beats\BeatsBundle\BeatsBundle());
symfony/console bridge for command-line access.Enable Verbose Output:
php bin/console beats:run my_beat -v
Check Configuration:
beats.yaml syntax (YAML errors may silently fail).php bin/console debug:config beats to inspect loaded beats.Test Locally:
// In tests, inject a mock BeatManager
$beatManager = $this->createMock(BeatManager::class);
$beatManager->expects($this->once())->method('runBeat')->with('my_beat');
Custom Schedulers:
Beats\BeatsBundle\Scheduler\SchedulerInterface for non-cron logic (e.g., event-based triggers).Beat Metadata:
setMetadata() in BeatInterface:
public function setMetadata(array $metadata) {}
Environment-Specific Beats:
beats.yaml per environment (e.g., beats.dev.yaml, beats.prod.yaml) via Symfony’s parameter system.Performance Optimization:
Process component to run them asynchronously:
use Symfony\Component\Process\Process;
public function run() {
$process = new Process(['php', 'script.php']);
$process->start();
}
How can I help you explore Laravel packages today?