216 global $conf, $langs;
222 if (isset($this->label)) {
223 $this->label = trim($this->label);
225 if (isset($this->jobtype)) {
226 $this->jobtype = trim($this->jobtype);
228 if (isset($this->command)) {
229 $this->command = trim($this->command);
231 if (isset($this->classesname)) {
232 $this->classesname = trim($this->classesname);
234 if (isset($this->objectname)) {
235 $this->objectname = trim($this->objectname);
237 if (isset($this->methodename)) {
238 $this->methodename = trim($this->methodename);
240 if (isset($this->params)) {
241 $this->params = trim($this->params);
243 if (isset($this->md5params)) {
244 $this->md5params = trim($this->md5params);
246 if (isset($this->module_name)) {
247 $this->module_name = trim($this->module_name);
249 if (isset($this->lastoutput)) {
250 $this->lastoutput = trim($this->lastoutput);
252 if (isset($this->lastresult)) {
253 $this->lastresult = trim($this->lastresult);
255 if (isset($this->unitfrequency)) {
256 $this->unitfrequency = trim($this->unitfrequency);
258 if (isset($this->frequency)) {
259 $this->frequency = (int) $this->frequency;
261 if (isset($this->
status)) {
264 if (isset($this->note_private)) {
265 $this->note_private = trim($this->note_private);
267 if (isset($this->nbrun)) {
268 $this->nbrun = (int) $this->nbrun;
270 if (isset($this->maxrun)) {
271 $this->maxrun = (int) $this->maxrun;
273 if (isset($this->libname)) {
274 $this->libname = trim($this->libname);
276 if (isset($this->test)) {
277 $this->test = trim($this->test);
283 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
286 if (empty($this->label)) {
287 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
290 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
291 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
294 if (empty($this->unitfrequency)) {
295 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
298 if (($this->jobtype ==
'command') && (empty($this->command))) {
299 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
302 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
303 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
306 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
307 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
310 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
311 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
314 if (($this->jobtype ==
'function') && (empty($this->libname))) {
315 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
320 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
326 $sql .=
"classesname,";
327 $sql .=
"objectname,";
328 $sql .=
"methodename,";
330 $sql .=
"md5params,";
331 $sql .=
"module_name,";
333 $sql .=
"datelastrun,";
334 $sql .=
"datenextrun,";
336 $sql .=
"datestart,";
337 $sql .=
"lastresult,";
338 $sql .=
"datelastresult,";
339 $sql .=
"lastoutput,";
340 $sql .=
"unitfrequency,";
341 $sql .=
"frequency,";
343 $sql .=
"fk_user_author,";
344 $sql .=
"fk_user_mod,";
350 $sql .=
") VALUES (";
351 $sql .=
" ".(!isset($this->entity) ? (int) $conf->entity : (int) $this->entity).
",";
352 $sql .=
" '".$this->db->idate($now).
"',";
353 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
354 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
355 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
356 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
357 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
358 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
359 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
360 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
361 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
362 $sql .=
" ".(is_numeric($this->priority) ? (int) $this->priority : 50).
",";
363 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
364 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
365 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
366 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
367 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
368 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
369 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
370 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
371 $sql .=
" ".(!isset($this->frequency) ?
'0' : ((int) $this->frequency)).
",";
372 $sql .=
" ".(!isset($this->
status) ?
'0' : ((int) $this->
status)).
",";
373 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
374 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
375 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
376 $sql .=
" ".(!isset($this->nbrun) ?
'0' : ((int) $this->nbrun)).
",";
377 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $this->maxrun)).
",";
378 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
379 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'");
384 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
385 $resql = $this->db->query($sql);
388 $this->errors[] =
"Error ".$this->db->lasterror();
392 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
397 $this->db->rollback();
414 public function fetch(
int $id,
string $objectname =
'',
string $methodname =
'')
418 $sql .=
" t.entity,";
421 $sql .=
" t.jobtype,";
423 $sql .=
" t.command,";
424 $sql .=
" t.classesname,";
425 $sql .=
" t.objectname,";
426 $sql .=
" t.methodename,";
427 $sql .=
" t.params,";
428 $sql .=
" t.md5params,";
429 $sql .=
" t.module_name,";
430 $sql .=
" t.priority,";
431 $sql .=
" t.datelastrun,";
432 $sql .=
" t.datenextrun,";
433 $sql .=
" t.dateend,";
434 $sql .=
" t.datestart,";
435 $sql .=
" t.lastresult,";
436 $sql .=
" t.datelastresult,";
437 $sql .=
" t.lastoutput,";
438 $sql .=
" t.unitfrequency,";
439 $sql .=
" t.frequency,";
440 $sql .=
" t.status,";
441 $sql .=
" t.processing,";
443 $sql .=
" t.email_alert,";
444 $sql .=
" t.fk_user_author,";
445 $sql .=
" t.fk_user_mod,";
446 $sql .=
" t.note as note_private,";
448 $sql .=
" t.maxrun,";
449 $sql .=
" t.libname,";
451 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
453 $sql .=
" WHERE t.rowid = ".((int)
$id);
455 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
456 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
457 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
460 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
461 $resql = $this->db->query($sql);
463 if ($this->db->num_rows($resql)) {
464 $obj = $this->db->fetch_object($resql);
466 $this->
id = $obj->rowid;
467 $this->
ref = $obj->rowid;
468 $this->entity = $obj->entity;
469 $this->tms = $this->db->jdate($obj->tms);
470 $this->datec = $this->db->jdate($obj->datec);
471 $this->label = $obj->label;
472 $this->jobtype = $obj->jobtype;
473 $this->command = $obj->command;
474 $this->classesname = $obj->classesname;
475 $this->objectname = $obj->objectname;
476 $this->methodename = $obj->methodename;
477 $this->params = $obj->params;
478 $this->md5params = $obj->md5params;
479 $this->module_name = $obj->module_name;
480 $this->priority = $obj->priority;
481 $this->datelastrun = $this->db->jdate($obj->datelastrun);
482 $this->datenextrun = $this->db->jdate($obj->datenextrun);
483 $this->dateend = $this->db->jdate($obj->dateend);
484 $this->datestart = $this->db->jdate($obj->datestart);
485 $this->lastresult = (string) $obj->lastresult;
486 $this->lastoutput = $obj->lastoutput;
487 $this->datelastresult = $this->db->jdate($obj->datelastresult);
488 $this->unitfrequency = $obj->unitfrequency;
489 $this->frequency = $obj->frequency;
490 $this->
status = $obj->status;
491 $this->processing = $obj->processing;
492 $this->pid = $obj->pid;
493 $this->email_alert = $obj->email_alert;
494 $this->fk_user_author = $obj->fk_user_author;
495 $this->fk_user_mod = $obj->fk_user_mod;
496 $this->note_private = $obj->note_private;
497 $this->nbrun = $obj->nbrun;
498 $this->maxrun = $obj->maxrun;
499 $this->libname = $obj->libname;
500 $this->test = $obj->test;
502 $this->db->free($resql);
506 $this->error =
"Error ".$this->db->lasterror();
523 public function fetchAll(
string $sortorder =
'DESC',
string $sortfield =
't.rowid',
int $limit = 0,
int $offset = 0,
int $status = 1, $filter =
'',
int $processing = -1)
525 $this->lines = array();
529 $sql .=
" t.entity,";
532 $sql .=
" t.jobtype,";
534 $sql .=
" t.command,";
535 $sql .=
" t.classesname,";
536 $sql .=
" t.objectname,";
537 $sql .=
" t.methodename,";
538 $sql .=
" t.params,";
539 $sql .=
" t.md5params,";
540 $sql .=
" t.module_name,";
541 $sql .=
" t.priority,";
542 $sql .=
" t.datelastrun,";
543 $sql .=
" t.datenextrun,";
544 $sql .=
" t.dateend,";
545 $sql .=
" t.datestart,";
546 $sql .=
" t.lastresult,";
547 $sql .=
" t.datelastresult,";
548 $sql .=
" t.lastoutput,";
549 $sql .=
" t.unitfrequency,";
550 $sql .=
" t.frequency,";
551 $sql .=
" t.status,";
552 $sql .=
" t.processing,";
554 $sql .=
" t.email_alert,";
555 $sql .=
" t.fk_user_author,";
556 $sql .=
" t.fk_user_mod,";
557 $sql .=
" t.note as note_private,";
559 $sql .=
" t.maxrun,";
560 $sql .=
" t.libname,";
562 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
563 $sql .=
" WHERE 1 = 1";
564 if ($processing >= 0) {
565 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
567 if ($status >= 0 && $status < 2) {
568 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
569 } elseif ($status == 2) {
570 $sql .=
" AND t.status = 2";
574 if (is_array($filter)) {
575 if (count($filter) > 0) {
576 foreach ($filter as $key => $value) {
577 if ($key ==
't.rowid') {
578 $sql .=
" AND ".$this->db->sanitize($key).
" = ".((int) $value);
580 $sql .=
" AND ".$this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
592 $this->errors[] = $errormessage;
593 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
597 $sql .= $this->db->order($sortfield, $sortorder);
598 if (!empty($limit) && !empty($offset)) {
599 $sql .= $this->db->plimit($limit + 1, $offset);
602 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
603 $resql = $this->db->query($sql);
605 $num = $this->db->num_rows($resql);
610 $obj = $this->db->fetch_object($resql);
611 $cronjob_obj =
new Cronjob($this->db);
613 $cronjob_obj->id = $obj->rowid;
614 $cronjob_obj->ref = $obj->rowid;
615 $cronjob_obj->entity = $obj->entity;
616 $cronjob_obj->tms = $this->db->jdate($obj->tms);
617 $cronjob_obj->datec = $this->db->jdate($obj->datec);
618 $cronjob_obj->label = $obj->label;
619 $cronjob_obj->jobtype = $obj->jobtype;
620 $cronjob_obj->command = $obj->command;
621 $cronjob_obj->classesname = $obj->classesname;
622 $cronjob_obj->objectname = $obj->objectname;
623 $cronjob_obj->methodename = $obj->methodename;
624 $cronjob_obj->params = $obj->params;
625 $cronjob_obj->md5params = $obj->md5params;
626 $cronjob_obj->module_name = $obj->module_name;
627 $cronjob_obj->priority = $obj->priority;
628 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
629 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
630 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
631 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
632 $cronjob_obj->lastresult = $obj->lastresult;
633 $cronjob_obj->lastoutput = $obj->lastoutput;
634 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
635 $cronjob_obj->unitfrequency = $obj->unitfrequency;
636 $cronjob_obj->frequency = $obj->frequency;
637 $cronjob_obj->status = $obj->status;
638 $cronjob_obj->processing = $obj->processing;
639 $cronjob_obj->pid = $obj->pid;
640 $cronjob_obj->email_alert = $obj->email_alert;
641 $cronjob_obj->fk_user_author = $obj->fk_user_author;
642 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
643 $cronjob_obj->note_private = $obj->note_private;
644 $cronjob_obj->nbrun = $obj->nbrun;
645 $cronjob_obj->maxrun = $obj->maxrun;
646 $cronjob_obj->libname = $obj->libname;
647 $cronjob_obj->test = $obj->test;
649 $this->lines[] = $cronjob_obj;
654 $this->db->free($resql);
658 $this->error =
"Error ".$this->db->lasterror();
671 public function update($user =
null,
int $notrigger = 0)
673 global $conf, $langs;
675 $langs->load(
'cron');
680 if (isset($this->label)) {
681 $this->label = trim($this->label);
683 if (isset($this->jobtype)) {
684 $this->jobtype = trim($this->jobtype);
686 if (isset($this->command)) {
687 $this->command = trim($this->command);
689 if (isset($this->classesname)) {
690 $this->classesname = trim($this->classesname);
692 if (isset($this->objectname)) {
693 $this->objectname = trim($this->objectname);
695 if (isset($this->methodename)) {
696 $this->methodename = trim($this->methodename);
698 if (isset($this->params)) {
699 $this->params = trim($this->params);
701 if (isset($this->md5params)) {
702 $this->md5params = trim($this->md5params);
704 if (isset($this->module_name)) {
705 $this->module_name = trim($this->module_name);
707 if (isset($this->priority)) {
708 $this->priority = trim($this->priority);
710 if (isset($this->lastoutput)) {
711 $this->lastoutput = trim($this->lastoutput);
713 if (isset($this->lastresult)) {
714 $this->lastresult = trim($this->lastresult);
716 if (isset($this->unitfrequency)) {
717 $this->unitfrequency = trim($this->unitfrequency);
719 if (isset($this->frequency)) {
720 $this->frequency = (int) $this->frequency;
722 if (isset($this->
status)) {
725 if (isset($this->note_private)) {
726 $this->note_private = trim($this->note_private);
728 if (isset($this->nbrun)) {
729 $this->nbrun = (is_numeric($this->nbrun)) ? (
int) trim((
string) $this->nbrun) : 0;
731 if (isset($this->libname)) {
732 $this->libname = trim($this->libname);
734 if (isset($this->test)) {
735 $this->test = trim($this->test);
738 if (empty($this->maxrun)) {
741 if (empty($this->processing)) {
742 $this->processing = 0;
744 if (empty($this->pid)) {
747 if (empty($this->email_alert)) {
748 $this->email_alert =
'';
750 if (empty($this->datenextrun)) {
751 $this->datenextrun =
dol_now();
756 if (
dol_strlen($this->datenextrun) == 0 && $this->
status == self::STATUS_ENABLED) {
757 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
760 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
761 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
764 if (empty($this->label)) {
765 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
768 if (empty($this->unitfrequency)) {
769 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
772 if (($this->jobtype ==
'command') && (empty($this->command))) {
773 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
776 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
777 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
780 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
781 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
784 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
785 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
789 if (($this->jobtype ==
'function') && (empty($this->libname))) {
790 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
796 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
797 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).
",";
798 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
799 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
800 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
801 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
802 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
803 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
804 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
805 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
806 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
807 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
808 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
809 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
810 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
811 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
812 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
813 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
814 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
815 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ?
"'".$this->db->escape($this->unitfrequency).
"'" :
"null").
",";
816 $sql .=
" frequency=".(isset($this->frequency) ? ((int) $this->frequency) :
"null").
",";
817 $sql .=
" status=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
818 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
819 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
820 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
821 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
822 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
823 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
824 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
825 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
826 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
827 $sql .=
" WHERE rowid=".((int) $this->
id);
831 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
832 $resql = $this->db->query($sql);
835 $this->errors[] =
"Error ".$this->db->lasterror();
840 foreach ($this->errors as $errmsg) {
841 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
842 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
844 $this->db->rollback();
1161 global $langs, $conf, $hookmanager;
1163 $hookmanager->initHooks(array(
'cron'));
1168 $langs->load(
'cron');
1170 if (empty($userlogin)) {
1171 $this->error =
"User login is mandatory";
1172 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1178 if ($conf->entity != $this->entity && $this->entity > 0) {
1179 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1181 $savcurrententity = $conf->entity;
1182 $conf->setEntityValues($this->db, $this->entity);
1183 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1185 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1186 $user =
new User($this->db);
1187 $result = $user->fetch(0, $userlogin);
1189 $this->error =
"User Error:".$user->error;
1190 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1191 $conf->setEntityValues($this->db, $savcurrententity);
1194 if (empty($user->id)) {
1195 $this->error =
"User login: ".$userlogin.
" does not exist";
1196 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1197 $conf->setEntityValues($this->db, $savcurrententity);
1202 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1205 $ExecTimeLimit = 600;
1206 if (!empty($ExecTimeLimit)) {
1207 $err = error_reporting();
1210 @set_time_limit($ExecTimeLimit);
1211 error_reporting($err);
1214 if (!empty($MemoryLimit)) {
1215 @ini_set(
'memory_limit', $MemoryLimit);
1219 $this->datelastrun = $now;
1220 $this->datelastresult =
null;
1221 $this->lastoutput =
'';
1222 $this->lastresult =
'';
1223 $this->processing = 1;
1224 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1226 $result = $this->
update($user);
1228 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1229 $conf->setEntityValues($this->db, $savcurrententity);
1234 if ($this->jobtype ==
'method') {
1236 if (!empty($this->entity) && !empty($this->module_name) && !isModEnabled(strtolower($this->module_name))) {
1237 $this->error = $langs->transnoentitiesnoconv(
'CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1238 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1239 $this->lastoutput = $this->error;
1240 $this->lastresult =
'-1';
1247 if ($ret ===
false || (!class_exists($this->objectname))) {
1248 if ($ret ===
false) {
1249 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1251 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1253 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1254 $this->lastoutput = $this->error;
1255 $this->lastresult =
'-1';
1262 if (!method_exists($this->objectname, $this->methodename)) {
1263 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1264 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1265 $this->lastoutput = $this->error;
1266 $this->lastresult =
'-1';
1269 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1270 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1271 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1272 $this->lastoutput = $this->error;
1273 $this->lastresult =
'-1';
1280 $result = $langs->load($this->module_name);
1281 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1284 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1285 $this->error = $langs->error;
1286 $this->lastoutput = $this->error;
1287 $this->lastresult =
'-1';
1293 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
"); (Note: Log for cron jobs may be into a different log file)", LOG_DEBUG);
1296 $nameofclass = (string) $this->objectname;
1297 $object =
new $nameofclass($this->db);
1298 if ($this->entity > 0) {
1299 $object->entity = $this->entity;
1302 $params_arr = array();
1303 if (!empty($this->params) || $this->params ===
'0') {
1304 $params_arr = array_map(
'trim', explode(
",", $this->params));
1307 if (!is_array($params_arr)) {
1308 $result = call_user_func(array(
$object, $this->methodename), $this->params);
1310 $result = call_user_func_array(array(
$object, $this->methodename), $params_arr);
1313 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1314 $langs->load(
"errors");
1320 $errmsg .= (($errmsg ?
', ' :
'').implode(
', ',
$object->errors));
1322 if (empty($errmsg)) {
1323 $errmsg = $langs->trans(
'ErrorUnknown');
1326 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1328 $this->error = $errmsg;
1329 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1330 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1333 dol_syslog(get_class($this).
"::run_jobs END");
1334 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1335 $this->lastresult = var_export($result,
true);
1340 if ($this->jobtype ==
'function') {
1342 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1344 if ($ret ===
false) {
1345 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1346 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1347 $conf->setEntityValues($this->db, $savcurrententity);
1352 $result = $langs->load($this->module_name);
1353 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1355 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1356 $conf->setEntityValues($this->db, $savcurrententity);
1360 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1361 $params_arr = explode(
", ", $this->params);
1362 if (!is_array($params_arr)) {
1363 $result = call_user_func($this->methodename, $this->params);
1365 $result = call_user_func_array($this->methodename, $params_arr);
1368 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1369 $langs->load(
"errors");
1370 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1371 $this->error = $langs->trans(
'ErrorUnknown');
1372 $this->lastoutput = $this->error;
1373 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1376 $this->lastoutput = var_export($result,
true);
1377 $this->lastresult = var_export($result,
true);
1382 if ($this->jobtype ==
'command') {
1383 global $dolibarr_cron_allow_cli;
1385 if (empty($dolibarr_cron_allow_cli)) {
1386 $langs->load(
"errors");
1387 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1388 $this->lastoutput =
'';
1389 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1391 $outputdir = $conf->cron->dir_temp;
1392 if (empty($outputdir)) {
1393 $outputdir = $conf->cronjob->dir_temp;
1396 if (!empty($outputdir)) {
1398 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1401 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1402 $utils =
new Utils($this->db);
1403 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1405 $this->error = $arrayresult[
'error'];
1406 $this->lastoutput = $arrayresult[
'output'];
1407 $this->lastresult = (string) $arrayresult[
'result'];
1412 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1414 $this->datelastresult =
dol_now();
1415 $this->processing = 0;
1417 $result = $this->
update($user);
1419 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1420 $conf->setEntityValues($this->db, $savcurrententity);
1424 $conf->setEntityValues($this->db, $savcurrententity);
1426 if ($error && !empty($this->email_alert)) {
1427 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1428 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1429 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1431 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1432 $result = $cmailfile->sendfile();
1435 return $error ? -1 : 1;
1450 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1452 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1453 $user =
new User($this->db);
1454 $result = $user->fetch(0, $userlogin);
1456 $this->error =
"User Error : ".$user->error;
1457 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1460 if (empty($user->id)) {
1461 $this->error =
" User user login:".$userlogin.
" do not exists";
1462 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1467 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1469 if (empty($this->datenextrun)) {
1470 if (empty($this->datestart)) {
1471 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1474 $this->datenextrun = $now + ($this->frequency * (int) $this->unitfrequency);
1477 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1480 $this->datenextrun = $this->datestart + ($this->frequency * (int) $this->unitfrequency);
1485 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1487 while ($this->datenextrun < $now) {
1488 if (!is_numeric($this->unitfrequency) || (
int) $this->unitfrequency == 2678400 || (
int) $this->unitfrequency <= 0) {
1491 $this->datenextrun += ($this->frequency * (int) $this->unitfrequency);
1495 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1500 if ($this->autodelete == 2) {
1501 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1502 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1503 $this->
status = self::STATUS_ARCHIVED;
1504 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1508 $result = $this->
update($user);
1510 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);