Jobs can be executed with two types of parameters: serializable parameters and runtime parameters. Runtime parameters are provided from a context. They can be added to this context by registering an event listener.
The AbcJobBundle provides the default parameters @abc.logger and @abc.manager. If specified an instance of Psr\Log\LoggerInterface respectively Abc\Abc\Bundle\JobBundle\Job\ManagerInterface is injected into the job.
namespace My\Bundle\ExampleBundle\Job\MyJob;
use Abc\Bundle\JobBundle\Annotation\JobParameters;
use Abc\Bundle\JobBundle\Job\ManagerInterface;
class ManagerAwareJob
{
/**
* @ParamType("manager", type="@abc.manager")
*/
public function sayHello(ManagerInterface $manager)
{
$manager->...
}
}To inject a custom runtime parameter you need to do two things:
- Create a listener class
- Register the listener class in the service container
First you have to create the listener class that sets the runtime parameter into the execution context of a job:
class MyJobListener
{
private $factory;
// ...
public function onPreExecute(ExecutionEvent $event)
{
$parameter = $this->factory->create('something');
$event->getContext()->set('my_custom_param', $parameter);
}
}The event passed to the listener is of type ExecutionEvent which provides access to the execution context of a job. This is a simple container where you can register parameters with a certain key.
Next you need to register the listener in the service container and tag it:
<service id="my_job_listener" class="MyJobListener" public="true">
<argument type="service">...</argument>
<tag name="abc.job.event_listener" event="abc.job.pre_execute" method="onPreExecute"/>
</service>You can now use this runtime parameter inside your jobs:
/**
* @ParamType("myParam", type="@my_custom_param")
*/
public function doSomething($myParam) {
}Back to index