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);
356 if (isset($this->lastresult)) {
357 $this->lastresult = trim($this->lastresult);
359 if (isset($this->unitfrequency)) {
360 $this->unitfrequency = trim($this->unitfrequency);
362 if (isset($this->frequency)) {
363 $this->frequency = (int) $this->frequency;
365 if (isset($this->
status)) {
368 if (isset($this->note_private)) {
369 $this->note_private = trim($this->note_private);
371 if (isset($this->nbrun)) {
372 $this->nbrun = (int) $this->nbrun;
374 if (isset($this->maxrun)) {
375 $this->maxrun = (int) $this->maxrun;
377 if (isset($this->libname)) {
378 $this->libname = trim($this->libname);
380 if (isset($this->test)) {
381 $this->test = trim($this->test);
387 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
390 if (empty($this->label)) {
391 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
394 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
395 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
398 if (empty($this->unitfrequency)) {
399 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
402 if (($this->jobtype ==
'command') && (empty($this->command))) {
403 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
406 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
407 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
410 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
411 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
414 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
415 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
418 if (($this->jobtype ==
'function') && (empty($this->libname))) {
419 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
424 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
430 $sql .=
"classesname,";
431 $sql .=
"objectname,";
432 $sql .=
"methodename,";
434 $sql .=
"md5params,";
435 $sql .=
"module_name,";
437 $sql .=
"datelastrun,";
438 $sql .=
"datenextrun,";
440 $sql .=
"datestart,";
441 $sql .=
"lastresult,";
442 $sql .=
"datelastresult,";
443 $sql .=
"lastoutput,";
444 $sql .=
"unitfrequency,";
445 $sql .=
"frequency,";
447 $sql .=
"fk_user_author,";
448 $sql .=
"fk_user_mod,";
454 $sql .=
") VALUES (";
455 $sql .=
" ".(!isset($this->entity) ? (int) $conf->entity : (int) $this->entity).
",";
456 $sql .=
" '".$this->db->idate($now).
"',";
457 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
458 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
459 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
460 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
461 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
462 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
463 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
464 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
465 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
466 $sql .=
" ".(is_numeric($this->priority) ? (int) $this->priority : 50).
",";
467 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
468 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
469 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
470 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
471 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
472 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
473 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
474 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
475 $sql .=
" ".(!isset($this->frequency) ?
'0' : ((int) $this->frequency)).
",";
476 $sql .=
" ".(!isset($this->
status) ?
'0' : ((int) $this->
status)).
",";
477 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
478 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
479 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
480 $sql .=
" ".(!isset($this->nbrun) ?
'0' : ((int) $this->nbrun)).
",";
481 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $this->maxrun)).
",";
482 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
483 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'");
488 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
489 $resql = $this->db->query($sql);
492 $this->errors[] =
"Error ".$this->db->lasterror();
496 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
501 $this->db->rollback();
519 public function fetch(
int $id,
string $objectname =
'',
string $methodname =
'',
string $label =
'')
523 $sql .=
" t.entity,";
526 $sql .=
" t.jobtype,";
528 $sql .=
" t.command,";
529 $sql .=
" t.classesname,";
530 $sql .=
" t.objectname,";
531 $sql .=
" t.methodename,";
532 $sql .=
" t.params,";
533 $sql .=
" t.md5params,";
534 $sql .=
" t.module_name,";
535 $sql .=
" t.priority,";
536 $sql .=
" t.datelastrun,";
537 $sql .=
" t.datenextrun,";
538 $sql .=
" t.dateend,";
539 $sql .=
" t.datestart,";
540 $sql .=
" t.lastresult,";
541 $sql .=
" t.datelastresult,";
542 $sql .=
" t.lastoutput,";
543 $sql .=
" t.unitfrequency,";
544 $sql .=
" t.frequency,";
545 $sql .=
" t.status,";
546 $sql .=
" t.processing,";
548 $sql .=
" t.email_alert,";
549 $sql .=
" t.fk_user_author,";
550 $sql .=
" t.fk_user_mod,";
551 $sql .=
" t.note as note_private,";
553 $sql .=
" t.maxrun,";
554 $sql .=
" t.libname,";
556 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
558 $sql .=
" WHERE t.rowid = ".((int)
$id);
560 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
561 $sql .=
" AND t.label = '".$this->db->escape($label).
"'";
563 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
564 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
565 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
568 $resql = $this->db->query($sql);
570 if ($this->db->num_rows($resql)) {
571 $obj = $this->db->fetch_object($resql);
573 $this->
id = $obj->rowid;
574 $this->
ref = $obj->rowid;
575 $this->entity = $obj->entity;
576 $this->tms = $this->db->jdate($obj->tms);
577 $this->date_modification = $this->db->jdate($obj->tms);
578 $this->datec = $this->db->jdate($obj->datec);
579 $this->label = $obj->label;
580 $this->jobtype = $obj->jobtype;
581 $this->command = $obj->command;
582 $this->classesname = $obj->classesname;
583 $this->objectname = $obj->objectname;
584 $this->methodename = $obj->methodename;
585 $this->params = $obj->params;
586 $this->md5params = $obj->md5params;
587 $this->module_name = $obj->module_name;
588 $this->priority = $obj->priority;
589 $this->datelastrun = $this->db->jdate($obj->datelastrun);
590 $this->datenextrun = $this->db->jdate($obj->datenextrun);
591 $this->dateend = $this->db->jdate($obj->dateend);
592 $this->datestart = $this->db->jdate($obj->datestart);
593 $this->lastresult = (string) $obj->lastresult;
594 $this->lastoutput = $obj->lastoutput;
595 $this->datelastresult = $this->db->jdate($obj->datelastresult);
596 $this->unitfrequency = $obj->unitfrequency;
597 $this->frequency = $obj->frequency;
598 $this->
status = $obj->status;
599 $this->processing = $obj->processing;
600 $this->pid = $obj->pid;
601 $this->email_alert = $obj->email_alert;
602 $this->fk_user_author = $obj->fk_user_author;
603 $this->fk_user_mod = $obj->fk_user_mod;
604 $this->note_private = $obj->note_private;
605 $this->nbrun = $obj->nbrun;
606 $this->maxrun = $obj->maxrun;
607 $this->libname = $obj->libname;
608 $this->test = $obj->test;
610 $this->db->free($resql);
614 $this->error =
"Error ".$this->db->lasterror();
631 public function fetchAll(
string $sortorder =
'DESC',
string $sortfield =
't.rowid',
int $limit = 0,
int $offset = 0,
int $status = 1, $filter =
'',
int $processing = -1)
633 $this->lines = array();
637 $sql .=
" t.entity,";
640 $sql .=
" t.jobtype,";
642 $sql .=
" t.command,";
643 $sql .=
" t.classesname,";
644 $sql .=
" t.objectname,";
645 $sql .=
" t.methodename,";
646 $sql .=
" t.params,";
647 $sql .=
" t.md5params,";
648 $sql .=
" t.module_name,";
649 $sql .=
" t.priority,";
650 $sql .=
" t.datelastrun,";
651 $sql .=
" t.datenextrun,";
652 $sql .=
" t.dateend,";
653 $sql .=
" t.datestart,";
654 $sql .=
" t.lastresult,";
655 $sql .=
" t.datelastresult,";
656 $sql .=
" t.lastoutput,";
657 $sql .=
" t.unitfrequency,";
658 $sql .=
" t.frequency,";
659 $sql .=
" t.status,";
660 $sql .=
" t.processing,";
662 $sql .=
" t.email_alert,";
663 $sql .=
" t.fk_user_author,";
664 $sql .=
" t.fk_user_mod,";
665 $sql .=
" t.note as note_private,";
667 $sql .=
" t.maxrun,";
668 $sql .=
" t.libname,";
670 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
671 $sql .=
" WHERE 1 = 1";
672 if ($processing >= 0) {
673 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
675 if ($status >= 0 && $status < 2) {
676 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
677 } elseif ($status == 2) {
678 $sql .=
" AND t.status = 2";
682 if (is_array($filter)) {
683 if (count($filter) > 0) {
684 foreach ($filter as $key => $value) {
685 if ($key ==
't.rowid') {
686 $sql .=
" AND ".$this->db->sanitize($key).
" = ".((int) $value);
688 $sql .=
" AND ".$this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
700 $this->errors[] = $errormessage;
701 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
705 $sql .= $this->db->order($sortfield, $sortorder);
706 if (!empty($limit) && !empty($offset)) {
707 $sql .= $this->db->plimit($limit + 1, $offset);
710 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
711 $resql = $this->db->query($sql);
713 $num = $this->db->num_rows($resql);
718 $obj = $this->db->fetch_object($resql);
719 $cronjob_obj =
new Cronjob($this->db);
721 $cronjob_obj->id = $obj->rowid;
722 $cronjob_obj->ref = $obj->rowid;
723 $cronjob_obj->entity = $obj->entity;
724 $cronjob_obj->tms = $this->db->jdate($obj->tms);
725 $cronjob_obj->datec = $this->db->jdate($obj->datec);
726 $cronjob_obj->label = $obj->label;
727 $cronjob_obj->jobtype = $obj->jobtype;
728 $cronjob_obj->command = $obj->command;
729 $cronjob_obj->classesname = $obj->classesname;
730 $cronjob_obj->objectname = $obj->objectname;
731 $cronjob_obj->methodename = $obj->methodename;
732 $cronjob_obj->params = $obj->params;
733 $cronjob_obj->md5params = $obj->md5params;
734 $cronjob_obj->module_name = $obj->module_name;
735 $cronjob_obj->priority = $obj->priority;
736 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
737 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
738 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
739 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
740 $cronjob_obj->lastresult = $obj->lastresult;
741 $cronjob_obj->lastoutput = $obj->lastoutput;
742 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
743 $cronjob_obj->unitfrequency = $obj->unitfrequency;
744 $cronjob_obj->frequency = $obj->frequency;
745 $cronjob_obj->status = $obj->status;
746 $cronjob_obj->processing = $obj->processing;
747 $cronjob_obj->pid = $obj->pid;
748 $cronjob_obj->email_alert = $obj->email_alert;
749 $cronjob_obj->fk_user_author = $obj->fk_user_author;
750 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
751 $cronjob_obj->note_private = $obj->note_private;
752 $cronjob_obj->nbrun = $obj->nbrun;
753 $cronjob_obj->maxrun = $obj->maxrun;
754 $cronjob_obj->libname = $obj->libname;
755 $cronjob_obj->test = $obj->test;
757 $this->lines[] = $cronjob_obj;
762 $this->db->free($resql);
766 $this->error =
"Error ".$this->db->lasterror();
779 public function update($user =
null,
int $notrigger = 0)
781 global $conf, $langs;
783 $langs->load(
'cron');
788 if (isset($this->label)) {
789 $this->label = trim($this->label);
791 if (isset($this->jobtype)) {
792 $this->jobtype = trim($this->jobtype);
794 if (isset($this->command)) {
795 $this->command = trim($this->command);
797 if (isset($this->classesname)) {
798 $this->classesname = trim($this->classesname);
800 if (isset($this->objectname)) {
801 $this->objectname = trim($this->objectname);
803 if (isset($this->methodename)) {
804 $this->methodename = trim($this->methodename);
806 if (isset($this->params)) {
807 $this->params = trim($this->params);
809 if (isset($this->md5params)) {
810 $this->md5params = trim($this->md5params);
812 if (isset($this->module_name)) {
813 $this->module_name = trim($this->module_name);
815 if (isset($this->priority)) {
816 $this->priority = trim($this->priority);
818 if (isset($this->lastoutput)) {
819 $this->lastoutput = trim($this->lastoutput);
821 if (isset($this->lastresult)) {
822 $this->lastresult = trim($this->lastresult);
824 if (isset($this->unitfrequency)) {
825 $this->unitfrequency = trim($this->unitfrequency);
827 if (isset($this->frequency)) {
828 $this->frequency = (int) $this->frequency;
830 if (isset($this->
status)) {
833 if (isset($this->note_private)) {
834 $this->note_private = trim($this->note_private);
836 if (isset($this->nbrun)) {
837 $this->nbrun = (is_numeric($this->nbrun)) ? (
int) trim((
string) $this->nbrun) : 0;
839 if (isset($this->libname)) {
840 $this->libname = trim($this->libname);
842 if (isset($this->test)) {
843 $this->test = trim($this->test);
846 if (empty($this->maxrun)) {
849 if (empty($this->processing)) {
850 $this->processing = 0;
852 if (empty($this->pid)) {
855 if (empty($this->email_alert)) {
856 $this->email_alert =
'';
858 if (empty($this->datenextrun)) {
859 $this->datenextrun =
dol_now();
864 if (
dol_strlen($this->datenextrun) == 0 && $this->
status == self::STATUS_ENABLED) {
865 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
868 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
869 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
872 if (empty($this->label)) {
873 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
876 if (empty($this->unitfrequency)) {
877 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
880 if (($this->jobtype ==
'command') && (empty($this->command))) {
881 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
884 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
885 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
888 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
889 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
892 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
893 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
897 if (($this->jobtype ==
'function') && (empty($this->libname))) {
898 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
904 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
905 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).
",";
906 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
907 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
908 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
909 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
910 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
911 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
912 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
913 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
914 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
915 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
916 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
917 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
918 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
919 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
920 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
921 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
922 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
923 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ?
"'".$this->db->escape($this->unitfrequency).
"'" :
"null").
",";
924 $sql .=
" frequency=".(isset($this->frequency) ? ((int) $this->frequency) :
"null").
",";
925 $sql .=
" status=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
926 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
927 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
928 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
929 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
930 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
931 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
932 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
933 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
934 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
935 $sql .=
" WHERE rowid=".((int) $this->
id);
939 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
940 $resql = $this->db->query($sql);
943 $this->errors[] =
"Error ".$this->db->lasterror();
948 foreach ($this->errors as $errmsg) {
949 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
950 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
952 $this->db->rollback();
1270 global $langs, $conf, $hookmanager;
1272 $hookmanager->initHooks(array(
'cron'));
1277 $langs->load(
'cron');
1279 if (empty($userlogin)) {
1280 $this->error =
"User login is mandatory";
1281 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1287 if ($conf->entity != $this->entity && $this->entity > 0) {
1288 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1290 $savcurrententity = $conf->entity;
1291 $conf->setEntityValues($this->db, $this->entity);
1292 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1294 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1295 $user =
new User($this->db);
1296 $result = $user->fetch(0, $userlogin);
1298 $this->error =
"User Error:".$user->error;
1299 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1300 $conf->setEntityValues($this->db, $savcurrententity);
1303 if (empty($user->id)) {
1304 $this->error =
"User login: ".$userlogin.
" does not exist";
1305 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1306 $conf->setEntityValues($this->db, $savcurrententity);
1311 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1315 if (!empty($ExecTimeLimit)) {
1316 $err = error_reporting();
1319 @set_time_limit($ExecTimeLimit);
1320 error_reporting($err);
1323 if (!empty($MemoryLimit)) {
1324 @ini_set(
'memory_limit', $MemoryLimit);
1328 $this->datelastrun = $now;
1329 $this->datelastresult =
null;
1330 $this->lastoutput =
'';
1331 $this->lastresult =
'';
1332 $this->processing = 1;
1333 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1335 $result = $this->
update($user);
1337 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1338 $conf->setEntityValues($this->db, $savcurrententity);
1343 if ($this->jobtype ==
'method') {
1345 if (!empty($this->entity) && !empty($this->module_name) && !
isModEnabled(strtolower($this->module_name))) {
1346 $this->error = $langs->transnoentitiesnoconv(
'CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1347 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1348 $this->lastoutput = $this->error;
1349 $this->lastresult =
'-1';
1356 if ($ret ===
false || (!class_exists($this->objectname))) {
1357 if ($ret ===
false) {
1358 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1360 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1362 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1363 $this->lastoutput = $this->error;
1364 $this->lastresult =
'-1';
1371 if (!method_exists($this->objectname, $this->methodename)) {
1372 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1373 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1374 $this->lastoutput = $this->error;
1375 $this->lastresult =
'-1';
1378 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1379 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1380 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1381 $this->lastoutput = $this->error;
1382 $this->lastresult =
'-1';
1389 $result = $langs->load($this->module_name);
1390 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1393 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1394 $this->error = $langs->error;
1395 $this->lastoutput = $this->error;
1396 $this->lastresult =
'-1';
1402 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);
1405 $nameofclass = (string) $this->objectname;
1406 $object =
new $nameofclass($this->db);
1407 if ($this->entity > 0) {
1408 $object->entity = $this->entity;
1411 $params_arr = array();
1412 if (!empty($this->params) || $this->params ===
'0') {
1413 $params_arr = array_map(
'trim', explode(
",", $this->params));
1416 if (!is_array($params_arr)) {
1417 $result = call_user_func(array(
$object, $this->methodename), $this->params);
1419 $result = call_user_func_array(array(
$object, $this->methodename), $params_arr);
1422 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1423 $langs->load(
"errors");
1429 $errmsg .= (($errmsg ?
', ' :
'').implode(
', ',
$object->errors));
1431 if (empty($errmsg)) {
1432 $errmsg = $langs->trans(
'ErrorUnknown');
1435 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1437 $this->error = $errmsg;
1438 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1439 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1442 dol_syslog(get_class($this).
"::run_jobs END");
1443 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1444 $this->lastresult = var_export($result,
true);
1449 if ($this->jobtype ==
'function') {
1451 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1453 if ($ret ===
false) {
1454 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1455 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1456 $conf->setEntityValues($this->db, $savcurrententity);
1461 $result = $langs->load($this->module_name);
1462 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1464 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1465 $conf->setEntityValues($this->db, $savcurrententity);
1469 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1470 $params_arr = explode(
", ", $this->params);
1471 if (!is_array($params_arr)) {
1472 $result = call_user_func($this->methodename, $this->params);
1474 $result = call_user_func_array($this->methodename, $params_arr);
1477 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1478 $langs->load(
"errors");
1479 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1480 $this->error = $langs->trans(
'ErrorUnknown');
1481 $this->lastoutput = $this->error;
1482 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1485 $this->lastoutput = var_export($result,
true);
1486 $this->lastresult = var_export($result,
true);
1491 if ($this->jobtype ==
'command') {
1492 global $dolibarr_cron_allow_cli;
1494 if (empty($dolibarr_cron_allow_cli)) {
1495 $langs->load(
"errors");
1496 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1497 $this->lastoutput =
'';
1498 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1500 $outputdir = $conf->cron->dir_temp;
1501 if (empty($outputdir)) {
1502 $outputdir = $conf->cronjob->dir_temp;
1505 if (!empty($outputdir)) {
1507 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1510 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1511 $utils =
new Utils($this->db);
1512 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1514 $this->error = $arrayresult[
'error'];
1515 $this->lastoutput = $arrayresult[
'output'];
1516 $this->lastresult = (string) $arrayresult[
'result'];
1521 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1523 $this->datelastresult =
dol_now();
1524 $this->processing = 0;
1526 $result = $this->
update($user);
1528 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1529 $conf->setEntityValues($this->db, $savcurrententity);
1533 $conf->setEntityValues($this->db, $savcurrententity);
1535 if ($error && !empty($this->email_alert)) {
1536 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1537 $subject = $langs->transnoentitiesnoconv(
"ErrorInBatch", $this->label);
1538 $msg = $langs->transnoentitiesnoconv(
"ErrorInBatch", $this->label);
1540 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1541 $result = $cmailfile->sendfile();
1544 return $error ? -1 : 1;
1559 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1561 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1562 $user =
new User($this->db);
1563 $result = $user->fetch(0, $userlogin);
1565 $this->error =
"User Error : ".$user->error;
1566 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1569 if (empty($user->id)) {
1570 $this->error =
" User user login:".$userlogin.
" do not exists";
1571 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1576 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1578 if (empty($this->datenextrun)) {
1579 if (empty($this->datestart)) {
1580 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1583 $this->datenextrun = $now + ($this->frequency * (int) $this->unitfrequency);
1586 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1589 $this->datenextrun = $this->datestart + ($this->frequency * (int) $this->unitfrequency);
1594 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1596 while ($this->datenextrun < $now) {
1597 if (!is_numeric($this->unitfrequency) || (
int) $this->unitfrequency == 2678400 || (
int) $this->unitfrequency <= 0) {
1600 $this->datenextrun += ($this->frequency * (int) $this->unitfrequency);
1604 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1609 if ($this->autodelete == 2) {
1610 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1611 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1612 $this->
status = self::STATUS_ARCHIVED;
1613 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1617 $result = $this->
update($user);
1619 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);