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");
502 $this->db->rollback();
520 public function fetch(
int $id,
string $objectname =
'',
string $methodname =
'',
string $label =
'')
524 $sql .=
" t.entity,";
527 $sql .=
" t.jobtype,";
529 $sql .=
" t.command,";
530 $sql .=
" t.classesname,";
531 $sql .=
" t.objectname,";
532 $sql .=
" t.methodename,";
533 $sql .=
" t.params,";
534 $sql .=
" t.md5params,";
535 $sql .=
" t.module_name,";
536 $sql .=
" t.priority,";
537 $sql .=
" t.datelastrun,";
538 $sql .=
" t.datenextrun,";
539 $sql .=
" t.dateend,";
540 $sql .=
" t.datestart,";
541 $sql .=
" t.lastresult,";
542 $sql .=
" t.datelastresult,";
543 $sql .=
" t.lastoutput,";
544 $sql .=
" t.unitfrequency,";
545 $sql .=
" t.frequency,";
546 $sql .=
" t.status,";
547 $sql .=
" t.processing,";
549 $sql .=
" t.email_alert,";
550 $sql .=
" t.fk_user_author,";
551 $sql .=
" t.fk_user_mod,";
552 $sql .=
" t.note as note_private,";
554 $sql .=
" t.maxrun,";
555 $sql .=
" t.libname,";
557 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
559 $sql .=
" WHERE t.rowid = ".((int)
$id);
561 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
562 $sql .=
" AND t.label = '".$this->db->escape($label).
"'";
564 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
565 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
566 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
569 $resql = $this->db->query($sql);
571 if ($this->db->num_rows($resql)) {
572 $obj = $this->db->fetch_object($resql);
574 $this->
id = $obj->rowid;
575 $this->
ref = $obj->rowid;
576 $this->entity = $obj->entity;
577 $this->tms = $this->db->jdate($obj->tms);
578 $this->date_modification = $this->db->jdate($obj->tms);
579 $this->datec = $this->db->jdate($obj->datec);
580 $this->label = $obj->label;
581 $this->jobtype = $obj->jobtype;
582 $this->command = $obj->command;
583 $this->classesname = $obj->classesname;
584 $this->objectname = $obj->objectname;
585 $this->methodename = $obj->methodename;
586 $this->params = $obj->params;
587 $this->md5params = $obj->md5params;
588 $this->module_name = $obj->module_name;
589 $this->priority = $obj->priority;
590 $this->datelastrun = $this->db->jdate($obj->datelastrun);
591 $this->datenextrun = $this->db->jdate($obj->datenextrun);
592 $this->dateend = $this->db->jdate($obj->dateend);
593 $this->datestart = $this->db->jdate($obj->datestart);
594 $this->lastresult = (
string) $obj->lastresult;
595 $this->lastoutput = $obj->lastoutput;
596 $this->datelastresult = $this->db->jdate($obj->datelastresult);
597 $this->unitfrequency = $obj->unitfrequency;
598 $this->frequency = $obj->frequency;
599 $this->
status = $obj->status;
600 $this->processing = $obj->processing;
601 $this->pid = $obj->pid;
602 $this->email_alert = $obj->email_alert;
603 $this->fk_user_author = $obj->fk_user_author;
604 $this->fk_user_mod = $obj->fk_user_mod;
605 $this->note_private = $obj->note_private;
606 $this->nbrun = $obj->nbrun;
607 $this->maxrun = $obj->maxrun;
608 $this->libname = $obj->libname;
609 $this->test = $obj->test;
611 $this->db->free($resql);
615 $this->error =
"Error ".$this->db->lasterror();
632 public function fetchAll(
string $sortorder =
'DESC',
string $sortfield =
't.rowid',
int $limit = 0,
int $offset = 0,
int $status = 1, $filter =
'',
int $processing = -1)
634 $this->lines = array();
638 $sql .=
" t.entity,";
641 $sql .=
" t.jobtype,";
643 $sql .=
" t.command,";
644 $sql .=
" t.classesname,";
645 $sql .=
" t.objectname,";
646 $sql .=
" t.methodename,";
647 $sql .=
" t.params,";
648 $sql .=
" t.md5params,";
649 $sql .=
" t.module_name,";
650 $sql .=
" t.priority,";
651 $sql .=
" t.datelastrun,";
652 $sql .=
" t.datenextrun,";
653 $sql .=
" t.dateend,";
654 $sql .=
" t.datestart,";
655 $sql .=
" t.lastresult,";
656 $sql .=
" t.datelastresult,";
657 $sql .=
" t.lastoutput,";
658 $sql .=
" t.unitfrequency,";
659 $sql .=
" t.frequency,";
660 $sql .=
" t.status,";
661 $sql .=
" t.processing,";
663 $sql .=
" t.email_alert,";
664 $sql .=
" t.fk_user_author,";
665 $sql .=
" t.fk_user_mod,";
666 $sql .=
" t.note as note_private,";
668 $sql .=
" t.maxrun,";
669 $sql .=
" t.libname,";
671 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
672 $sql .=
" WHERE 1 = 1";
673 if ($processing >= 0) {
674 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
676 if ($status >= 0 && $status < 2) {
677 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
678 } elseif ($status == 2) {
679 $sql .=
" AND t.status = 2";
683 if (is_array($filter)) {
684 if (count($filter) > 0) {
685 foreach ($filter as $key => $value) {
686 if ($key ==
't.rowid') {
687 $sql .=
" AND ".$this->db->sanitize($key).
" = ".((int) $value);
689 $sql .=
" AND ".$this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
699 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
701 $this->errors[] = $errormessage;
702 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
706 $sql .= $this->db->order($sortfield, $sortorder);
707 if (!empty($limit) && !empty($offset)) {
708 $sql .= $this->db->plimit($limit + 1, $offset);
711 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
712 $resql = $this->db->query($sql);
714 $num = $this->db->num_rows($resql);
719 $obj = $this->db->fetch_object($resql);
720 $cronjob_obj =
new Cronjob($this->db);
722 $cronjob_obj->id = $obj->rowid;
723 $cronjob_obj->ref = $obj->rowid;
724 $cronjob_obj->entity = $obj->entity;
725 $cronjob_obj->tms = $this->db->jdate($obj->tms);
726 $cronjob_obj->datec = $this->db->jdate($obj->datec);
727 $cronjob_obj->label = $obj->label;
728 $cronjob_obj->jobtype = $obj->jobtype;
729 $cronjob_obj->command = $obj->command;
730 $cronjob_obj->classesname = $obj->classesname;
731 $cronjob_obj->objectname = $obj->objectname;
732 $cronjob_obj->methodename = $obj->methodename;
733 $cronjob_obj->params = $obj->params;
734 $cronjob_obj->md5params = $obj->md5params;
735 $cronjob_obj->module_name = $obj->module_name;
736 $cronjob_obj->priority = $obj->priority;
737 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
738 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
739 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
740 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
741 $cronjob_obj->lastresult = $obj->lastresult;
742 $cronjob_obj->lastoutput = $obj->lastoutput;
743 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
744 $cronjob_obj->unitfrequency = $obj->unitfrequency;
745 $cronjob_obj->frequency = $obj->frequency;
746 $cronjob_obj->status = $obj->status;
747 $cronjob_obj->processing = $obj->processing;
748 $cronjob_obj->pid = $obj->pid;
749 $cronjob_obj->email_alert = $obj->email_alert;
750 $cronjob_obj->fk_user_author = $obj->fk_user_author;
751 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
752 $cronjob_obj->note_private = $obj->note_private;
753 $cronjob_obj->nbrun = $obj->nbrun;
754 $cronjob_obj->maxrun = $obj->maxrun;
755 $cronjob_obj->libname = $obj->libname;
756 $cronjob_obj->test = $obj->test;
758 $this->lines[] = $cronjob_obj;
763 $this->db->free($resql);
767 $this->error =
"Error ".$this->db->lasterror();
780 public function update($user =
null,
int $notrigger = 0)
782 global
$conf, $langs;
784 $langs->load(
'cron');
789 if (isset($this->label)) {
790 $this->label = trim($this->label);
792 if (isset($this->jobtype)) {
793 $this->jobtype = trim($this->jobtype);
795 if (isset($this->command)) {
796 $this->command = trim($this->command);
798 if (isset($this->classesname)) {
799 $this->classesname = trim($this->classesname);
801 if (isset($this->objectname)) {
802 $this->objectname = trim($this->objectname);
804 if (isset($this->methodename)) {
805 $this->methodename = trim($this->methodename);
807 if (isset($this->params)) {
808 $this->params = trim($this->params);
810 if (isset($this->md5params)) {
811 $this->md5params = trim($this->md5params);
813 if (isset($this->module_name)) {
814 $this->module_name = trim($this->module_name);
816 if (isset($this->priority)) {
817 $this->priority = trim($this->priority);
819 if (isset($this->lastoutput)) {
820 $this->lastoutput = trim($this->lastoutput);
821 $this->lastoutput =
dol_substr($this->lastoutput, 0, self::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
823 if (isset($this->lastresult)) {
824 $this->lastresult = trim($this->lastresult);
826 if (isset($this->unitfrequency)) {
827 $this->unitfrequency = trim($this->unitfrequency);
829 if (isset($this->frequency)) {
830 $this->frequency = (int) $this->frequency;
832 if (isset($this->
status)) {
835 if (isset($this->note_private)) {
836 $this->note_private = trim($this->note_private);
838 if (isset($this->nbrun)) {
839 $this->nbrun = (is_numeric($this->nbrun)) ? (
int) trim((
string) $this->nbrun) : 0;
841 if (isset($this->libname)) {
842 $this->libname = trim($this->libname);
844 if (isset($this->test)) {
845 $this->test = trim($this->test);
848 if (empty($this->maxrun)) {
851 if (empty($this->processing)) {
852 $this->processing = 0;
854 if (empty($this->pid)) {
857 if (empty($this->email_alert)) {
858 $this->email_alert =
'';
860 if (empty($this->datenextrun)) {
861 $this->datenextrun =
dol_now();
866 if (
dol_strlen($this->datenextrun) == 0 && $this->
status == self::STATUS_ENABLED) {
867 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
870 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
871 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
874 if (empty($this->label)) {
875 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
878 if (empty($this->unitfrequency)) {
879 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
882 if (($this->jobtype ==
'command') && (empty($this->command))) {
883 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
886 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
887 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
890 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
891 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
894 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
895 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
899 if (($this->jobtype ==
'function') && (empty($this->libname))) {
900 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
906 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
907 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) :
$conf->entity).
",";
908 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
909 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
910 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
911 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
912 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
913 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
914 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
915 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
916 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
917 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
918 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
919 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
920 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
921 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
922 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
923 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
924 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
925 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ?
"'".$this->db->escape($this->unitfrequency).
"'" :
"null").
",";
926 $sql .=
" frequency=".(isset($this->frequency) ? ((int) $this->frequency) :
"null").
",";
927 $sql .=
" status=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
928 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
929 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
930 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
931 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
932 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
933 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
934 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
935 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
936 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
937 $sql .=
" WHERE rowid=".((int) $this->
id);
941 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
942 $resql = $this->db->query($sql);
945 $this->errors[] =
"Error ".$this->db->lasterror();
950 foreach ($this->errors as $errmsg) {
951 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
952 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
954 $this->db->rollback();
1272 global $langs,
$conf, $hookmanager;
1274 $hookmanager->initHooks(array(
'cron'));
1279 $langs->load(
'cron');
1281 if (empty($userlogin)) {
1282 $this->error =
"User login is mandatory";
1283 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1289 if (
$conf->entity != $this->entity && $this->entity > 0) {
1290 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".
$conf->entity, LOG_WARNING);
1292 $savcurrententity =
$conf->entity;
1293 $conf->setEntityValues($this->db, $this->entity);
1294 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".
$conf->entity);
1296 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1297 $user =
new User($this->db);
1298 $result = $user->fetch(0, $userlogin);
1300 $this->error =
"User Error:".$user->error;
1301 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1302 $conf->setEntityValues($this->db, $savcurrententity);
1305 if (empty($user->id)) {
1306 $this->error =
"User login: ".$userlogin.
" does not exist";
1307 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1308 $conf->setEntityValues($this->db, $savcurrententity);
1313 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1317 if (!empty($ExecTimeLimit)) {
1318 $err = error_reporting();
1321 @set_time_limit($ExecTimeLimit);
1322 error_reporting($err);
1325 if (!empty($MemoryLimit)) {
1326 @ini_set(
'memory_limit', $MemoryLimit);
1330 $this->datelastrun = $now;
1331 $this->datelastresult =
null;
1332 $this->lastoutput =
'';
1333 $this->lastresult =
'';
1334 $this->processing = 1;
1335 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1337 $result = $this->
update($user);
1339 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1340 $conf->setEntityValues($this->db, $savcurrententity);
1347 $cronjobid = (int) $this->
id;
1348 $cronjobpid = (int) $this->pid;
1350 register_shutdown_function(
static function () use ($cronjobid, $cronjobpid, $dbs) {
1351 if (empty($cronjobid) || empty($dbs)) {
1358 }
catch (Throwable $e) {
1363 $sql =
"SELECT processing, pid, datelastresult FROM ".MAIN_DB_PREFIX.
"cronjob WHERE rowid = ".((int) $cronjobid);
1364 $resql = $dbs->query($sql);
1368 $obj = $dbs->fetch_object($resql);
1371 if (!$obj || (
int) $obj->processing !== 1 || !empty($obj->datelastresult)) {
1376 if (!empty($obj->pid) && (int) $obj->pid !== (
int) $cronjobpid) {
1381 $lastoutput =
'Cron job aborted unexpectedly (shutdown).';
1383 $lastError = error_get_last();
1384 if (is_array($lastError) && !empty($lastError[
'message'])) {
1385 $lastoutput .=
' Last error: '.dol_trunc($lastError[
'message'], 2000,
'right',
'UTF-8', 1);
1388 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob";
1389 $sql .=
" SET processing = 0, pid = NULL, datelastresult = '".$dbs->idate($now).
"', lastresult = '-1', lastoutput = '".$dbs->escape($lastoutput).
"'";
1390 $sql .=
" WHERE rowid = ".((int) $cronjobid).
" AND processing = 1 AND datelastresult IS NULL";
1395 if ($this->jobtype ==
'method') {
1397 if (!empty($this->entity) && !empty($this->module_name) && !
isModEnabled(strtolower($this->module_name))) {
1398 $this->error = $langs->transnoentitiesnoconv(
'CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1399 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1400 $this->lastoutput = $this->error;
1401 $this->lastresult =
'-1';
1408 if ($ret ===
false || (!class_exists($this->objectname))) {
1409 if ($ret ===
false) {
1410 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1412 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1414 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1415 $this->lastoutput = $this->error;
1416 $this->lastresult =
'-1';
1423 if (!method_exists($this->objectname, $this->methodename)) {
1424 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1425 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1426 $this->lastoutput = $this->error;
1427 $this->lastresult =
'-1';
1430 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1431 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $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 $result = $langs->load($this->module_name);
1442 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1445 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1446 $this->error = $langs->error;
1447 $this->lastoutput = $this->error;
1448 $this->lastresult =
'-1';
1454 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);
1457 $nameofclass = (
string) $this->objectname;
1458 $object =
new $nameofclass($this->db);
1459 if ($this->entity > 0) {
1460 $object->entity = $this->entity;
1463 $params_arr = array();
1464 if (!empty($this->params) || $this->params ===
'0') {
1465 $params_arr = array_map(
'trim', explode(
",", $this->params));
1468 if (!is_array($params_arr)) {
1469 $result = call_user_func(array(
$object, $this->methodename), $this->params);
1471 $result = call_user_func_array(array(
$object, $this->methodename), $params_arr);
1474 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1475 $langs->load(
"errors");
1481 $errmsg .= (($errmsg ?
', ' :
'').implode(
', ',
$object->errors));
1483 if (empty($errmsg)) {
1484 $errmsg = $langs->trans(
'ErrorUnknown');
1487 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1489 $this->error = $errmsg;
1490 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1491 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1494 dol_syslog(get_class($this).
"::run_jobs END");
1495 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1496 $this->lastresult = var_export($result,
true);
1501 if ($this->jobtype ==
'function') {
1503 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1505 if ($ret ===
false) {
1506 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1507 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1508 $this->lastoutput = $this->error;
1509 $this->lastresult =
'-1';
1515 $result = $langs->load($this->module_name);
1516 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1518 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1519 $this->error = $langs->error;
1520 $this->lastoutput = $this->error;
1521 $this->lastresult =
'-1';
1527 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1529 $params_arr = array();
1530 if (!empty($this->params) || $this->params ===
'0') {
1531 $params_arr = array_map(
'trim', explode(
",", $this->params));
1534 $result = call_user_func_array($this->methodename, $params_arr);
1536 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1537 $langs->load(
"errors");
1538 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1539 $this->error = $langs->trans(
'ErrorUnknown');
1540 $this->lastoutput = $this->error;
1541 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1544 $this->lastoutput = var_export($result,
true);
1545 $this->lastresult = var_export($result,
true);
1551 if ($this->jobtype ==
'command') {
1552 global $dolibarr_cron_allow_cli;
1554 if (empty($dolibarr_cron_allow_cli)) {
1555 $langs->load(
"errors");
1556 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1557 $this->lastoutput =
'';
1558 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1561 $outputdir =
$conf->cron->dir_temp;
1562 if (empty($outputdir)) {
1563 $outputdir =
$conf->cronjob->dir_temp;
1567 if (!empty($outputdir)) {
1568 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1571 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1572 $utils =
new Utils($this->db);
1573 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1575 $this->error = $arrayresult[
'error'];
1576 $this->lastoutput = $arrayresult[
'output'];
1577 $this->lastresult = (
string) $arrayresult[
'result'];
1579 $langs->load(
"errors");
1580 $this->error = $langs->trans(
"ErrorNoTmpDir", (
string) $outputdir);
1581 $this->lastoutput =
'';
1582 $this->lastresult =
'-1';
1588 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1590 $this->datelastresult =
dol_now();
1591 $this->processing = 0;
1593 $result = $this->
update($user);
1595 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1596 $conf->setEntityValues($this->db, $savcurrententity);
1600 $conf->setEntityValues($this->db, $savcurrententity);
1602 if ($error && !empty($this->email_alert)) {
1603 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1604 $subject = $langs->transnoentitiesnoconv(
"ErrorInBatch", $this->label);
1605 $msg = $langs->transnoentitiesnoconv(
"ErrorInBatch", $this->label);
1607 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1608 $result = $cmailfile->sendfile();
1611 return $error ? -1 : 1;
1626 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1628 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1629 $user =
new User($this->db);
1630 $result = $user->fetch(0, $userlogin);
1632 $this->error =
"User Error : ".$user->error;
1633 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1636 if (empty($user->id)) {
1637 $this->error =
" User user login:".$userlogin.
" do not exists";
1638 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1643 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1645 if (empty($this->datenextrun)) {
1646 if (empty($this->datestart)) {
1647 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1650 $this->datenextrun = $now + ((int) $this->frequency * (
int) $this->unitfrequency);
1653 if (!is_numeric($this->frequency) || (int) $this->unitfrequency == 2678400) {
1656 $this->datenextrun = $this->datestart + ((int) $this->frequency * (
int) $this->unitfrequency);
1661 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1663 while ($this->datenextrun < $now) {
1664 if (!is_numeric($this->unitfrequency) || (int) $this->unitfrequency == 2678400 || (
int) $this->unitfrequency <= 0) {
1667 $this->datenextrun += ((int) $this->frequency * (
int) $this->unitfrequency);
1671 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1676 if ($this->autodelete == 2) {
1677 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1678 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1679 $this->
status = self::STATUS_ARCHIVED;
1680 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1684 $result = $this->
update($user);
1686 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);