View Full Version : vB3 cron system
filburt1
02-08-2004, 06:55 PM
What is the CLEAN (i.e., not exit!) way of ending a cron script?
For example, I have this function:
function autoimapdie($result)
{
global $logprefix;
if ($result === false)
{
$errors = imap_errors();
$warnings = imap_warnings();
$messages = array_merge($errors, $warnings);
foreach ($messages as $key => $value)
{
$messages[$key] = htmlspecialchars_uni($value);
}
if (!empty($messages))
{
$messages = htmlspecialchars("\"" . implode("\", \"", $messages) . "\"");
log_cron_action("$logprefix Failed due to imap errors or warnings: $messages",
$nextitem);
}
}
}
After the log_cron_action line, how can I return control back to the original cron control script?
MindTrix
02-08-2004, 07:07 PM
My host says return;
dunno if thats any help :p
filburt1
02-08-2004, 08:12 PM
My host says return;
dunno if thats any help :p
That will merely exit out of the function. There is code calling the function that will continue to get executed.
I don't want to edit any of vB's files, if possible, as this is for vBMS and I'm avoiding that for this release.
filburt1
02-08-2004, 08:29 PM
A thought occurs: is there an exit handler function that will make a graceful exit anyway when exit is used?
MindTrix
02-08-2004, 08:32 PM
How do you mean? (me lost) my host still swears u should use exit at the end of the cron line
filburt1
02-08-2004, 08:33 PM
How do you mean? (me lost) my host still swears u should use exit at the end of the cron line
Does your host know that I'm talking about vB3's cron system and not a standard Unix cron (i.e., crontab, etc.) setup?
MindTrix
02-08-2004, 08:36 PM
Doh! :( Sorry for wasting your time Filburt.
g-force2k2
02-08-2004, 11:23 PM
Filburt isn't control given automatically given back to the main cron script as it loops through each required file? Unless your function isn't itself going to be a cron script.
Cheers,
g-force2k2
filburt1
02-09-2004, 12:17 AM
Filburt isn't control given automatically given back to the main cron script as it loops through each required file? Unless your function isn't itself going to be a cron script.
Cheers,
g-force2k2
Well for example:
// is vBMS even enabled?
debugecho("Checking for vBMS enabled...");
if (!$vboptions['vbms_enabled'])
{
log_cron_action("$logprefix mail check ignored: vBMS is not enabled", $nextitem);
}
After that log entry, the script should hand control over to the next scheduled cron job (if any). As it is now (as I understand it, at least), it will just continue running, even though a known failure point was effectively ignored.
g-force2k2
02-09-2004, 02:16 AM
Could not not define a global variable that is ran through each loop and if an error is occured then instead of continuing the while loop it could instead break out, and if necessary first build a new cron log? Just an idea.
Cheers,
g-force2k2
filburt1
02-09-2004, 02:18 AM
The latest code block I posted is already at the global level. It would be easy for me to raise an error flag, but the problem is I would have to have a gargantuan tree of nested ifs (at least ten, more likely around 20) in order to gracefully have the code flow to the end of the file (the point at which the cron system resumes its work with the next job).
g-force2k2
02-09-2004, 02:28 AM
so then are you trying to break the control of the cron job to stop the following scheduled cron jobs, because I see that you are logging the error that has occurred, and if thats the case couldn't you simple add a continue ; to end that section of the while loop in the file, after it the included cron php files are within the actual loop, it would then resume the next cron job after all, because the continue only breaks that certain loop.
Cheers,
g-force2k2
filburt1
02-09-2004, 02:29 AM
To clear the ambiguity, I'm trying to make my custom cron job (vbms_checkmail.php) gracefully terminate and yield control to the next scheduled job (whatever.php), if there is any.
g-force2k2
02-09-2004, 02:31 AM
Alright, then couldn't continue ; possibly do the trick?
Cheers,
g-force2k2
filburt1
02-09-2004, 02:33 AM
That's actually a really good idea. The only problem would be that it could reference the wrong loop (for example, if I want to exit from within a loop).
Or wait, how would it work exactly? If continue is used in a function where there aren't even loops, would PHP just reversely traverse the code tree until it finds a loop, and then force the next iteration?
edit: http://www.php.net/continue for reference doesn't mention anything other than the freaky fact it supports an argument. I wish PHP was as clean as Java but as easy as it is now...
g-force2k2
02-09-2004, 02:45 AM
Hmm... very interesting, I did some tests with continue ; in both an included file and a function and both returned errors, but something else did work.
return ;
It returns to the main script I am assuming here's the three php files that I made and when I told it to 'continue' or in this case 'return' it skipped the rest of the script.
numbermain.php
<?php
error_reporting ( E_ALL & ~E_NOTICE ) ;
require_once ( "numberfunction.php" ) ;
while ( $x <= 35 ) :
include ( "numbercount.php" ) ;
endwhile ;
?>
numbercount.php
<?php
error_reporting ( E_ALL & ~E_NOTICE ) ;
global $x ;
$x++ ;
$dobreak = do_check ( $x ) ;
if ( $dobreak ) :
return ;
endif ;
echo $x . "<br />" ;
?>
numberfunction.php
<?php
error_reporting ( E_ALL & ~E_NOTICE ) ;
function do_check ( &$x )
{
$dobreak = 0 ;
if ( $x == 5 OR $x == 9 ) :
$dobreak = 1 ;
endif ;
return $dobreak ;
}
?>
And the result was:
1
2
3
4
6
7
8
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Skipped five and nine as I had asked the script. Maybe that will help some.
Cheers,
g-force2k2
filburt1
02-09-2004, 02:50 AM
Well remember that because PHP's file inclusion functions are essentially identical to the #include preprocessor directive in C--just dumps a file into another file--the code being run is this:
<?php
error_reporting ( E_ALL & ~E_NOTICE ) ;
require_once ( "numberfunction.php" ) ;
while ( $x <= 35 ) :
error_reporting ( E_ALL & ~E_NOTICE ) ;
function do_check ( &$x )
{
$dobreak = 0 ;
if ( $x == 5 OR $x == 9 ) :
$dobreak = 1 ;
endif ;
return $dobreak ;
}
endwhile ;
?>
You always need braces surrounding an include or require when using control structures or loops: http://www.php.net/include/ (example 11-6)
g-force2k2
02-09-2004, 02:55 AM
Well remember that because PHP's file inclusion functions are essentially identical to the #include preprocessor directive in C--just dumps a file into another file--the code being run is this:
Yeah I don't really know C ( or C++ ) at all, wanted to learn some, but you don't think that placing a return to return the the main cron control script, which would be exiting out of the inner cron script that is erroring woud do what you're looking to accomplish?
Cheers,
g-force2k2
filburt1
02-09-2004, 03:01 PM
Yeah I don't really know C ( or C++ ) at all, wanted to learn some, but you don't think that placing a return to return the the main cron control script, which would be exiting out of the inner cron script that is erroring woud do what you're looking to accomplish?
Cheers,
g-force2k2
Bizarrely, it looks like that might work. However it looks like any death code would have to be within vbms_checkmail.php and none of its helper functions.
Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function.
filburt1
02-18-2004, 07:10 PM
Interestingly, it looks like exit; will work. vB registers a shutdown function via register_shutdown_function() named exec_cron() that will, hopefully, cause the cron jobs to continue to run.
filburt1
02-18-2004, 07:18 PM
Well it just kills the script instead. I would very much appreciate a developer's input.
My exit function:
function gotonextcron($message)
{
global $nextitem, $stopwatch;
static $logprefix = "vBMS:";
log_cron_action("$logprefix $message (" . $stopwatch->elapsed() . " sec into mail check)", $nextitem);
exit; // should call a shutdown function rather than nuking itself
}
filburt1
02-22-2004, 01:51 PM
This is now effectively the last major outstanding issue in releasing an alpha for vBMS. I would very much appreciate some more help (and thanks to those who have helped so far).
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.