Marco van Herwaarden
02-06-2005, 03:46 PM
After looking into some error reports on my Thread of the Week hack, i found the reason of the errors in some strange (in my eyes) behaviour of job run using the Scheduled Task Manager.
Test script 'includes/cron/test.php':
<?php
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($DB_site))
{
exit;
}
function test1()
{
global $log, $count;
$log .= "Line $count. Level: Function<br />";
$count += 1;
}
// global $log, $count;
$log = "";
$count = 1;
$log = "Line $count. Level: Main<br />";
$count += 1;
test1();
$log .= "Line $count. Level: Main<br />";
$count += 1;
// Now let's write the log lines
log_cron_action($log, $nextitem);
?>
Put this script in a file called 'test.php' in your 'includes/cron' directory.
Now add a new Task with the Scheduled Task Manager, pointing to this file, set it to log the action.
If you now press "Run Now" and look at the lines written into the Scheduled Task Log. It will look like this:
134 test 18:34, 6th Feb 2005
Line 1. Level: Main
Line 2. Level: Function
Line 3. Level: Main
This is what i expected the script to do, all look ok. I set variable "$count" to the value "1" in the main line and add a line to "$log". Still in the main i add 1 to the counter and i call the function test1.
In function test1 i set the vars $log and $count to global, thus giving me access to the same vars as used in the mainline. I again add a line to the $log and increase $counter. Now return to mainline.
Back in the main line $counter still contain the value as set in the function, and $log now contain 2 lines. I add 1 more line, write $log to the Scheduled Task log and finish.
This is exactly what happens when i run it with the "Run Now" button.
Now i repeat the test, but this time i let it run automatic, by setting a time for the Task to run. I wait till it has run, and go look in the Scheduled Task Log.
I expect exactly the same output, since it is the same script.
But, output is as follows:
138 test 18:44, 6th Feb 2005
Line 1. Level: Main
Line 2. Level: Main
No output from lines added in the function. The global in the function didn't give me access to the vars used in the mainline.
What am i missing here?
PS Yes when i also in the main line declare these 2 vars as global, or if i would pass them as parameters to the function, i could make this work. But that is not the point. I just want to understand what is going on.
Test script 'includes/cron/test.php':
<?php
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($DB_site))
{
exit;
}
function test1()
{
global $log, $count;
$log .= "Line $count. Level: Function<br />";
$count += 1;
}
// global $log, $count;
$log = "";
$count = 1;
$log = "Line $count. Level: Main<br />";
$count += 1;
test1();
$log .= "Line $count. Level: Main<br />";
$count += 1;
// Now let's write the log lines
log_cron_action($log, $nextitem);
?>
Put this script in a file called 'test.php' in your 'includes/cron' directory.
Now add a new Task with the Scheduled Task Manager, pointing to this file, set it to log the action.
If you now press "Run Now" and look at the lines written into the Scheduled Task Log. It will look like this:
134 test 18:34, 6th Feb 2005
Line 1. Level: Main
Line 2. Level: Function
Line 3. Level: Main
This is what i expected the script to do, all look ok. I set variable "$count" to the value "1" in the main line and add a line to "$log". Still in the main i add 1 to the counter and i call the function test1.
In function test1 i set the vars $log and $count to global, thus giving me access to the same vars as used in the mainline. I again add a line to the $log and increase $counter. Now return to mainline.
Back in the main line $counter still contain the value as set in the function, and $log now contain 2 lines. I add 1 more line, write $log to the Scheduled Task log and finish.
This is exactly what happens when i run it with the "Run Now" button.
Now i repeat the test, but this time i let it run automatic, by setting a time for the Task to run. I wait till it has run, and go look in the Scheduled Task Log.
I expect exactly the same output, since it is the same script.
But, output is as follows:
138 test 18:44, 6th Feb 2005
Line 1. Level: Main
Line 2. Level: Main
No output from lines added in the function. The global in the function didn't give me access to the vars used in the mainline.
What am i missing here?
PS Yes when i also in the main line declare these 2 vars as global, or if i would pass them as parameters to the function, i could make this work. But that is not the point. I just want to understand what is going on.