E
E
e-hot2016-12-07 13:11:17
symfony
e-hot, 2016-12-07 13:11:17

How to make another service start on an event in a method of one service with parameter passing?

Greetings.
I have never had to use the events functionality in SF2 projects before, now I have to do it, but ... I studied the materials on the Internet - it's creepy how this whole topic is described and everyone has it in their own way - it was not possible to collect a single "picture" of the vision of Events in SF2 , therefore, if anyone is able to point out what can be done in the following situation:
1. The service in one of its methods, upon the fact of a specific if condition, must make it so that the method of another service starts with one parameter from the current one. The key word - STARTED - like as if by himself. It is clear that you can just stupidly start the very method of the desired service by calling it from the current method, but then the current method of the current service will finish working only after the completion of the work of that running method of the desired service - and I don’t need this, it is necessary that another one is launched the process went in parallel and "independently" of the current one.
2. In other words, it suggests catching the desired event in the system, taking a parameter and starting a certain process as a service using the passed parameter.
3. While I understand that in the method of the current service, you need to register the creation of an event, and the starting service will be a listener or subscriber - the service must also be turned into a listener. Is it necessary to create a listener class, etc.?
Can anyone explain this whole process.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Skobkin, 2016-12-07
@skobkin

I studied the materials on the internet - it's terrible how this whole topic is described and each in its own way - it was not possible to collect a single "picture"

You don't need any materials on the Internet. There is official documentation , which normally describes the essence of how events work in Symfony.
What prevents to call from service A service B?
It's like nothing starts up by itself.
If you trigger an event, exactly the same thing will happen. Parallel execution has little to do with exceptions.
You do not have to use the events as you described them. But if you do, you will need to make an EventListener or EventSubscriber. Read the documentation.
In general, POSSIBLE, you need queues. That is, you can look towards RabbitMQ, ZeroMQ, etc. The essence will be this:
You have a queue server and a pool of workers that constantly poll the queue server for new jobs.
In your PHP code, the service sends a message to the queue server with the job parameters and continues to do its job. At the same time, one of the pool workers receives this job and starts processing it. But all this does not happen within the framework of one session of the Symfony application, but roughly speaking, two separate applications work that are connected through the queue server: your Symfony application that sends the task to the queue and the worker (s) that process the queue (in general, there may be written in another language).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question