1

I am doing some sort of cronjob inside a WordPress plugin that does the following. When the cronjob needs to be fired, it sends a piece of jquery code with the user that happens to visit. As soon as the script is loaded on the frontend, it sends a post back to the server with the actions that need to be performed.

Now the function that fires on the server takes a long time to complete, that's why I do it this way. In PHP i have ignore_user_abort(true); so it does not matter if the user closes the browser. And I have some build in fallback if for some reason the actions are not fired with the particular user.

This works great all, only problem, for the particular user that visits, the browser keeps waiting for a reply. Is there a way I could break the jQuery post as soon as it is received by the server.

And if someone has another better solution for me to mimic a cronjob please let me know. I do not want to rely on functions as exec(), pcntl_form, wp-cron, as they are often disabled for users.

Saif Bechan
  • 16,551
  • 23
  • 83
  • 125

2 Answers2

1

I don't think the client actually waits for the request to complete (at least it shouldn't, if you have async on and everything...)

But anyway, if it's still a problem, you could send the request, then use xhr.abort(); or such after, say, a second or two.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • What do you mean with the client does not wait for the request to complete? It does wait, am i doing something wrong? What do you mean with: `if you have async on and everything`, how do I set that on. Further, I am going to test out xhr.abort, so far that sounds good. is there a way to tell if the server has recieved the post before calling the abort. – Saif Bechan Nov 11 '11 at 11:55
  • Ok I think I understand what you mean. Yeah async is set up ok, the browser visually does not wait for the response, but I can see it waiting in firebug. The connection stays alive. I just don't like that. – Saif Bechan Nov 11 '11 at 12:10
  • Well, the only way out of that, afaik, is `xhr.abort()` or cutting the connection on the server side. Depending on the server configuration, you may be able to spawn a background process/thread/whatever to do the actual work. I don't think the "spinning" on the client side actually matters though. – AKX Nov 11 '11 at 12:16
  • OK, but as I said, I do not want to rely on spawning a new process on the server side. If I would go that route the whole ajax function would not be necessary, I could just spwan the new process on the initial function call. On my server forking is disables, and I do not want to rely on any of those methods. – Saif Bechan Nov 11 '11 at 12:27
0

Perhaps you could use the Async plugin for jQuery: http://plugins.jquery.com/project/async

Pieter
  • 3,339
  • 5
  • 30
  • 63
  • No this is exactly what I do not want. This plugin does `long-lasting loops in an asynchronous way to avoid losing the browser responsiveness.` And I want to loose the responsiveness. I just want to send the post and don't wait for the response. – Saif Bechan Nov 11 '11 at 11:34
  • Ok, I didn't understand your question quite right then. In that case, you'll have to do it server-side such that the service spawns a new thread and returns immediately after that. See: http://stackoverflow.com/questions/5059334/async-fire-and-forget-operation-with-callback, http://stackoverflow.com/questions/6374860/fire-and-forget-with-asp-net-mvc – Pieter Nov 11 '11 at 11:38
  • LOL, now you don't understand the second part of the question. Read the last lines, I do not want to spawn new processes on the server side. They are sometimes disabled on some servers. Thanks anyway. – Saif Bechan Nov 11 '11 at 11:53