多个ajax请求给出504网关超时

时间:2014-12-01 16:00:15

标签: php jquery ajax

我正在使用PHP构建一个非常大的pdf书,我使用ajax来触发脚本。我的问题是,因为它太大了超时,所以我决定打破它并递归调用脚本几次。几个星期前我测试了这个,很好。但是现在我得到一些ajax调用的随机“504网关超时”问题。

我不知道在哪里开始调试这个,因为我相信做几个ajax调用的全部目的是为了防止这个问题。不幸的是,我没有访问服务器作为它在共享主机包上。

<?php $i =1; for($i;$i<=$number_of_pages;$i++) { ?>
<?php if ($i == $number_of_pages) { $last = 1; } else { $last = 0; } ?>
<script>
$(document).ready(function() {
    jQuery('.waiting-gif').show(); 
    var number_of_pages = '<?php echo $number_of_pages; ?>';
    var build_progress_width = 480/number_of_pages;

    var last = '<?php echo $last; ?>';

    $.ajax({
        url: 'index.php?route=pdfengine/pdfengine/trigger_pdf_gen',
        type: 'post',
        dataType: 'json',
        data: {     
            pdf_id : '<?php echo $this->request->get['pdf_id']; ?>',
            page_number: '<?php echo $i; ?>',
        },
        beforeSend: function() {



        },
        complete: function() {},
        success: function(json) {
            $('.build-progress-green').css('width', build_progress_width);
            $('.pdf-build-progress').append('<div class="build-progress-green"></div>');




            if (last == '1') {

                $.ajax({
                        url: 'index.php?route=pdfengine/pdfengine/merge_pdfs',
                        type: 'post',
                        dataType: 'json',
                        data: {     
                            pdf_id : '<?php echo $this->request->get['pdf_id']; ?>',

                        },
                        beforeSend: function() {},
                        complete: function() {},
                        success: function(json) {

                            var link = document.getElementById('downloadlink');
                            // Set the link to the json response
                            link.href = json['download_link'];
                            $('#downloadlink').attr('download','reads-pdf-id-' + json['pdf_id'] + '.pdf');
                            // Unhide the link
                            link.style.display = 'block';
                            $('#downloadlink').trigger('click');
                            $('.waiting-gif').hide();



                        }
                    });
            }
        }


        });
    });



</script>
<?php } ?>

通过ajax调用主函数...

 public function trigger_pdf_gen() {

    if(isset($this->request->post['pdf_id'])) {

        $this->load->model('pdfengine/pdfengine');       

        $pdf_info = $this->model_pdfengine_pdfengine->getPdfInfo($this->request->post['pdf_id']);

        $page_number = $this->request->post['page_number'];

        $this->session->data['childs_name'] = $pdf_info[0]['childs_name'];
        $this->session->data['pdf_id'] = $pdf_info[0]['pdf_id'];    
        $this->session->data['gift_message'] = $pdf_info[0]['gift_message'];                
        $double_pages = explode(',',$this->model_pdfengine_pdfengine->getDoublePages($pdf_info[0]['book_path']));
        $is_double_page = 0;
        foreach ($double_pages as $double_page) {


            if ($double_page == $page_number) { $is_double_page = 1;  } 

        }

        $this->generate_final_pdf($pdf_info[0]['pdf_id'],$pdf_info[0]['book_path'],$pdf_info[0]['childs_gender'], $page_number,$is_double_page );


        $json = array();
        $json[] = "<a href='".DIR_FINAL_PDFS_PUBLIC.$pdf_info[0]['pdf_id']."/final-build-".$pdf_info[0]['pdf_id'].".pdf'>The file :)</a>";
        $this->response->setOutput(json_encode($json));

    }        


 }

这可能是与服务器相关的临时问题吗?

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方法,我不会把它称为解决方案,但它能完成这项工作。

我取消了for循环并使用javascript强制延迟ajax请求的每个触发器。

var counter = 1;
trigger_pdf();
var i = setInterval( trigger_pdf, 61000); 

function trigger_pdf() {

$.ajax({
    url: 'index.php?route=pdfengine/pdfengine/trigger_pdf_gen',
    type: 'post',
    dataType: 'json',
    data: {     
        pdf_id : '<?php echo $this->request->get['pdf_id']; ?>',
        page_number: counter,
    },
    beforeSend: function() {



    },
    complete: function() {},
    error: function() {
        alert('Build failed and timed out, please contact ####');
    },
    success: function(json) {
        $('.build-progress-green').css('width', build_progress_width);
        $('.pdf-build-progress').append('<div class="build-progress-green"></div>');


    }


});

if(counter == number_of_pages) { 
    clearInterval(i);
            $.ajax({
                    url: 'index.php?route=pdfengine/pdfengine/merge_pdfs',
                    type: 'post',
                    dataType: 'json',
                    data: {     
                        pdf_id : '<?php echo $this->request->get['pdf_id']; ?>',

                    },
                    beforeSend: function() {},
                    complete: function() {},
                    success: function(json) {

                        var link = document.getElementById('downloadlink');
                        // Set the link to the json response
                        link.href = json['download_link'];
                        $('#downloadlink').attr('download','reads-pdf-id-' + json['pdf_id'] + '.pdf');
                        // Unhide the link
                        link.style.display = 'block';
                        $('#downloadlink').trigger('click');
                        $('.waiting-gif').hide();



                    }
                });     
}
counter++;


}