Subscribe to an event
Register listeners inboot() through the event mapper:
src/Plugin.php
Write a listener
A listener is an invokable class. It’s resolved from the container, so it can depend on any service:src/Listeners/PushContact.php
Priorities
Priority::HIGH (100) runs before Priority::NORMAL (50), which runs before Priority::LOW (0).
Inheritance matters
Listeners match withinstanceof, so a listener on a parent class also receives its subclasses.
Events you can subscribe to
Class names are under
<Module>\Domain\Events\. See the event catalog for the full list, including who listens in core.
Dispatch your own events
Scheduled work
There’s no queue. Recurring work runs whencron.php executes, which dispatches Cron\Domain\Events\CronEvent. Subscribe to it like any other event.
src/Listeners/SyncContacts.php
- Opt in. Do nothing unless an administrator started the job, so an idle installation pays nothing for your plugin.
- Batch. Process a fixed number of records per tick.
- Persist a cursor. Store the last processed ID in an option, so the next tick continues instead of restarting.
- Track counters and a status.
processing,pausedandcompletedgive the admin page something to show, and stopping is just a status change. - Rate-limit yourself. For a daily job, store a timestamp and return early until it’s due.
Entity changes made in cron listeners are flushed after the event finishes, the same as in a web request.
Testing
- Run the cron tick by hand with
php cron.php. - Trigger domain events by doing the thing in the UI, such as creating a user.
- Watch
var/log/app-*.log, and log at the start and end of your listener while developing.