-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[job-queue] Change type hint from Closure to callable #286
Comments
I recently learned on http://php.net/manual/en/functions.anonymous.php
which means that the queue processor itself can utilize DI and more complex code can be broken down into class methods on the processor instance instead of putting all of the code inside of the closure. class MyConsumer implements PsrProcessor
{
protected $em;
protected $jobRunner;
public function __construct(
EntityManagerInterface $em,
JobRunner $jobRunner
) {
$this->em = $em;
$this->jobRunner = $jobRunner;
}
public function process(PsrMessage $message, PsrContext $session)
{
$id = $message->getBody();
if (!preg_match('/\A\d+\z/', $id)) {
return self::REJECT;
}
$result = $this->jobRunner->runUnique("job-$id", "myjob", function () use ($id) {
return $this->doTheWork($id);
});
return $result ? self::ACK : self::REJECT;
}
protected function doTheWork(string $id): bool
{
$repo = $this->em->getRepository(MyEntity::class);
/** @var MyEntity|null $entity */
$entity = $repo->find($id);
if (!$entity) {
return false;
}
// ... more work ...
return true;
}
} |
Could you open a PR? |
The above example works without any changes. I was just sharing a workaround that I found for my initial problem after I learned that I think that |
In the JobRunner (and possibly other areas), the type hint currently limits you to working with closures. For more complex jobs, it is nicer to have an object because you can utilize DI and autoloading. Changing
Closure
tocallable
would maintain backwards compatibility and open up more options to the end user.enqueue-dev/pkg/job-queue/JobRunner.php
Line 34 in 02f992c
enqueue-dev/pkg/job-queue/JobRunner.php
Line 66 in 02f992c
enqueue-dev/pkg/job-queue/JobRunner.php
Line 81 in 02f992c
The text was updated successfully, but these errors were encountered: