237 global
$conf, $langs;
243 if (isset($this->label)) {
244 $this->label = trim($this->label);
246 if (isset($this->jobtype)) {
247 $this->jobtype = trim($this->jobtype);
249 if (isset($this->command)) {
250 $this->command = trim($this->command);
252 if (isset($this->classesname)) {
253 $this->classesname = trim($this->classesname);
255 if (isset($this->objectname)) {
256 $this->objectname = trim($this->objectname);
258 if (isset($this->methodename)) {
259 $this->methodename = trim($this->methodename);
261 if (isset($this->params)) {
262 $this->params = trim($this->params);
264 if (isset($this->md5params)) {
265 $this->md5params = trim($this->md5params);
267 if (isset($this->module_name)) {
268 $this->module_name = trim($this->module_name);
270 if (isset($this->lastoutput)) {
271 $this->lastoutput = trim($this->lastoutput);
273 if (isset($this->lastresult)) {
274 $this->lastresult = trim($this->lastresult);
276 if (isset($this->unitfrequency)) {
277 $this->unitfrequency = trim($this->unitfrequency);
279 if (isset($this->frequency)) {
280 $this->frequency = (int) $this->frequency;
282 if (isset($this->
status)) {
285 if (isset($this->note_private)) {
286 $this->note_private = trim($this->note_private);
288 if (isset($this->nbrun)) {
289 $this->nbrun = (int) $this->nbrun;
291 if (isset($this->maxrun)) {
292 $this->maxrun = (int) $this->maxrun;
294 if (isset($this->libname)) {
295 $this->libname = trim($this->libname);
297 if (isset($this->test)) {
298 $this->test = trim($this->test);
304 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
307 if (empty($this->label)) {
308 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
311 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
312 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
315 if (empty($this->unitfrequency)) {
316 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
319 if (($this->jobtype ==
'command') && (empty($this->command))) {
320 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
323 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
324 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
327 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
328 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
331 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
332 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
335 if (($this->jobtype ==
'function') && (empty($this->libname))) {
336 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
341 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
347 $sql .=
"classesname,";
348 $sql .=
"objectname,";
349 $sql .=
"methodename,";
351 $sql .=
"md5params,";
352 $sql .=
"module_name,";
354 $sql .=
"datelastrun,";
355 $sql .=
"datenextrun,";
357 $sql .=
"datestart,";
358 $sql .=
"lastresult,";
359 $sql .=
"datelastresult,";
360 $sql .=
"lastoutput,";
361 $sql .=
"unitfrequency,";
362 $sql .=
"frequency,";
364 $sql .=
"fk_user_author,";
365 $sql .=
"fk_user_mod,";
371 $sql .=
") VALUES (";
372 $sql .=
" ".(!isset($this->entity) ? (int)
$conf->entity : (int) $this->entity).
",";
373 $sql .=
" '".$this->db->idate($now).
"',";
374 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
375 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
376 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
377 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
378 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
379 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
380 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
381 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
382 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
383 $sql .=
" ".(is_numeric($this->priority) ? (int) $this->priority : 50).
",";
384 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
385 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
386 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
387 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
388 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
389 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
390 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
391 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
392 $sql .=
" ".(!isset($this->frequency) ?
'0' : ((int) $this->frequency)).
",";
393 $sql .=
" ".(!isset($this->
status) ?
'0' : ((int) $this->
status)).
",";
394 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
395 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
396 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
397 $sql .=
" ".(!isset($this->nbrun) ?
'0' : ((int) $this->nbrun)).
",";
398 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $this->maxrun)).
",";
399 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
400 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'");
405 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
406 $resql = $this->db->query($sql);
409 $this->errors[] =
"Error ".$this->db->lasterror();
413 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
418 $this->db->rollback();
435 public function fetch(
int $id,
string $objectname =
'',
string $methodname =
'')
439 $sql .=
" t.entity,";
442 $sql .=
" t.jobtype,";
444 $sql .=
" t.command,";
445 $sql .=
" t.classesname,";
446 $sql .=
" t.objectname,";
447 $sql .=
" t.methodename,";
448 $sql .=
" t.params,";
449 $sql .=
" t.md5params,";
450 $sql .=
" t.module_name,";
451 $sql .=
" t.priority,";
452 $sql .=
" t.datelastrun,";
453 $sql .=
" t.datenextrun,";
454 $sql .=
" t.dateend,";
455 $sql .=
" t.datestart,";
456 $sql .=
" t.lastresult,";
457 $sql .=
" t.datelastresult,";
458 $sql .=
" t.lastoutput,";
459 $sql .=
" t.unitfrequency,";
460 $sql .=
" t.frequency,";
461 $sql .=
" t.status,";
462 $sql .=
" t.processing,";
464 $sql .=
" t.email_alert,";
465 $sql .=
" t.fk_user_author,";
466 $sql .=
" t.fk_user_mod,";
467 $sql .=
" t.note as note_private,";
469 $sql .=
" t.maxrun,";
470 $sql .=
" t.libname,";
472 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
474 $sql .=
" WHERE t.rowid = ".((int)
$id);
476 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
477 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
478 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
481 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
482 $resql = $this->db->query($sql);
484 if ($this->db->num_rows($resql)) {
485 $obj = $this->db->fetch_object($resql);
487 $this->
id = $obj->rowid;
488 $this->
ref = $obj->rowid;
489 $this->entity = $obj->entity;
490 $this->tms = $this->db->jdate($obj->tms);
491 $this->datec = $this->db->jdate($obj->datec);
492 $this->label = $obj->label;
493 $this->jobtype = $obj->jobtype;
494 $this->command = $obj->command;
495 $this->classesname = $obj->classesname;
496 $this->objectname = $obj->objectname;
497 $this->methodename = $obj->methodename;
498 $this->params = $obj->params;
499 $this->md5params = $obj->md5params;
500 $this->module_name = $obj->module_name;
501 $this->priority = $obj->priority;
502 $this->datelastrun = $this->db->jdate($obj->datelastrun);
503 $this->datenextrun = $this->db->jdate($obj->datenextrun);
504 $this->dateend = $this->db->jdate($obj->dateend);
505 $this->datestart = $this->db->jdate($obj->datestart);
506 $this->lastresult = (string) $obj->lastresult;
507 $this->lastoutput = $obj->lastoutput;
508 $this->datelastresult = $this->db->jdate($obj->datelastresult);
509 $this->unitfrequency = $obj->unitfrequency;
510 $this->frequency = $obj->frequency;
511 $this->
status = $obj->status;
512 $this->processing = $obj->processing;
513 $this->pid = $obj->pid;
514 $this->email_alert = $obj->email_alert;
515 $this->fk_user_author = $obj->fk_user_author;
516 $this->fk_user_mod = $obj->fk_user_mod;
517 $this->note_private = $obj->note_private;
518 $this->nbrun = $obj->nbrun;
519 $this->maxrun = $obj->maxrun;
520 $this->libname = $obj->libname;
521 $this->test = $obj->test;
523 $this->db->free($resql);
527 $this->error =
"Error ".$this->db->lasterror();
544 public function fetchAll(
string $sortorder =
'DESC',
string $sortfield =
't.rowid',
int $limit = 0,
int $offset = 0,
int $status = 1, $filter =
'',
int $processing = -1)
546 $this->lines = array();
550 $sql .=
" t.entity,";
553 $sql .=
" t.jobtype,";
555 $sql .=
" t.command,";
556 $sql .=
" t.classesname,";
557 $sql .=
" t.objectname,";
558 $sql .=
" t.methodename,";
559 $sql .=
" t.params,";
560 $sql .=
" t.md5params,";
561 $sql .=
" t.module_name,";
562 $sql .=
" t.priority,";
563 $sql .=
" t.datelastrun,";
564 $sql .=
" t.datenextrun,";
565 $sql .=
" t.dateend,";
566 $sql .=
" t.datestart,";
567 $sql .=
" t.lastresult,";
568 $sql .=
" t.datelastresult,";
569 $sql .=
" t.lastoutput,";
570 $sql .=
" t.unitfrequency,";
571 $sql .=
" t.frequency,";
572 $sql .=
" t.status,";
573 $sql .=
" t.processing,";
575 $sql .=
" t.email_alert,";
576 $sql .=
" t.fk_user_author,";
577 $sql .=
" t.fk_user_mod,";
578 $sql .=
" t.note as note_private,";
580 $sql .=
" t.maxrun,";
581 $sql .=
" t.libname,";
583 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
584 $sql .=
" WHERE 1 = 1";
585 if ($processing >= 0) {
586 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
588 if ($status >= 0 && $status < 2) {
589 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
590 } elseif ($status == 2) {
591 $sql .=
" AND t.status = 2";
595 if (is_array($filter)) {
596 if (count($filter) > 0) {
597 foreach ($filter as $key => $value) {
598 if ($key ==
't.rowid') {
599 $sql .=
" AND ".$this->db->sanitize($key).
" = ".((int) $value);
601 $sql .=
" AND ".$this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
613 $this->errors[] = $errormessage;
614 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
618 $sql .= $this->db->order($sortfield, $sortorder);
619 if (!empty($limit) && !empty($offset)) {
620 $sql .= $this->db->plimit($limit + 1, $offset);
623 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
624 $resql = $this->db->query($sql);
626 $num = $this->db->num_rows($resql);
631 $obj = $this->db->fetch_object($resql);
632 $cronjob_obj =
new Cronjob($this->db);
634 $cronjob_obj->id = $obj->rowid;
635 $cronjob_obj->ref = $obj->rowid;
636 $cronjob_obj->entity = $obj->entity;
637 $cronjob_obj->tms = $this->db->jdate($obj->tms);
638 $cronjob_obj->datec = $this->db->jdate($obj->datec);
639 $cronjob_obj->label = $obj->label;
640 $cronjob_obj->jobtype = $obj->jobtype;
641 $cronjob_obj->command = $obj->command;
642 $cronjob_obj->classesname = $obj->classesname;
643 $cronjob_obj->objectname = $obj->objectname;
644 $cronjob_obj->methodename = $obj->methodename;
645 $cronjob_obj->params = $obj->params;
646 $cronjob_obj->md5params = $obj->md5params;
647 $cronjob_obj->module_name = $obj->module_name;
648 $cronjob_obj->priority = $obj->priority;
649 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
650 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
651 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
652 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
653 $cronjob_obj->lastresult = $obj->lastresult;
654 $cronjob_obj->lastoutput = $obj->lastoutput;
655 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
656 $cronjob_obj->unitfrequency = $obj->unitfrequency;
657 $cronjob_obj->frequency = $obj->frequency;
658 $cronjob_obj->status = $obj->status;
659 $cronjob_obj->processing = $obj->processing;
660 $cronjob_obj->pid = $obj->pid;
661 $cronjob_obj->email_alert = $obj->email_alert;
662 $cronjob_obj->fk_user_author = $obj->fk_user_author;
663 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
664 $cronjob_obj->note_private = $obj->note_private;
665 $cronjob_obj->nbrun = $obj->nbrun;
666 $cronjob_obj->maxrun = $obj->maxrun;
667 $cronjob_obj->libname = $obj->libname;
668 $cronjob_obj->test = $obj->test;
670 $this->lines[] = $cronjob_obj;
675 $this->db->free($resql);
679 $this->error =
"Error ".$this->db->lasterror();
692 public function update($user =
null,
int $notrigger = 0)
694 global
$conf, $langs;
696 $langs->load(
'cron');
701 if (isset($this->label)) {
702 $this->label = trim($this->label);
704 if (isset($this->jobtype)) {
705 $this->jobtype = trim($this->jobtype);
707 if (isset($this->command)) {
708 $this->command = trim($this->command);
710 if (isset($this->classesname)) {
711 $this->classesname = trim($this->classesname);
713 if (isset($this->objectname)) {
714 $this->objectname = trim($this->objectname);
716 if (isset($this->methodename)) {
717 $this->methodename = trim($this->methodename);
719 if (isset($this->params)) {
720 $this->params = trim($this->params);
722 if (isset($this->md5params)) {
723 $this->md5params = trim($this->md5params);
725 if (isset($this->module_name)) {
726 $this->module_name = trim($this->module_name);
728 if (isset($this->priority)) {
729 $this->priority = trim($this->priority);
731 if (isset($this->lastoutput)) {
732 $this->lastoutput = trim($this->lastoutput);
734 if (isset($this->lastresult)) {
735 $this->lastresult = trim($this->lastresult);
737 if (isset($this->unitfrequency)) {
738 $this->unitfrequency = trim($this->unitfrequency);
740 if (isset($this->frequency)) {
741 $this->frequency = (int) $this->frequency;
743 if (isset($this->
status)) {
746 if (isset($this->note_private)) {
747 $this->note_private = trim($this->note_private);
749 if (isset($this->nbrun)) {
750 $this->nbrun = (is_numeric($this->nbrun)) ? (
int) trim((
string) $this->nbrun) : 0;
752 if (isset($this->libname)) {
753 $this->libname = trim($this->libname);
755 if (isset($this->test)) {
756 $this->test = trim($this->test);
759 if (empty($this->maxrun)) {
762 if (empty($this->processing)) {
763 $this->processing = 0;
765 if (empty($this->pid)) {
768 if (empty($this->email_alert)) {
769 $this->email_alert =
'';
771 if (empty($this->datenextrun)) {
772 $this->datenextrun =
dol_now();
777 if (
dol_strlen($this->datenextrun) == 0 && $this->
status == self::STATUS_ENABLED) {
778 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
781 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
782 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
785 if (empty($this->label)) {
786 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
789 if (empty($this->unitfrequency)) {
790 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
793 if (($this->jobtype ==
'command') && (empty($this->command))) {
794 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
797 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
798 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
801 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
802 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
805 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
806 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
810 if (($this->jobtype ==
'function') && (empty($this->libname))) {
811 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
817 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
818 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) :
$conf->entity).
",";
819 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
820 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
821 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
822 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
823 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
824 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
825 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
826 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
827 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
828 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
829 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
830 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
831 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
832 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
833 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
834 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
835 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
836 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ?
"'".$this->db->escape($this->unitfrequency).
"'" :
"null").
",";
837 $sql .=
" frequency=".(isset($this->frequency) ? ((int) $this->frequency) :
"null").
",";
838 $sql .=
" status=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
839 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
840 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
841 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
842 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
843 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
844 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
845 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
846 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
847 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
848 $sql .=
" WHERE rowid=".((int) $this->
id);
852 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
853 $resql = $this->db->query($sql);
856 $this->errors[] =
"Error ".$this->db->lasterror();
861 foreach ($this->errors as $errmsg) {
862 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
863 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
865 $this->db->rollback();
1182 global $langs,
$conf, $hookmanager;
1184 $hookmanager->initHooks(array(
'cron'));
1189 $langs->load(
'cron');
1191 if (empty($userlogin)) {
1192 $this->error =
"User login is mandatory";
1193 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1199 if (
$conf->entity != $this->entity && $this->entity > 0) {
1200 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".
$conf->entity, LOG_WARNING);
1202 $savcurrententity =
$conf->entity;
1203 $conf->setEntityValues($this->db, $this->entity);
1204 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".
$conf->entity);
1206 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1207 $user =
new User($this->db);
1208 $result = $user->fetch(0, $userlogin);
1210 $this->error =
"User Error:".$user->error;
1211 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1212 $conf->setEntityValues($this->db, $savcurrententity);
1215 if (empty($user->id)) {
1216 $this->error =
"User login: ".$userlogin.
" does not exist";
1217 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1218 $conf->setEntityValues($this->db, $savcurrententity);
1223 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1226 $ExecTimeLimit = 600;
1227 if (!empty($ExecTimeLimit)) {
1228 $err = error_reporting();
1231 @set_time_limit($ExecTimeLimit);
1232 error_reporting($err);
1235 if (!empty($MemoryLimit)) {
1236 @ini_set(
'memory_limit', $MemoryLimit);
1240 $this->datelastrun = $now;
1241 $this->datelastresult =
null;
1242 $this->lastoutput =
'';
1243 $this->lastresult =
'';
1244 $this->processing = 1;
1245 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1247 $result = $this->
update($user);
1249 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1250 $conf->setEntityValues($this->db, $savcurrententity);
1255 if ($this->jobtype ==
'method') {
1257 if (!empty($this->entity) && !empty($this->module_name) && !isModEnabled(strtolower($this->module_name))) {
1258 $this->error = $langs->transnoentitiesnoconv(
'CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1259 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1260 $this->lastoutput = $this->error;
1261 $this->lastresult =
'-1';
1268 if ($ret ===
false || (!class_exists($this->objectname))) {
1269 if ($ret ===
false) {
1270 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1272 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1274 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1275 $this->lastoutput = $this->error;
1276 $this->lastresult =
'-1';
1283 if (!method_exists($this->objectname, $this->methodename)) {
1284 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1285 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1286 $this->lastoutput = $this->error;
1287 $this->lastresult =
'-1';
1290 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1291 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1292 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1293 $this->lastoutput = $this->error;
1294 $this->lastresult =
'-1';
1301 $result = $langs->load($this->module_name);
1302 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1305 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1306 $this->error = $langs->error;
1307 $this->lastoutput = $this->error;
1308 $this->lastresult =
'-1';
1314 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);
1317 $nameofclass = (string) $this->objectname;
1318 $object =
new $nameofclass($this->db);
1319 if ($this->entity > 0) {
1320 $object->entity = $this->entity;
1323 $params_arr = array();
1324 if (!empty($this->params) || $this->params ===
'0') {
1325 $params_arr = array_map(
'trim', explode(
",", $this->params));
1328 if (!is_array($params_arr)) {
1329 $result = call_user_func(array(
$object, $this->methodename), $this->params);
1331 $result = call_user_func_array(array(
$object, $this->methodename), $params_arr);
1334 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1335 $langs->load(
"errors");
1341 $errmsg .= (($errmsg ?
', ' :
'').implode(
', ',
$object->errors));
1343 if (empty($errmsg)) {
1344 $errmsg = $langs->trans(
'ErrorUnknown');
1347 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1349 $this->error = $errmsg;
1350 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1351 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1354 dol_syslog(get_class($this).
"::run_jobs END");
1355 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1356 $this->lastresult = var_export($result,
true);
1361 if ($this->jobtype ==
'function') {
1363 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1365 if ($ret ===
false) {
1366 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1367 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1368 $conf->setEntityValues($this->db, $savcurrententity);
1373 $result = $langs->load($this->module_name);
1374 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1376 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1377 $conf->setEntityValues($this->db, $savcurrententity);
1381 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1382 $params_arr = explode(
", ", $this->params);
1383 if (!is_array($params_arr)) {
1384 $result = call_user_func($this->methodename, $this->params);
1386 $result = call_user_func_array($this->methodename, $params_arr);
1389 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1390 $langs->load(
"errors");
1391 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1392 $this->error = $langs->trans(
'ErrorUnknown');
1393 $this->lastoutput = $this->error;
1394 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1397 $this->lastoutput = var_export($result,
true);
1398 $this->lastresult = var_export($result,
true);
1403 if ($this->jobtype ==
'command') {
1404 global $dolibarr_cron_allow_cli;
1406 if (empty($dolibarr_cron_allow_cli)) {
1407 $langs->load(
"errors");
1408 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1409 $this->lastoutput =
'';
1410 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1412 $outputdir =
$conf->cron->dir_temp;
1413 if (empty($outputdir)) {
1414 $outputdir =
$conf->cronjob->dir_temp;
1417 if (!empty($outputdir)) {
1419 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1422 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1423 $utils =
new Utils($this->db);
1424 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1426 $this->error = $arrayresult[
'error'];
1427 $this->lastoutput = $arrayresult[
'output'];
1428 $this->lastresult = (string) $arrayresult[
'result'];
1433 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1435 $this->datelastresult =
dol_now();
1436 $this->processing = 0;
1438 $result = $this->
update($user);
1440 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1441 $conf->setEntityValues($this->db, $savcurrententity);
1445 $conf->setEntityValues($this->db, $savcurrententity);
1447 if ($error && !empty($this->email_alert)) {
1448 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1449 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1450 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1452 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1453 $result = $cmailfile->sendfile();
1456 return $error ? -1 : 1;
1471 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1473 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1474 $user =
new User($this->db);
1475 $result = $user->fetch(0, $userlogin);
1477 $this->error =
"User Error : ".$user->error;
1478 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1481 if (empty($user->id)) {
1482 $this->error =
" User user login:".$userlogin.
" do not exists";
1483 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1488 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1490 if (empty($this->datenextrun)) {
1491 if (empty($this->datestart)) {
1492 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1495 $this->datenextrun = $now + ($this->frequency * (int) $this->unitfrequency);
1498 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1501 $this->datenextrun = $this->datestart + ($this->frequency * (int) $this->unitfrequency);
1506 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1508 while ($this->datenextrun < $now) {
1509 if (!is_numeric($this->unitfrequency) || (
int) $this->unitfrequency == 2678400 || (
int) $this->unitfrequency <= 0) {
1512 $this->datenextrun += ($this->frequency * (int) $this->unitfrequency);
1516 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1521 if ($this->autodelete == 2) {
1522 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1523 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1524 $this->
status = self::STATUS_ARCHIVED;
1525 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1529 $result = $this->
update($user);
1531 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);