WP_Cron script not updating all posts in loop

I’ve had an issue with this piece of script for a month now http://pastebin.com/f6LqgqQS. What the script is supposed to do is loop through all my posts, pull the like count from the facebook graph and update the post’s meta with it.

Now the issue I’ve had for so long is that not all of my posts were updated. It worked fine when I had 5 posts or so but once I reached 60+, the older posts were left unaffected. They did update after a while though, but it could take over 4 days even though wp_cron is set to hourly (and I know it needs visitors to run).

Read More

Now I always thought that graph api was the culprit and that facebook limited my number of requests, but after some testing with running the script on init instead, it seems that wp_cron was causing the problem. I even disabled wp_cron now and I’m running it from the Cpanel with cronjob and so far it has worked great.

But the problem right now is that I don’t really have a way to confirm that the script does indeed work correctly and if wp_cron was the culprit after all, as my searches turned up nothing. So I’m wondering if someone more experienced here could take a look at the script and figure out if indeed wp_cron was the problem after all.

Related posts

1 comment

  1. The wp-cron process runs as a web request. PHP processes run from the web typically have a limit to their run time, like 60 seconds or so. Processes run as a command line process (like from a real cron) have no such limit.

    So the problem here is that the script takes too long to run and gets killed. There is no fix for this, wp-cron is not intended to do long-running custom scripts.

    And if you keep hitting facebook’s Graph API 60 times an hour, they’ll eventually block you. Scale it back, you should only get this information once a day, at most.

    Better yet, write the script so that it only updates info for those posts when they are actually accessed in the last day or so, and just uses the cached info the rest of the time.

Comments are closed.