0

Running Yii2 on Ubuntu 16.04. I want to start a background task and then query it's status. The task starts fine, but queries from the client doesn't get a response until the server task ends.

Server Code

function actionRun($arguments)
{    
    $paramsJson = json_encode($arguments);
    $script = 'php /var/www/html/app/yii consolecontroller/action';
    $command = "{$script} '{$paramsJson}' > /dev/null 2>&1 &";
    exec($command);
}

Client Code

$('#buttonSubmit').on('click', function (event) {
    event.preventDefault();
    setTimeout(function () {updateJobProgress();}, 100);

    $.ajax({
        url: printForm.attr('action'),
        type: 'post',
        data: printForm.serialize()
    });

    function updateJobProgress() {
        var reportJob = $('input[name="reportJobId"]');
        $.ajax({
            url:reportJob.data('status'),
            data:{reportJobId: reportJob.val()},
            success:function(data) {
                if (data.progressStatus < 5000) {
                    reportJob.html('processing');
                } else {
                    reportJob.html('done');
                }
            }
        });
        setTimeout(function() {updateJobProgress();}, 700);
    }
});
zDaniels
  • 600
  • 1
  • 10
  • 21
  • 1
    Please check this [answer](https://stackoverflow.com/a/15273676/7818938) it may be helpful – vityapro Sep 08 '17 at 11:56
  • @vityapro thanks but the server is already returning a 200 response within 190ms. The problem is after it sends the 200, the server doesn't respond to any other requests until the server side exec() command finishes. – zDaniels Sep 08 '17 at 13:45
  • It looks like xdebug might be causing the problem. I will do more testing. – zDaniels Sep 08 '17 at 22:56
  • 1
    You might experience PHP session blocking since PHP is a single threaded. In order to allow for `parallel requests` (like pinging while previous request still running) you need to close the session with `session_write_close`. See explanation at https://codingexplained.com/coding/php/solving-concurrent-request-blocking-in-php. – lubosdz Sep 09 '17 at 19:26

1 Answers1

0

After some testing, I found that Yii2 was handling the ob flushing as well as the session. The AJAX call was returning a response 200 within 190ms, but subsequent ajax calls to check for status were not getting a response.

The reason subsequent requests were not getting a response was because of xdebug. I turned off xdebug and everything worked. I edited the file /etc/php/7.0/mods-available/xdegub.ini and changed xdebug.remote_autostart from 1 to 0. This stops xdebug from running all the time. Now, when I want to use xdebug, I just use an extension on my browser to activate it.

zDaniels
  • 600
  • 1
  • 10
  • 21