29if (!defined(
'NOTOKENRENEWAL')) {
30 define(
'NOTOKENRENEWAL',
'1');
32if (!defined(
'NOREQUIREMENU')) {
33 define(
'NOREQUIREMENU',
'1');
35if (!defined(
'NOREQUIREHTML')) {
36 define(
'NOREQUIREHTML',
'1');
38if (!defined(
'NOREQUIREAJAX')) {
39 define(
'NOREQUIREAJAX',
'1');
41if (!defined(
'NOLOGIN')) {
42 define(
'NOLOGIN',
'1');
44if (!defined(
'NOIPCHECK')) {
45 define(
'NOIPCHECK',
'1');
49if (!defined(
'USESUFFIXINLOG')) {
50 define(
'USESUFFIXINLOG',
'_cron');
56$entity = (!empty($_GET[
'entity']) ? (int) $_GET[
'entity'] : (!empty($_POST[
'entity']) ? (int) $_POST[
'entity'] : 1));
57if (is_numeric($entity)) {
58 define(
"DOLENTITY", $entity);
62if (php_sapi_name() ==
"cli") {
63 echo
"Error: This page can't be used as a CLI script. For the CLI version of script, launch cron_run_job.php available into scripts/cron/ directory.\n";
69require
'../../main.inc.php';
81$langs->loadLangs(array(
"admin",
"cron",
"dict"));
98$key =
GETPOST(
'securitykey',
'alpha');
100 echo
'Securitykey is required. Check setup of cron jobs module.';
104 echo
'Securitykey is wrong.';
108$userlogin =
GETPOST(
'userlogin',
'alpha');
109if (empty($userlogin)) {
110 echo
'Userlogin is required.';
113require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
114$user =
new User($db);
115$result = $user->fetch(0, $userlogin);
117 echo
"User Error:".$user->error;
118 dol_syslog(
"cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
121 if (empty($user->id)) {
122 echo
" User login:".$userlogin.
" do not exists";
123 dol_syslog(
" User login:".$userlogin.
" do not exists", LOG_ERR);
137 if (!is_numeric($id)) {
138 echo
"Error: Bad value for parameter job id";
139 dol_syslog(
"cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
142 $filter[
't.rowid'] =
$id;
146$sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob set processing = 0";
147$sql .=
" WHERE processing = 1";
148$sql .=
" AND datelastrun <= '".$db->idate(
dol_now() -
getDolGlobalInt(
'CRON_MAX_DELAY_FOR_JOBS', 24) * 3600,
'gmt').
"'";
149$sql .=
" AND datelastresult IS NULL";
153if (function_exists(
'posix_kill') && function_exists(
'posix_get_last_error')) {
154 $sql =
"SELECT rowid, pid";
155 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob";
156 $sql .=
" WHERE processing = 1";
157 $sql .=
" AND datelastresult IS NULL";
158 $sql .=
" AND pid IS NOT NULL";
159 $resql =
$db->query($sql);
161 while ($obj =
$db->fetch_object($resql)) {
162 $pid = (int) $obj->pid;
167 $isalive = @posix_kill($pid, 0);
169 $errno = posix_get_last_error();
172 $msg =
'Cron job unlocked: stale PID '.$pid;
174 $sqlu =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob";
175 $sqlu .=
" SET processing = 0, pid = NULL, datelastresult = '".$db->idate($nowcleanup).
"', lastresult = '-1', lastoutput = '".
$db->escape($msg).
"'";
176 $sqlu .=
" WHERE rowid = ".((int) $obj->rowid).
" AND processing = 1 AND pid = ".((int) $pid).
" AND datelastresult IS NULL";
179 dol_syslog(
"cron_run_jobs_by_url.php unlocked stuck job id=".$obj->rowid.
" (stale pid ".$pid.
")", LOG_WARNING);
180 echo
"Unlocked stuck job id=".$obj->rowid.
" (stale pid ".$pid.
")\n";
188$result =
$object->fetchAll(
'ASC,ASC,ASC',
't.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
190 echo
"Error: ".$object->error;
198$nbofjobs = count(
$object->lines);
199$nbofjobslaunchedok = 0;
200$nbofjobslaunchedko = 0;
206 foreach (
$object->lines as $line) {
207 '@phan-var-force Cronjob $line';
208 dol_syslog(
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label, LOG_DEBUG);
209 echo
"cron_run_jobs.php cronjobid: ".$line->id.
" priority=".$line->priority.
" entity=".$line->entity.
" label=".$line->label;
212 if ($line->entity !=
$conf->entity) {
213 dol_syslog(
"cron_run_jobs.php we work on another entity so we reload user and conf", LOG_DEBUG);
214 echo
" -> we change entity so we reload user and conf";
216 $conf->entity = (empty($line->entity) ? 1 : $line->entity);
217 $conf->setValues($db);
220 if (
$conf->entity != $user->entity && $user->entity != 0) {
221 $result = $user->fetch(0, $userlogin,
'', 0,
$conf->entity);
223 echo
"\nUser Error: ".$user->error.
"\n";
224 dol_syslog(
"cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
228 echo
"\nUser login: ".$userlogin.
" does not exists for entity ".
$conf->entity.
"\n";
229 dol_syslog(
"User login:".$userlogin.
" does not exists", LOG_ERR);
242 $datenextrunok = (empty($line->datenextrun) || (int) $line->datenextrun < $now);
243 $datestartok = (empty($line->datestart) || $line->datestart <= $now);
244 $dateendok = (empty($line->dateend) || $line->dateend >= $now);
245 if ($datenextrunok && $datestartok && $dateendok) {
251 $result = $cronjob->fetch($line->id);
253 echo
"Error cronjobid: ".$line->id.
" cronjob->fetch: ".$cronjob->error.
"\n";
254 echo
"Failed to fetch job ".$line->id.
"\n";
255 dol_syslog(
"cron_run_jobs.php::fetch Error".$cronjob->error, LOG_ERR);
259 $result = $cronjob->run_jobs($userlogin);
261 echo
"Error cronjobid: ".$line->id.
" cronjob->run_job: ".$cronjob->error.
"\n";
262 echo
"At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
263 echo
"You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
264 dol_syslog(
"cron_run_jobs.php::run_jobs Error".$cronjob->error, LOG_ERR);
265 $nbofjobslaunchedko++;
266 $resultstring =
'KO';
268 $nbofjobslaunchedok++;
269 $resultstring =
'OK';
272 echo
"Result of run_jobs = ".$resultstring.
" result = ".$result;
275 $result = $cronjob->reprogram_jobs($userlogin, $now);
277 echo
" - Error cronjobid: ".$line->id.
" cronjob->reprogram_job: ".$cronjob->error.
"\n";
278 echo
"Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
279 dol_syslog(
"cron_run_jobs.php::reprogram_jobs Error".$cronjob->error, LOG_ERR);
283 echo
" - Job re-scheduled\n";
285 echo
" - not qualified (datenextrunok=".($datenextrunok ?: 0).
", datestartok=".($datestartok ?: 0).
", dateendok=".($dateendok ?: 0).
")\n";
293 echo
"Result: ".($nbofjobs).
" jobs - ".($nbofjobslaunchedok + $nbofjobslaunchedko).
" launched = ".$nbofjobslaunchedok.
" OK + ".$nbofjobslaunchedko.
" KO";
295 echo
"Result: No active jobs found.";
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
verifCond($strToEvaluate, $onlysimplestring='1')
Verify if condition in string is ok or not.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.