0

This isn't as simple of a question as you may think. Basically I have a C# program that is hitting the worpdress site and the wp_schedule doesn't seem to fire off. If I go to the site with my firefox browser then it seems to fire-off. Soooo what's the difference here? What exactly causes the cron to fire? Is it the HTTP GET? Or is there some script or function inside the page that is loaded by browsers?

4
  • How is your c# program "HITS" the WordPress site?
    – Bainternet
    May 10, 2011 at 23:25
  • using (WebClient client = new WebClient()) { byte[] ignoreData = client.DownloadData(pathUri); }
    – Thirlan
    May 10, 2011 at 23:33
  • hmm going to check how the C# program does it a bit more... maybe it isn't using a GET after all -_-
    – Thirlan
    May 10, 2011 at 23:34
  • It is performing a GET... maybe something in the headers is not right, which brings it back to the previous issue of, what magic thing needs to be done here? I'm going to try user-agent.
    – Thirlan
    May 11, 2011 at 0:21

3 Answers 3

1

The WP-Cron functions are not actually cron functions. Instead of a cron running and executing tasks precisely as scheduled, it waits until the frontend or admin is loaded, checks if any jobs are scheduled and then fires as needed. To execute the cron jobs, WP loads wp-cron.php, which is located in the root folder. My understanding is that when you visit the site and a cron is scheduled, a request is made to http://yourdomain.com/wp-cron.php, which initiates the scheduled cron job(s). I don't know exactly how this works (I've read about it before, but cannot find the resource again), but this request is made in such a way that not all servers can handle it, causing it not to work. I wonder if your problem is that this request isn't made with a GET request to your site.

As an alternative, I would recommend using a GET request to http://yourdomain.com/wp-cron.php. In fact, in the excellent Professional WordPress Plugin Development, they recommend the following:

A common method is using wget to load wp-cron.php on a schedule. If your server is Linux- based, cron will already exist and can be scheduled using the crontab command. If your server is Windows-based, you can install wget and create a scheduled task to execute it. The wget command would work like this: wget http://www.example.com/wp-cron.php When wget requests wp-cron.php WordPress looks for all scheduled cron jobs and executes as needed. Setting up true cron on your server will guarantee your cron jobs will run perfectly on schedule without missing a beat.

Along with this, you need to disable the default way of handling cron:

define(‘DISABLE_WP_CRON’, true);

Another discussion of this can be found here:

http://caramboo.com/2010/03/wordpress-remote-cron-scheduling/

4
  • This is for a plugin so disabling the WP_CRON is out of the question. Since it is a plugin I can't use the system cron either since I can't guarantee the OS that the user will use.
    – Thirlan
    May 11, 2011 at 15:45
  • I have thought about poking wp-cron.php, but I'm afraid that this might be too hacky and will break in a subsequent upgrade of WordPress. The suggestions are good however and I'll continue to explore my options. In the end I might have to pick the lesser of two evils.
    – Thirlan
    May 11, 2011 at 15:51
  • I do not have experience with this myself, but given that this is the suggestion of Justin Tadlock, Brad Williams, and Ozh Richard, I would think that it is a solid way of proceeding. These guys are rock stars in the WP plugin community.
    – tollmanz
    May 14, 2011 at 19:23
  • Okay this has been the most reliable solution.
    – Thirlan
    May 16, 2011 at 8:59
0

Are you following redirects?

In cron.php you can see that the spawn_cron function is doing

 wp_redirect( add_query_arg('doing_wp_cron', '', stripslashes($_SERVER['REQUEST_URI'])) );

I didn't work a lot with WP crons, but maybe this point you on the right path

1
  • 1
    I've been getting it to work now. I think part of the reason it was NOT firing off was because when using wp_schedule_single_event WordPress will stop any event with the same name from being run within 10 minutes of each other. The only way to override this was to pass a unique arg to the function. So the unique arg I passed was time(). This seems to have solved the problem, but even the 10 minute issue doesn't explain why it would work if I visited the page with a regular browser.
    – Thirlan
    May 11, 2011 at 15:50
0

If you want to remove the need for C# polling then this shameless plug (in :) might solve your problem:

Improved Cron

Paul

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.