WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] using ajax to avoid php and server timeouts (6 posts)

  1. nosilver4u
    Member
    Posted 1 year ago #

    The plugin in question is this: http://wordpress.org/extend/plugins/ewww-image-optimizer/

    Background:
    I have a bulk optimization routine that allows the user to run the optimization tools on their whole media library, or large chunks at a time. Many have had issues with it timing out, so I implemented a 'resume' function. And it works pretty well, but there are an increasing number of people who do not see any output because they have gzip enabled on their server. Thus the server can't flush any buffers, because it would break things, and the user is left sitting there wondering if anything is ever going to happen until it either finishes, or times out (I've run it for as long as 2 hours before, so this isn't a fun scenario).

    I've tried turning off gzip, but since my code gets executed after portions of the page have already been built, this doesn't work. If there is a way to turn off gzip before wordpress even starts building/loading the page, that would possibly make this a moot issue.

    That leaves me with using ajax, and now I'm stuck on a looping issue. When I run the bulk optimize in a normal loop (tried both a standard for loop, and jQuery.each), the javascript doesn't wait for the php response before it continues, so I end up launching 6 or more optimizations concurrently. This makes it pretty tough to keep tabs on what is going on, and since PNG optimization is already pretty CPU intensive, this makes it even worse.

    I read some stuff about using unshift to pop an element off the array, and then using the callback to iterate the loop after php finishes, but that was a terrible flop. On my dev box, I have around 2,400 images, and the javascript hit a recursion limit. I have users with 20,000-50,000 images, so that is obviously a real problem.

    So... am I missing something, is there a way to make this work? If the ajax doesn't work, is there a way to turn off gzip before wordpress starts loading the page? Or am I best served just going back to the way things were?

  2. catacaustic
    Member
    Posted 1 year ago #

    You'll need to look at the callback functionality of the jQuery calls. As an example, assuming that you're using .post()...

    var optimise_items = [1, 2, 3, 4, 5];
    var optimise_counter = 0;
    
    function optimize () {
        // get your data ready here
    
        jQuery.post (ajaxurl, data, function () {
            optimise_counter++;
    
            if (optimise_counter < optimise_items.length) {
                optimise ();
            }
        });
    }

    Of course, that doesn't take into account what you're actually doing, but it's an example of how it would work.

    That gives you a recursive function call that will kepe calling the optimise() function after each omtimisation has completed. This keeps things to one single call at a time, and will finish after all of the itmes have been completed.

  3. nosilver4u
    Member
    Posted 1 year ago #

    I've currently got this:

    jQuery(document).ready(function($) {
                    var response = null;
                    processImage(response);
                    return false;
            });
    function processImage (response) {
            $('#bulk-status').html(response);
            var attachment_id = ewww_vars.attachments.unshift();
            var loop_data = {
                    action: 'bulk_loop',
                    _wpnonce: ewww_vars._wpnonce,
                    attachment: attachment_id
            };
            $('#bulk-id').html(attachment_id);
            $.post(ajaxurl, loop_data, processImage(response) );
    }
    });

    I'm pretty new to javascript and ajax, does that accomplish roughly the same thing? The problem I'm running into with this is that it tells me something like 'too much resursion'.

  4. catacaustic
    Member
    Posted 1 year ago #

    You haven't got any way to break the loop. No matter how many times the loop goes through, it doesn't stop. You need to have some condition that will complete the loop otehrwise it just won't stop ever. In my example code, see how I'm testing how far thorugh the loop is with the if() statement?

  5. nosilver4u
    Member
    Posted 1 year ago #

    aha, I had missed a line in the example I was following that did exactly that. Thanks for your help, I was getting a bit despondent that I had run into an impossible situation. It is now working, and I can move on to converting the rest of the code over to the AJAX functions.
    Thanks again!

  6. catacaustic
    Member
    Posted 1 year ago #

    Good to hear. Infinate loops can be a hard thing ot diagnose sometimes. I hope the rest of it's easier for you.

Topic Closed

This topic has been closed to new replies.

About this Topic