Skip to content

[job-queue] Change type hint from Closure to callable #286

Closed

Description

@msheakoski

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 to callable would maintain backwards compatibility and open up more options to the end user.

public function runUnique($ownerId, $name, \Closure $runCallback)

public function createDelayed($name, \Closure $startCallback)

public function runDelayed($jobId, \Closure $runCallback)

Activity

  1. msheakoski commented on Dec 4, 2017

    @msheakoski
    ContributorAuthor

    I recently learned on http://php.net/manual/en/functions.anonymous.php

    As of PHP 5.4.0, when declared in the context of a class, the current class is automatically bound to it, making $this available inside of the function's scope.

    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;
        }
    }
  2. makasim commented on Dec 4, 2017

    @makasim
    Member

    Could you open a PR?

  3. msheakoski commented on Dec 4, 2017

    @msheakoski
    ContributorAuthor

    The above example works without any changes. I was just sharing a workaround that I found for my initial problem after I learned that $this is accessible as the queue processor instance from inside the closure.

    I think that callable is still better than Closure because it gives the end user more flexibility for how they want to design their queue processor, such as moving the logic to an external class or domain object. I need to look through the enqueue code to see if anything specifically depends on closures then I will submit a PR if the change is okay to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions