Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send to background action right before response is sent

Tags:

php

yii

I've been reading a lot but everything I've tried didn't work at all. I'll try to briefly explain my problem and let's see if there's a solution. I'm using a web app developed with Yii.

The process is:

- Someone send a form to our web.
- Save user info in our DB
- Send that info to another web.
- Build a log and send it to admin
- 'OK' is sent to the user.

All this process takes about 20 seg. Too long before the user can leave the page. So the thing is I need the user to receive the 'OK' right after his info is saved. Something like this:

- Someone send a form to our web.
- Save user info in our DB.
- 'OK' is sent to the user.
- Send that info to another web.
- Build a log and send it to admin.

I need to make the process asynchronous so after response is sent, the process keep running in the background.

I have 3 methods in the model: - method1 -> save user info. - method2 -> send info. - method3 -> built and send report.

In the controller I have a method that is called by the web who sends the form, which fires all the process and finally sends the 'OK' to the web.

Its something like this:

public function actionAux(){
    $form = new Form();
    $form->method1;
    $form->method2;
    $form->method3;

    $this->response(); // This send the 'OK'
}

Well, I need these methods to be fire in that order but 2 & 3 run in background so response can be send right after user info is saved (method1).

Hope I have explain myself and I can find a solution. Thanks in advance.

like image 524
pelasO Avatar asked Dec 12 '25 22:12

pelasO


1 Answers

You can not break any linear process. But to support your type of requirements, you need Queing tools like (ZeroMQ, BeanstalkD, GearMan etc...).

These type of tools are very light, Multi threaded and excellent durability. In my current project, I'm using BeanstalkD. We had requirement to send message to 10,000 Users but don't want sender to wait till.

  1. ZeroMQ support for php
  2. Yii (php) extension also available for beanstalk
  3. GearMan for php

All The best !!

like image 192
Sanjay Mohnani Avatar answered Dec 15 '25 12:12

Sanjay Mohnani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!