320 global
$conf, $langs;
326 if (isset($this->label)) {
327 $this->label = trim($this->label);
329 if (isset($this->jobtype)) {
330 $this->jobtype = trim($this->jobtype);
332 if (isset($this->command)) {
333 $this->command = trim($this->command);
335 if (isset($this->classesname)) {
336 $this->classesname = trim($this->classesname);
338 if (isset($this->objectname)) {
339 $this->objectname = trim($this->objectname);
341 if (isset($this->methodename)) {
342 $this->methodename = trim($this->methodename);
344 if (isset($this->params)) {
345 $this->params = trim($this->params);
347 if (isset($this->md5params)) {
348 $this->md5params = trim($this->md5params);
350 if (isset($this->module_name)) {
351 $this->module_name = trim($this->module_name);
353 if (isset($this->lastoutput)) {
354 $this->lastoutput = trim($this->lastoutput);
355 $this->lastoutput =
dol_substr($this->lastoutput, 0, self::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
357 if (isset($this->lastresult)) {
358 $this->lastresult = trim($this->lastresult);
360 if (isset($this->unitfrequency)) {
361 $this->unitfrequency = trim($this->unitfrequency);
363 if (isset($this->frequency)) {
364 $this->frequency = (int) $this->frequency;
366 if (isset($this->
status)) {
369 if (isset($this->note_private)) {
370 $this->note_private = trim($this->note_private);
372 if (isset($this->nbrun)) {
373 $this->nbrun = (int) $this->nbrun;
375 if (isset($this->maxrun)) {
376 $this->maxrun = (int) $this->maxrun;
378 if (isset($this->libname)) {
379 $this->libname = trim($this->libname);
381 if (isset($this->test)) {
382 $this->test = trim($this->test);
388 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
391 if (empty($this->label)) {
392 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
395 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
396 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
399 if (empty($this->unitfrequency)) {
400 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
403 if (($this->jobtype ==
'command') && (empty($this->command))) {
404 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
407 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
408 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
411 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
412 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
415 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
416 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
419 if (($this->jobtype ==
'function') && (empty($this->libname))) {
420 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
425 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
431 $sql .=
"classesname,";
432 $sql .=
"objectname,";
433 $sql .=
"methodename,";
435 $sql .=
"md5params,";
436 $sql .=
"module_name,";
438 $sql .=
"datelastrun,";
439 $sql .=
"datenextrun,";
441 $sql .=
"datestart,";
442 $sql .=
"lastresult,";
443 $sql .=
"datelastresult,";
444 $sql .=
"lastoutput,";
445 $sql .=
"unitfrequency,";
446 $sql .=
"frequency,";
448 $sql .=
"fk_user_author,";
449 $sql .=
"fk_user_mod,";
455 $sql .=
") VALUES (";
456 $sql .=
" ".(!isset($this->entity) ? (int)
$conf->entity : (int) $this->entity).
",";
457 $sql .=
" '".$this->db->idate($now).
"',";
458 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
459 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
460 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
461 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
462 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
463 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
464 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
465 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
466 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
467 $sql .=
" ".(is_numeric($this->priority) ? (int) $this->priority : 50).
",";
468 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
469 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
470 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
471 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
472 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
473 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
474 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
475 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
476 $sql .=
" ".(!isset($this->frequency) ?
'0' : ((int) $this->frequency)).
",";
477 $sql .=
" ".(!isset($this->
status) ?
'0' : ((int) $this->
status)).
",";
478 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
479 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
480 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
481 $sql .=
" ".(!isset($this->nbrun) ?
'0' : ((int) $this->nbrun)).
",";
482 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $this->maxrun)).
",";
483 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
484 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'");
489 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
490 $resql = $this->db->query($sql);
493 $this->errors[] =
"Error ".$this->db->lasterror();
497 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
500 if (!$error && !$notrigger) {
502 $result = $this->call_trigger(
'CRONJOB_CREATE', $user);
511 $this->db->rollback();
529 public function fetch(
int $id,
string $objectname =
'',
string $methodname =
'',
string $label =
'')
533 $sql .=
" t.entity,";
536 $sql .=
" t.jobtype,";
538 $sql .=
" t.command,";
539 $sql .=
" t.classesname,";
540 $sql .=
" t.objectname,";
541 $sql .=
" t.methodename,";
542 $sql .=
" t.params,";
543 $sql .=
" t.md5params,";
544 $sql .=
" t.module_name,";
545 $sql .=
" t.priority,";
546 $sql .=
" t.datelastrun,";
547 $sql .=
" t.datenextrun,";
548 $sql .=
" t.dateend,";
549 $sql .=
" t.datestart,";
550 $sql .=
" t.lastresult,";
551 $sql .=
" t.datelastresult,";
552 $sql .=
" t.lastoutput,";
553 $sql .=
" t.unitfrequency,";
554 $sql .=
" t.frequency,";
555 $sql .=
" t.status,";
556 $sql .=
" t.processing,";
558 $sql .=
" t.email_alert,";
559 $sql .=
" t.fk_user_author,";
560 $sql .=
" t.fk_user_mod,";
561 $sql .=
" t.note as note_private,";
563 $sql .=
" t.maxrun,";
564 $sql .=
" t.libname,";
566 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
568 $sql .=
" WHERE t.rowid = ".((int)
$id);
570 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
571 $sql .=
" AND t.label = '".$this->db->escape($label).
"'";
573 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
574 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
575 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
578 $resql = $this->db->query($sql);
580 if ($this->db->num_rows($resql)) {
581 $obj = $this->db->fetch_object($resql);
583 $this->
id = $obj->rowid;
584 $this->
ref = $obj->rowid;
585 $this->entity = $obj->entity;
586 $this->tms = $this->db->jdate($obj->tms);
587 $this->date_modification = $this->db->jdate($obj->tms);
588 $this->datec = $this->db->jdate($obj->datec);
589 $this->label = $obj->label;
590 $this->jobtype = $obj->jobtype;
591 $this->command = $obj->command;
592 $this->classesname = $obj->classesname;
593 $this->objectname = $obj->objectname;
594 $this->methodename = $obj->methodename;
595 $this->params = $obj->params;
596 $this->md5params = $obj->md5params;
597 $this->module_name = $obj->module_name;
598 $this->priority = $obj->priority;
599 $this->datelastrun = $this->db->jdate($obj->datelastrun);
600 $this->datenextrun = $this->db->jdate($obj->datenextrun);
601 $this->dateend = $this->db->jdate($obj->dateend);
602 $this->datestart = $this->db->jdate($obj->datestart);
603 $this->lastresult = (string) $obj->lastresult;
604 $this->lastoutput = $obj->lastoutput;
605 $this->datelastresult = $this->db->jdate($obj->datelastresult);
606 $this->unitfrequency = $obj->unitfrequency;
607 $this->frequency = $obj->frequency;
608 $this->
status = $obj->status;
609 $this->processing = $obj->processing;
610 $this->pid = $obj->pid;
611 $this->email_alert = $obj->email_alert;
612 $this->fk_user_author = $obj->fk_user_author;
613 $this->fk_user_mod = $obj->fk_user_mod;
614 $this->note_private = $obj->note_private;
615 $this->nbrun = $obj->nbrun;
616 $this->maxrun = $obj->maxrun;
617 $this->libname = $obj->libname;
618 $this->test = $obj->test;
620 $this->db->free($resql);
623 $this->db->free($resql);
627 $this->error =
"Error ".$this->db->lasterror();
644 public function fetchAll(
string $sortorder =
'DESC',
string $sortfield =
't.rowid',
int $limit = 0,
int $offset = 0,
int $status = 1, $filter =
'',
int $processing = -1)
646 $this->lines = array();
650 $sql .=
" t.entity,";
653 $sql .=
" t.jobtype,";
655 $sql .=
" t.command,";
656 $sql .=
" t.classesname,";
657 $sql .=
" t.objectname,";
658 $sql .=
" t.methodename,";
659 $sql .=
" t.params,";
660 $sql .=
" t.md5params,";
661 $sql .=
" t.module_name,";
662 $sql .=
" t.priority,";
663 $sql .=
" t.datelastrun,";
664 $sql .=
" t.datenextrun,";
665 $sql .=
" t.dateend,";
666 $sql .=
" t.datestart,";
667 $sql .=
" t.lastresult,";
668 $sql .=
" t.datelastresult,";
669 $sql .=
" t.lastoutput,";
670 $sql .=
" t.unitfrequency,";
671 $sql .=
" t.frequency,";
672 $sql .=
" t.status,";
673 $sql .=
" t.processing,";
675 $sql .=
" t.email_alert,";
676 $sql .=
" t.fk_user_author,";
677 $sql .=
" t.fk_user_mod,";
678 $sql .=
" t.note as note_private,";
680 $sql .=
" t.maxrun,";
681 $sql .=
" t.libname,";
683 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
684 $sql .=
" WHERE 1 = 1";
685 if ($processing >= 0) {
686 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
688 if ($status >= 0 && $status < 2) {
689 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
690 } elseif ($status == 2) {
691 $sql .=
" AND t.status = 2";
695 if (is_array($filter)) {
696 if (count($filter) > 0) {
697 foreach ($filter as $key => $value) {
698 if ($key ==
't.rowid') {
699 $sql .=
" AND ".$this->db->sanitize($key).
" = ".((int) $value);
701 $sql .=
" AND ".$this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
713 $this->errors[] = $errormessage;
714 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
718 $sql .= $this->db->order($sortfield, $sortorder);
719 if (!empty($limit) && !empty($offset)) {
720 $sql .= $this->db->plimit($limit + 1, $offset);
723 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
724 $resql = $this->db->query($sql);
726 $num = $this->db->num_rows($resql);
731 $obj = $this->db->fetch_object($resql);
732 $cronjob_obj =
new Cronjob($this->db);
734 $cronjob_obj->id = $obj->rowid;
735 $cronjob_obj->ref = $obj->rowid;
736 $cronjob_obj->entity = $obj->entity;
737 $cronjob_obj->tms = $this->db->jdate($obj->tms);
738 $cronjob_obj->datec = $this->db->jdate($obj->datec);
739 $cronjob_obj->label = $obj->label;
740 $cronjob_obj->jobtype = $obj->jobtype;
741 $cronjob_obj->command = $obj->command;
742 $cronjob_obj->classesname = $obj->classesname;
743 $cronjob_obj->objectname = $obj->objectname;
744 $cronjob_obj->methodename = $obj->methodename;
745 $cronjob_obj->params = $obj->params;
746 $cronjob_obj->md5params = $obj->md5params;
747 $cronjob_obj->module_name = $obj->module_name;
748 $cronjob_obj->priority = $obj->priority;
749 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
750 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
751 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
752 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
753 $cronjob_obj->lastresult = $obj->lastresult;
754 $cronjob_obj->lastoutput = $obj->lastoutput;
755 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
756 $cronjob_obj->unitfrequency = $obj->unitfrequency;
757 $cronjob_obj->frequency = $obj->frequency;
758 $cronjob_obj->status = $obj->status;
759 $cronjob_obj->processing = $obj->processing;
760 $cronjob_obj->pid = $obj->pid;
761 $cronjob_obj->email_alert = $obj->email_alert;
762 $cronjob_obj->fk_user_author = $obj->fk_user_author;
763 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
764 $cronjob_obj->note_private = $obj->note_private;
765 $cronjob_obj->nbrun = $obj->nbrun;
766 $cronjob_obj->maxrun = $obj->maxrun;
767 $cronjob_obj->libname = $obj->libname;
768 $cronjob_obj->test = $obj->test;
770 $this->lines[] = $cronjob_obj;
775 $this->db->free($resql);
779 $this->error =
"Error ".$this->db->lasterror();
792 public function update($user =
null,
int $notrigger = 0)
794 global
$conf, $langs;
796 $langs->load(
'cron');
801 if (isset($this->label)) {
802 $this->label = trim($this->label);
804 if (isset($this->jobtype)) {
805 $this->jobtype = trim($this->jobtype);
807 if (isset($this->command)) {
808 $this->command = trim($this->command);
810 if (isset($this->classesname)) {
811 $this->classesname = trim($this->classesname);
813 if (isset($this->objectname)) {
814 $this->objectname = trim($this->objectname);
816 if (isset($this->methodename)) {
817 $this->methodename = trim($this->methodename);
819 if (isset($this->params)) {
820 $this->params = trim($this->params);
822 if (isset($this->md5params)) {
823 $this->md5params = trim($this->md5params);
825 if (isset($this->module_name)) {
826 $this->module_name = trim($this->module_name);
828 if (isset($this->priority)) {
829 $this->priority = trim($this->priority);
831 if (isset($this->lastoutput)) {
832 $this->lastoutput = trim($this->lastoutput);
833 $this->lastoutput =
dol_substr($this->lastoutput, 0, self::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
835 if (isset($this->lastresult)) {
836 $this->lastresult = trim($this->lastresult);
838 if (isset($this->unitfrequency)) {
839 $this->unitfrequency = trim($this->unitfrequency);
841 if (isset($this->frequency)) {
842 $this->frequency = (int) $this->frequency;
844 if (isset($this->
status)) {
847 if (isset($this->note_private)) {
848 $this->note_private = trim($this->note_private);
850 if (isset($this->nbrun)) {
851 $this->nbrun = (is_numeric($this->nbrun)) ? (
int) trim((
string) $this->nbrun) : 0;
853 if (isset($this->libname)) {
854 $this->libname = trim($this->libname);
856 if (isset($this->test)) {
857 $this->test = trim($this->test);
860 if (empty($this->maxrun)) {
863 if (empty($this->processing)) {
864 $this->processing = 0;
866 if (empty($this->pid)) {
869 if (empty($this->email_alert)) {
870 $this->email_alert =
'';
872 if (empty($this->datenextrun)) {
873 $this->datenextrun =
dol_now();
878 if (
dol_strlen($this->datenextrun) == 0 && $this->
status == self::STATUS_ENABLED) {
879 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
882 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
883 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
886 if (empty($this->label)) {
887 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
890 if (empty($this->unitfrequency)) {
891 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
894 if (($this->jobtype ==
'command') && (empty($this->command))) {
895 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
898 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
899 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
902 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
903 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
906 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
907 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
911 if (($this->jobtype ==
'function') && (empty($this->libname))) {
912 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
918 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
919 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) : ((int)
$conf->entity)).
",";
920 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
921 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
922 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
923 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
924 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
925 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
926 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
927 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
928 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
929 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
930 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
931 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
932 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
933 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
934 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
935 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
936 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
937 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ?
"'".$this->db->escape($this->unitfrequency).
"'" :
"null").
",";
938 $sql .=
" frequency=".(isset($this->frequency) ? ((int) $this->frequency) :
"null").
",";
939 $sql .=
" status=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
940 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? ((
int) $this->processing) :
"0").
",";
941 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
942 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
943 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
944 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
945 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? ((
int) $this->nbrun) :
"null").
",";
946 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? ((
int) $this->maxrun) :
"0").
",";
947 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
948 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
949 $sql .=
" WHERE rowid=".((int) $this->
id);
953 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
954 $resql = $this->db->query($sql);
957 $this->errors[] =
"Error ".$this->db->lasterror();
960 if (!$error && $user && !$notrigger) {
962 $result = $this->call_trigger(
'CRONJOB_MODIFY', $user);
971 foreach ($this->errors as $errmsg) {
972 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
973 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
975 $this->db->rollback();
1302 global $langs,
$conf, $hookmanager;
1304 $hookmanager->initHooks(array(
'cron'));
1309 $langs->load(
'cron');
1311 if (empty($userlogin)) {
1312 $this->error =
"User login is mandatory";
1313 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1319 if (
$conf->entity != $this->entity && $this->entity > 0) {
1320 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".
$conf->entity, LOG_WARNING);
1322 $savcurrententity =
$conf->entity;
1323 $conf->setEntityValues($this->db, $this->entity);
1324 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".
$conf->entity);
1326 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1327 $user =
new User($this->db);
1328 $result = $user->fetch(0, $userlogin);
1330 $this->error =
"User Error:".$user->error;
1331 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1332 $conf->setEntityValues($this->db, $savcurrententity);
1335 if (empty($user->id)) {
1336 $this->error =
"User login: ".$userlogin.
" does not exist";
1337 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1338 $conf->setEntityValues($this->db, $savcurrententity);
1343 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1347 if (!empty($ExecTimeLimit)) {
1348 $err = error_reporting();
1351 @set_time_limit($ExecTimeLimit);
1352 error_reporting($err);
1355 if (!empty($MemoryLimit)) {
1356 @ini_set(
'memory_limit', $MemoryLimit);
1360 $this->datelastrun = $now;
1361 $this->datelastresult =
null;
1362 $this->lastoutput =
'';
1363 $this->lastresult =
'';
1364 $this->processing = 1;
1365 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1367 $result = $this->
update($user);
1369 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1370 $conf->setEntityValues($this->db, $savcurrententity);
1377 $cronjobid = (int) $this->
id;
1378 $cronjobpid = (int) $this->pid;
1380 register_shutdown_function(
static function () use ($cronjobid, $cronjobpid, $dbs) {
1384 if (empty($cronjobid) || empty($dbs) || empty($dbs->connected)) {
1390 $dbs->rollback(
'cron register_shutdown_function');
1391 } catch (Throwable $e) {
1396 $sql =
"SELECT processing, pid, datelastresult FROM ".MAIN_DB_PREFIX.
"cronjob WHERE rowid = ".((int) $cronjobid);
1397 $resql = $dbs->query($sql);
1401 $obj = $dbs->fetch_object($resql);
1404 if (!$obj || (
int) $obj->processing !== 1 || !empty($obj->datelastresult)) {
1409 if (!empty($obj->pid) && (int) $obj->pid !== (
int) $cronjobpid) {
1414 $lastoutput =
'Cron job aborted unexpectedly (shutdown).';
1416 $lastError = error_get_last();
1417 if (is_array($lastError) && !empty($lastError[
'message'])) {
1418 $lastoutput .=
' Last error: '.dol_trunc($lastError[
'message'], 2000,
'right',
'UTF-8', 1);
1421 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob";
1422 $sql .=
" SET processing = 0, pid = NULL, datelastresult = '".$dbs->idate($now).
"', lastresult = '-1', lastoutput = '".$dbs->escape($lastoutput).
"'";
1423 $sql .=
" WHERE rowid = ".((int) $cronjobid).
" AND processing = 1 AND datelastresult IS NULL";
1428 if ($this->jobtype ==
'method') {
1430 if (!empty($this->entity) && !empty($this->module_name) && !
isModEnabled(strtolower($this->module_name))) {
1431 $this->error = $langs->transnoentitiesnoconv(
'CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1432 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1433 $this->lastoutput = $this->error;
1434 $this->lastresult =
'-1';
1441 if ($ret ===
false || (!class_exists($this->objectname))) {
1442 if ($ret ===
false) {
1443 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1445 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1447 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1448 $this->lastoutput = $this->error;
1449 $this->lastresult =
'-1';
1456 if (!method_exists($this->objectname, $this->methodename)) {
1457 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1458 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1459 $this->lastoutput = $this->error;
1460 $this->lastresult =
'-1';
1463 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1464 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1465 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1466 $this->lastoutput = $this->error;
1467 $this->lastresult =
'-1';
1475 if (!$error && !empty($this->module_name)) {
1476 $result = $langs->load($this->module_name);
1477 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1480 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1481 $this->error = $langs->error;
1482 $this->lastoutput = $this->error;
1483 $this->lastresult =
'-1';
1489 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);
1492 $nameofclass = (string) $this->objectname;
1493 $object =
new $nameofclass($this->db);
1494 if ($this->entity > 0) {
1495 $object->entity = $this->entity;
1498 $params_arr = array();
1499 if (!empty($this->params) || $this->params ===
'0') {
1500 $params_arr = array_map(
'trim', explode(
",", $this->params));
1503 if (!is_array($params_arr)) {
1504 $result = call_user_func(array(
$object, $this->methodename), $this->params);
1506 $result = call_user_func_array(array(
$object, $this->methodename), $params_arr);
1509 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1510 $langs->load(
"errors");
1516 $errmsg .= (($errmsg ?
', ' :
'').implode(
', ',
$object->errors));
1518 if (empty($errmsg)) {
1519 $errmsg = $langs->trans(
'ErrorUnknown');
1522 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1524 $this->error = $errmsg;
1525 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1526 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1529 dol_syslog(get_class($this).
"::run_jobs END");
1530 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1531 $this->lastresult = var_export($result,
true);
1536 if ($this->jobtype ==
'function') {
1538 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1540 if ($ret ===
false) {
1541 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1542 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1543 $this->lastoutput = $this->error;
1544 $this->lastresult =
'-1';
1550 $result = $langs->load($this->module_name);
1551 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1553 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1554 $this->error = $langs->error;
1555 $this->lastoutput = $this->error;
1556 $this->lastresult =
'-1';
1562 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1564 $params_arr = array();
1565 if (!empty($this->params) || $this->params ===
'0') {
1566 $params_arr = array_map(
'trim', explode(
",", $this->params));
1569 $result = call_user_func_array($this->methodename, $params_arr);
1571 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1572 $langs->load(
"errors");
1573 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1574 $this->error = $langs->trans(
'ErrorUnknown');
1575 $this->lastoutput = $this->error;
1576 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1579 $this->lastoutput = var_export($result,
true);
1580 $this->lastresult = var_export($result,
true);
1586 if ($this->jobtype ==
'command') {
1587 global $dolibarr_cron_allow_cli;
1589 if (empty($dolibarr_cron_allow_cli)) {
1590 $langs->load(
"errors");
1591 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1592 $this->lastoutput =
'';
1593 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1596 $outputdir =
$conf->cron->dir_temp;
1597 if (empty($outputdir)) {
1598 $outputdir =
$conf->cronjob->dir_temp;
1602 if (!empty($outputdir)) {
1603 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1606 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1607 $utils =
new Utils($this->db);
1608 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1610 $this->error = $arrayresult[
'error'];
1611 $this->lastoutput = $arrayresult[
'output'];
1612 $this->lastresult = (string) $arrayresult[
'result'];
1614 $langs->load(
"errors");
1615 $this->error = $langs->trans(
"ErrorNoTmpDir", (
string) $outputdir);
1616 $this->lastoutput =
'';
1617 $this->lastresult =
'-1';
1623 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1625 $this->datelastresult =
dol_now();
1626 $this->processing = 0;
1628 $result = $this->
update($user);
1630 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1631 $conf->setEntityValues($this->db, $savcurrententity);
1635 $conf->setEntityValues($this->db, $savcurrententity);
1637 if ($error && !empty($this->email_alert)) {
1638 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1639 $subject = $langs->transnoentitiesnoconv(
"ErrorInBatch", $this->label);
1640 $msg = $langs->transnoentitiesnoconv(
"ErrorInBatch", $this->label);
1642 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1643 $result = $cmailfile->sendfile();
1646 return $error ? -1 : 1;
1661 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1663 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1664 $user =
new User($this->db);
1665 $result = $user->fetch(0, $userlogin);
1667 $this->error =
"User Error : ".$user->error;
1668 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1671 if (empty($user->id)) {
1672 $this->error =
" User user login:".$userlogin.
" do not exists";
1673 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1678 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1680 if (empty($this->datenextrun)) {
1681 if (empty($this->datestart)) {
1682 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1685 $this->datenextrun = $now + ((int) $this->frequency * (
int) $this->unitfrequency);
1688 if (!is_numeric($this->frequency) || (int) $this->unitfrequency == 2678400) {
1691 $this->datenextrun = $this->datestart + ((int) $this->frequency * (
int) $this->unitfrequency);
1696 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1698 while ($this->datenextrun < $now) {
1699 if (!is_numeric($this->unitfrequency) || (int) $this->unitfrequency == 2678400 || (
int) $this->unitfrequency <= 0) {
1702 $this->datenextrun += ((int) $this->frequency * (
int) $this->unitfrequency);
1706 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1711 if ($this->autodelete == 2) {
1712 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1713 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1714 $this->
status = self::STATUS_ARCHIVED;
1715 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1719 $result = $this->
update($user);
1721 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);