5

I have a small http server which generates some images on-the-fly, and the generation process may take some time. After generation, the image is cached indefinetly.

Currently, if a user requests an image which is not cached, the server returns a 202 Accepted with a Refresh header. If the image is cached, a 301 Permanently Moved is sent and the user redirected to a unique url (different images may share the same unique url).

The whole system breaks if the image is referenced in an <img> tag (on Firefox, at least). Can this be solved without Javascript? If not, how would the script look like?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
moatPylon
  • 2,103
  • 1
  • 15
  • 22
  • Can't you just block the request while you're processing, and answer directly with the actual image when you're ready? How long does your image generation take? – lanzz May 29 '12 at 10:40
  • That depends. Some images may take up to a couple minutes to generate, while others take only few seconds. Blocking is not an option. – moatPylon May 29 '12 at 10:41
  • Have you tried with http 200 OK and the Refresh header. (It makes more sense a 202, but just in case ....) – richardtz May 29 '12 at 11:05
  • Just tried it. Doesn't work :( – moatPylon May 29 '12 at 11:24
  • Why exactly blocking is not an option? Can you at least block for 10 seconds before actually releasing refresh response to decrease amount of repeated requests to server? – Oleg V. Volkov May 29 '12 at 14:29
  • Yes, but not all requests can be blocked. For those images which take minutes to be generated, blocking is not an option. – moatPylon May 29 '12 at 15:24

1 Answers1

2

Im not sure if you can do it without Javascript but you could probably do this with ajax? I mean, point at the server and then just check to see if it is there... then if it is display it, else try again 30 seconds later, it could be something like:

function getImage(img) {
 $.ajax({
            cache: true,
            url: <<ADDRESS>>,
            data: "",
            timeout: 60,
            error: function (jqXHR, error, errorThrown) {
                setTimeout(function() {
                    getImage(img);
                }, 30000);
            },
            success: function (data) {
                //set the image
            }
        });
}

Okay your then hoping that the image will come down at somepoint.

The only other option would be to generate the image before it is requested? For example if it is just creating a thumbnail for a photo gallery, why wait until it is requested to generate it? Just generate it as soon as you have it?

Hope that helps / makes sense.

Chris
  • 304
  • 3
  • 15
  • Great! If `error`/`sucess` is replaced by `statusCode {202: ..., 301: ...}`, then it should work with the current system. – moatPylon May 30 '12 at 08:53
  • Hopefully, the error or jqXHR object should give you the actual error code (use console.log in Google Chrome, it helps a lot to see what an object has in it etc.) – Chris May 30 '12 at 09:18
  • +1 For a viable solution but blocking the request really would be a better option - even if blocking required a 5 minute wait, it would still return faster than using this code (since it would return as soon as generation was completed, not the poll after that up to 30s later). It would only block the image in question, not the page. – Basic Sep 06 '12 at 14:07