I recently turned on the Search module and was having trouble getting cron to finish re-indexing content. First, I was getting an error about a function name being declared twice. I disabled the module containing the function and was then able to continue with the indexing. Then a few runs later, I start getting redirected to our separate (non-Drupal support site). I knew that one of our pages contained a drupal_goto() call to redirect users, so I commented that out and the Cron run was able to complete. My question is, is Cron supposed to work like that, running php code that's embedded in pages?

Comments

rgraves’s picture

I'm having the same issue since I upgraded to Drupal 6. I have a few old pages that were brought over for legacy reasons and they contain php code. When accessing the scripts through the site, they work fine. But when cron.php tries to run the php code, it's running into errors. For example, if I have a test to ensure a variable is set and it's not, cron.php fails and doesn't finish indexing my pages.

To make matters worse, the errors in Drupal's logs don't tell me anything about what page it failed on.

I'm not sure why cron.php is trying to run the php code rather than just indexing the content.

Anyone know how to solve this issue?

emackn’s picture

Also seeing this with a drupal 5 to 6 upgrade.

bcobin’s picture

I'm seeing the same problem with cron invoking page-embedded PHP code. The page in question has a drupal_goto() command that redirects the user to different pages depending on whether the user is logged in or not. This is with a new D6 site.

This has nothing to do with being on the page (in fact, it's a "jump" page that simply redirects); rather, the mere existence of the page causes the problem. On invoking cron (both logged in and anonymous), the site redirects to the "logged in" destination and the cron run never completes.

The code I'm using is as follows:

<?php
global $user;
if ($user->uid) {
  // Logged in user
drupal_goto('user/'.$GLOBALS['user']->uid.'/mobile');
}
else {
  // Not logged in
drupal_goto('user/register');
}
?>

Is there any way to:

• Prevent cron from invoking PHP-embedded code on pages (I would say this is unexpected behavior in any event), or,
• Redirect in a different manner?

Any help would be greatly appreciated - this is posing a major problem for me at the moment... thanks!

bcobin’s picture

After hunting through the forum, it seems that the search mechanism is the issue; as the site is indexed during cron runs, the drupal_goto() command in pages with PHP is actually invoked, aborting the cron run.

I was able to work around it with the following snippet in my jump page, thanks to the PHP mavens here - thanks for saving my bacon! If cron is running via cron.php, the drupal_goto() command on the offending pages will be ignored. (Note that this is only for cron.php; running cron from the Status page will still produce an error.)

I think there really should be some provision in search indexing to prohibit invoking the PHP on indexed pages during a cron run, but in the meantime, this seems to be working well. Thanks, y'all!

<?php
global $user;
$script_name= str_replace(base_path(), '', $_SERVER['SCRIPT_NAME']);
if ($script_name != 'cron.php' && $user->uid) {
drupal_goto('user/'.$GLOBALS['user']->uid.'/mobile');
}
else
$script_name= str_replace(base_path(), '', $_SERVER['SCRIPT_NAME']);
if ($script_name != 'cron.php') {
drupal_goto('user/register');
}
?>