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);
    }
});
4
  • 1
    Please check this answer it may be helpful Commented Sep 8, 2017 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. Commented Sep 8, 2017 at 13:45
  • It looks like xdebug might be causing the problem. I will do more testing. Commented Sep 8, 2017 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 codingexplained.com/coding/php/…. Commented Sep 9, 2017 at 19:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.