Closed jackdpeterson closed 9 years ago
Hi !
The QueueAwareInterface, as documented here: https://github.com/juriansluiman/SlmQueue/blob/97e16e9d87b39e98184276d36faddb8dd85da23c/docs/4.QueueAware.md only works for job.
The problem by using this interface for your service is that even if you create an initializer, how the initializer could know which queue inject into a given service? It cannot know.
That's why I think injecting the queue directly is the best way:
class MyService
{
public function __construct(QueueInterface $queue)
{
}
}
This way you can control which queue to inject right into your factory.
Hope it helps! ;)
And an addition, inject the proper queue with your service factory:
public function createService($sl) {
$queueManager = $sl->get('SlmQueue\Queue\QueuePluginManager');
$queue = $queueManager->get('my-queue');
return new MyService($queue);
}
See also https://github.com/juriansluiman/SlmQueue/blob/master/docs/2.Configuration.md#queues
Thanks, that's exactly what I was looking for. This worked like a charm!
So close . . . It looks like I'm about halfway there to being able to submit jobs properly.
When using invokables things appear to work just fine (at least as far as submitting the job goes).
-- except now my dependency doesn't load (in this case the job depends on Doctrine ORM). if the job is missing the dependency then it'll yell about the same error as listed below -- just on the worker side of the equation.
When using factory-instantiated jobs -- it's another story. I'm not sure if my application is configured oddly or I'm just doing something wrong ... but here's what I'm getting:
$jobManager = $this->_queue->getJobPluginManager();
$job = $jobManager->get('MyFactoryInstantiatedJob');
$job->setContent($workload);
$this->_queue->push($job);
error:
Sep 1 23:34:22 com Zend\Log[1360]: PHP Fatal error: Call to protected Doctrine\ORM\EntityManager::__construct() from context 'Zend\ServiceManager\AbstractPluginManager' in /var/www/domains/com.project.example/vendor/zendframework/zend -servicemanager/src/AbstractPluginManager.php on line 207
service instantiation in module.config.php:
'ExampleService' => function ($serviceManager) {
$queueManager = $serviceManager->get('SlmQueue\Queue\QueuePluginManager');
$queue = $queueManager->get('default');
$service = new ExampleService($serviceManager->get('Doctrine\ORM\EntityManager'), $serviceManager->get('ExampleLoggerFacility'), $serviceManager->get('AcMailer\Service\MailService'), $queue);
return $service;
}
Any thoughts on why this would result in a doctrine error? I see that the JobPluginManager is a child of AbstractPluginManager.
Removing the code related to the JobPluginManager removes the exception -- so the exception definitely has something to do with how the job DI is being handled as far as I can tell.
Any thoughts on this would be helpful =)
I'm guessing that I need to inject in the Entity Manager using something along the lines of a worker strategy.
I've tried using the trait provided by doctrine orm module; however, that too didn't return a working entity manager.
As far as I understand the docs far it seems like I would need to:
But... I'm stuck. I'm not sure how or where I would pull from the shared event manager and provide that dependency into the local event manager on a per-job or per-worker basis.
If you have any dependencies in your job you should (well are encouraged) to create a factory for that particular job.
The factory must then be registered within the configuration of slm-queue for the job plugin manager (which is a service locator)
Then within your factory you should be able to pull any dependency from the service locator and pass it along to the job constructor. The global service locator can be pulled from the plugins managers service locator.
$sm->getServiceLocator()->get('doctrine.entitymanager.orm_default');
If you want I can pull up some code from one of mine as an example. Ping me. At a normal keyboard in an hour or so...
@basz
Thanks! That's actually the magic there that did the trick: adding in that parentlocator fetching tidbit.
$parentLocator = $serviceLocator->getServiceLocator()->get('doctrine.entitymanager.orm_default'); and then passing that in to my job as a dependecy.
Rockstar.
First of all, thanks to all of the contributors on this project. This is an absolutely fantastic module.
I do have one question after pouring through the documentation and perhaps someone has an answer to this.
I have most of my core busines functionality ran from service classes that are fat and really thin controller methods (Apigility REST api -- code connected). How does one access the Queue from a service like this? My initial assumption was to make my service implement the QueueAwareInterface; however, that didn't do what I expected. In fact, nothing appeared to happen --- no queue was injected using setter method injection.
My questions are as follows: