vivait / delayed-event-bundle
A bundle that delays events for a configurable time period (i.e. 24 hours)
Installs: 26 822
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 5
Forks: 1
Open Issues: 0
Requires
- php: ^7.4|^8.0
- ext-json: *
- leezy/pheanstalk-bundle: 4.x-dev@dev
- symfony/config: ^4.4|^5.4
- symfony/console: ^4.4|^5.4
- symfony/dependency-injection: ^4.4|^5.4
- symfony/event-dispatcher: ^4.4|^5.4
- symfony/framework-bundle: ^4.4|^5.4
- symfony/monolog-bundle: ~3.0
- symfony/process: ^4.4|^5.4
- symfony/uid: ^5.2|^6.0
- vivait/backoff: ^1.1
- wrep/daemonizable-command: ~3.1
Requires (Dev)
- behat/behat: ^3.0
- doctrine/doctrine-bundle: ^2.0
- doctrine/orm: ~2.2
- matthiasnoback/symfony-config-test: ^4.0
- phpunit/phpunit: ^9.0
- psalm/plugin-symfony: ^3.1
- symfony/phpunit-bridge: ^6.0
- symfony/polyfill-php74: ^1.26
- symfony/polyfill-php80: ^1.26
- symfony/polyfill-php81: ^1.26
- symfony/symfony: ^4.4|^5.4
- videlalvaro/php-amqplib: ~2.2
- vimeo/psalm: ^4.23
- yogarine/doctrine-annotation-autoload: ^0
- 2.x-dev
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.0.0
- 0.x-dev
- 0.14.2
- 0.14.1
- 0.14.0
- 0.13.1
- 0.13.0
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.0
- dev-fix/adhoc-2812-store-job-id
- dev-feature/synchronous
- dev-bump-symfony-uid
This package is auto-updated.
Last update: 2024-10-30 01:37:12 UTC
README
Triggers a Symfony event an arbitrary period after the original event
Configure the service
vivait_delayed_event: queue_transport: beanstalkd # default
Beanstalkd queue transport
This relies on pheanstalk
being installed and setup in your config. You can pass extra information to the beanstalk queue using the configuration
parameter:
Be aware TTR is the time a process can run before it effectively retries, if it's too short there is a realistic possibility that a job will be processed twice.
vivait_delayed_event: queue_transport: name: beanstalkd configuration: tube: my_tube ttr: 60
Creating a delayed event
Instead of tagging the event with a kernel tag, tag the event with a delayed_event
tag and provide a delay:
# app/config/services.yml services: app.your_listener_name: class: AppBundle\EventListener\AcmeListener tags: - { name: delayed_event.event_listener, delay: '24 hours', event: app.my_event, method: onMyEvent }
By default, any integer will be treated as seconds. The bundle will use PHP's textual datetime parsing to parse a textual datetime string in to seconds like in the example above.
Transformers
If you're delaying an event, rather than store the exact state of an entity at the time of the event, you'll probably want to receive the latest version of the entity. The bundle allows the usage of transformers, which are ran before each property of an event is serialized. By default, the bundle has an entity transformer enabled, which will detect any entities in an event and store a reference to an entity. This means that when the entity is unserialized for the delayed event, a fresh entity is loaded from the database.
You can enable/disable transformers on a global level:
# app/config.yml vivait_delayed_event: storage: delayed_event_cache transformers: doctrine: disabled
You can also enable/disable them when tagging an event listener:
# app/config/services.yml services: app.your_listener_name: class: AppBundle\EventListener\AcmeListener tags: - { name: delayed_event.event_listener, delay: '24 hours', event: app.my_event, method: onMyEvent, transformers: [doctrine] }
Note: The enabled
part is optional, and in the example above has been left out for brevity.
You can create custom transformers by implementing the TransformerInterface
interface, like so:
class AcmeTransformer implements TransformerInterface { /** * @param ReflectionProperty $property * @return bool */public function supports(\ReflectionProperty $property) { $property->getValue(); return is_object($data) && $this->doctrine->contains($data); } /** * @param $data * @return array */public function serialize($data) { // Get the ID $id = $this->doctrine->getMetaData($data)->getIdentifierFieldNames(); $class = get_class($data); /* @var UnitOfWork $uow */ $uow = $this->doctrine->getUnitOfWork(); $id = $uow->getDocumentIdentifier($data); return [ $class, $id ]; } /** * @param $data * @return mixed */public function deserialize($data) { [$class, $id] = $data; return $this->doctrine->getRepository($class)->find($id); } }
You must then tag the custom transformer:
# app/config/services.yml services: app.your_transformer_name: class: AppBundle\EventTransformer\AcmeTransformer tags: - { name: your_transformer_name }