Webador / SlmQueueBeanstalkd

Beanstalkd adapter for SlmQueue module
Other
10 stars 26 forks source link

Problem with Job and Dependencies #35

Closed teseo closed 10 years ago

teseo commented 10 years ago

Hello,

I am new in Zend Framework 2 and it could be a newbie thing. I can't get my dependences into a job

My slm_queue.global looks like:

    'job_manager' => array(
        'factories' => array(
            'HelloWorldJob' => 'Module\Factory\HelloWorldJobFactory',
        ),
    ),

    'queue_manager' => array(
        'factories' => array(
            'my-queue' => 'SlmQueueBeanstalkd\Factory\BeanstalkdQueueFactory'
        )
    ),
),

);

I am injecting my dependences in the controller by editing Modules.php and implementing:

public function getControllerConfig()
{
    return array(
        'factories' => array(
            'Module\Controller\Index' => function($cm) {
                    return new Controller\IndexController(
                        $cm->getServiceLocator()->get('translator'),
                        $cm->getServiceLocator()->get('SlmQueue\Queue\QueuePluginManager')->get('my-queue'),
                        $cm->getServiceLocator()->get('SlmQueue\Job\JobPluginManager')
                    );
                },
        ),
    );
}

The controller has exactly the right dependences looking the constructor like this:

public function __construct(\Zend\Mvc\I18n\Translator $translator, BeanstalkdQueue $queue, \SlmQueue\Job\JobPluginManager $jobManager)
{
    $this->translator = $translator;
    $this->queue = $queue;
    $this->jobManager = $jobManager;

}

Once I push the job to the queue I do it like this:

    $job = $this->jobManager->get('HelloWorldJob');
    $job->setContent(array(
        'subject' => 'Just hello'  . PHP_EOL,
    ));
    $this->queue->push($job);

As a matter of fact, If I var_dump the job here, The job has the dependence perfectly injected. Once I push it in the queue is when it got lost.

HelloWorldJobFactory looks like

class HelloWorldJobFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $sl) { /* @var Chat $randomService / $randomService = $sl->getServiceLocator()->get('random-service'); $job = new HelloWorldJob($randomService); return $job; } }

The job's constructor looks like:

public function __construct(RandomService $service)
{
    $this->service = $service;
}

Just following the Documentation, when I execute:

php public/index.php queue beanstalkd my-queue

The error is:

PHP Catchable fatal error: Argument 1 passed to Module\Job\HelloWorldJob::__construct() must be an instance of Core\Service\randomService, none given, called in /var/www/liquid/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php on line 170 and defined in /var/www/liquid/module/Module/src/Front/Job/HelloWorldJob.php on line 14

The dependence is not getting injected into the Job once I push it but in the Controller the job has everything setup properly.

I am completely lost, I would appreciate any help.

Many thanks!

juriansluiman commented 10 years ago

This is unfortunately a known issue in SlmQueue: juriansluiman/SlmQueue#85. The problem is during pop() (so in the "worker phase" of SlmQueue) where the class name of the job is used to create the job. For creation, the queue uses the plugin manager but the job simply reports its FQCN back as class.

So, if you have a job with a service name which is not the FQCN of the job, it will fail. In your example, this fails:

'job_manager' => array(
    'factories' => array(
        'HelloWorldJob' => 'Module\Factory\HelloWorldJobFactory',
    ),
),

This will work:

'job_manager' => array(
    'factories' => array(
        'Module\Job\HelloWorldJob' => 'Module\Factory\HelloWorldJobFactory',
    ),
),

Notice the service name of the job is set to its FQCN (Module\Job\HelloWorldJob).

teseo commented 10 years ago

Hello,

Many thanks for your quick response, it is very appreciated as creating a queue system for our company was core for our progress.

Indeed. Once I changed the name for its FQCN, everything worked as expected and brilliantly.

Thank you very much for your work and, again, for your quick response. The documentation is very good and clear, in one single day I managed to install it and make it work without dependencies and, after this response, with the dependences as well.

Have a nice day, you made an engineer happy today :+1:

juriansluiman commented 10 years ago

Closing since this issue is resolved.

BTW: SlmQueue allows now to have a job name which is not the FQCN. It has been resolved in v0.4.0-beta1 so if you want to use this, either upgrade to a beta version or wait until v0.4 has been tagged stable.