humanmade / Cavalcade

A better wp-cron. Horizontally scalable, works perfectly with multisite.
https://engineering.hmn.md/projects/cavalcade/
Other
515 stars 46 forks source link

`DOING_CRON` isn't defined #28

Closed dd32 closed 7 years ago

dd32 commented 7 years ago

I just noticed that DOING_CRON isn't defined when running jobs, this could cause unexpected behaviour for some jobs.

While running through WP-CLI, it won't be possible to define it before bootstrap occurs (AFAIK), but defining it late might be better than not at all.

rmccue commented 7 years ago

We should do this as soon as possible, but I think that's likely to be when the command's callback fires. We can't really hook in any earlier.

dd32 commented 7 years ago

Perhaps an option would be to enhance WP-CLI to accept certain constants on the command line? ie. wp --debug=true --cron=on

rmccue commented 7 years ago

wp-cli itself only sets the constant late, so I'm fine with also setting it late.

joehoyle commented 7 years ago

FWIW this just bit me too!

onnimonni commented 7 years ago

I think the most earliest it can be currently set in WP-CLI bootstrap is with the --require flag: https://github.com/wp-cli/wp-cli/blob/104ccf37da491465b8d42bc73695865440ebd333/php/bootstrap.php#L26

In case you'll want to use that the cavalcade runner should have a file which can be required with wp-cli:

$ echo "<?php define( 'DOING_CRON', true );" > /tmp/define-cron.php
$ wp cavalcade run <id> --require /tmp/define-cron.php

Another hacky option is to use auto_prepend_file in the cavalcade runner:

$ echo "<?php define( 'DOING_CRON', true );" > /tmp/define-cron.php
$ php -d auto_prepend_file="/tmp/define-cron.php" /usr/local/bin/wp cavalcade run <id>

This is kinda hacky and it shouldn't make a big difference if it's defined just before running the cron job.