216 global $conf, $langs;
222 if (isset($this->label)) {
223 $this->label = trim($this->label);
225 if (isset($this->jobtype)) {
226 $this->jobtype = trim($this->jobtype);
228 if (isset($this->command)) {
229 $this->command = trim($this->command);
231 if (isset($this->classesname)) {
232 $this->classesname = trim($this->classesname);
234 if (isset($this->objectname)) {
235 $this->objectname = trim($this->objectname);
237 if (isset($this->methodename)) {
238 $this->methodename = trim($this->methodename);
240 if (isset($this->params)) {
241 $this->params = trim($this->params);
243 if (isset($this->md5params)) {
244 $this->md5params = trim($this->md5params);
246 if (isset($this->module_name)) {
247 $this->module_name = trim($this->module_name);
249 if (isset($this->priority)) {
250 $this->priority = trim($this->priority);
252 if (isset($this->lastoutput)) {
253 $this->lastoutput = trim($this->lastoutput);
255 if (isset($this->lastresult)) {
256 $this->lastresult = trim($this->lastresult);
258 if (isset($this->unitfrequency)) {
259 $this->unitfrequency = trim($this->unitfrequency);
261 if (isset($this->frequency)) {
262 $this->frequency = (int) $this->frequency;
264 if (isset($this->
status)) {
267 if (isset($this->note_private)) {
268 $this->note_private = trim($this->note_private);
270 if (isset($this->nbrun)) {
271 $this->nbrun = (int) $this->nbrun;
273 if (isset($this->maxrun)) {
274 $this->maxrun = (int) $this->maxrun;
276 if (isset($this->libname)) {
277 $this->libname = trim($this->libname);
279 if (isset($this->test)) {
280 $this->test = trim($this->test);
286 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
289 if (empty($this->label)) {
290 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
293 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
294 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
297 if (empty($this->unitfrequency)) {
298 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
301 if (($this->jobtype ==
'command') && (empty($this->command))) {
302 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
305 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
306 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
309 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
310 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
313 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
314 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
317 if (($this->jobtype ==
'function') && (empty($this->libname))) {
318 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
323 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
329 $sql .=
"classesname,";
330 $sql .=
"objectname,";
331 $sql .=
"methodename,";
333 $sql .=
"md5params,";
334 $sql .=
"module_name,";
336 $sql .=
"datelastrun,";
337 $sql .=
"datenextrun,";
339 $sql .=
"datestart,";
340 $sql .=
"lastresult,";
341 $sql .=
"datelastresult,";
342 $sql .=
"lastoutput,";
343 $sql .=
"unitfrequency,";
344 $sql .=
"frequency,";
346 $sql .=
"fk_user_author,";
347 $sql .=
"fk_user_mod,";
353 $sql .=
") VALUES (";
354 $sql .=
" ".(!isset($this->entity) ? $conf->entity : $this->db->escape($this->entity)).
",";
355 $sql .=
" '".$this->db->idate($now).
"',";
356 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
357 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
358 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
359 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
360 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
361 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
362 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
363 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
364 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
365 $sql .=
" ".(!isset($this->priority) ?
'0' : $this->priority).
",";
366 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
367 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
368 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
369 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
370 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
371 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
372 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
373 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
374 $sql .=
" ".(!isset($this->frequency) ?
'0' : ((int) $this->frequency)).
",";
375 $sql .=
" ".(!isset($this->
status) ?
'0' : ((int) $this->
status)).
",";
376 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
377 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
378 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
379 $sql .=
" ".(!isset($this->nbrun) ?
'0' : ((int) $this->nbrun)).
",";
380 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $this->maxrun)).
",";
381 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
382 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'");
387 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
388 $resql = $this->db->query($sql);
391 $this->errors[] =
"Error ".$this->db->lasterror();
395 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
400 $this->db->rollback();
417 public function fetch(
int $id,
string $objectname =
'',
string $methodname =
'')
421 $sql .=
" t.entity,";
424 $sql .=
" t.jobtype,";
426 $sql .=
" t.command,";
427 $sql .=
" t.classesname,";
428 $sql .=
" t.objectname,";
429 $sql .=
" t.methodename,";
430 $sql .=
" t.params,";
431 $sql .=
" t.md5params,";
432 $sql .=
" t.module_name,";
433 $sql .=
" t.priority,";
434 $sql .=
" t.datelastrun,";
435 $sql .=
" t.datenextrun,";
436 $sql .=
" t.dateend,";
437 $sql .=
" t.datestart,";
438 $sql .=
" t.lastresult,";
439 $sql .=
" t.datelastresult,";
440 $sql .=
" t.lastoutput,";
441 $sql .=
" t.unitfrequency,";
442 $sql .=
" t.frequency,";
443 $sql .=
" t.status,";
444 $sql .=
" t.processing,";
446 $sql .=
" t.email_alert,";
447 $sql .=
" t.fk_user_author,";
448 $sql .=
" t.fk_user_mod,";
449 $sql .=
" t.note as note_private,";
451 $sql .=
" t.maxrun,";
452 $sql .=
" t.libname,";
454 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
456 $sql .=
" WHERE t.rowid = ".((int) $id);
458 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
459 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
460 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
463 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
464 $resql = $this->db->query($sql);
466 if ($this->db->num_rows($resql)) {
467 $obj = $this->db->fetch_object($resql);
469 $this->
id = $obj->rowid;
470 $this->
ref = $obj->rowid;
471 $this->entity = $obj->entity;
472 $this->tms = $this->db->jdate($obj->tms);
473 $this->datec = $this->db->jdate($obj->datec);
474 $this->label = $obj->label;
475 $this->jobtype = $obj->jobtype;
476 $this->command = $obj->command;
477 $this->classesname = $obj->classesname;
478 $this->objectname = $obj->objectname;
479 $this->methodename = $obj->methodename;
480 $this->params = $obj->params;
481 $this->md5params = $obj->md5params;
482 $this->module_name = $obj->module_name;
483 $this->priority = $obj->priority;
484 $this->datelastrun = $this->db->jdate($obj->datelastrun);
485 $this->datenextrun = $this->db->jdate($obj->datenextrun);
486 $this->dateend = $this->db->jdate($obj->dateend);
487 $this->datestart = $this->db->jdate($obj->datestart);
488 $this->lastresult = (string) $obj->lastresult;
489 $this->lastoutput = $obj->lastoutput;
490 $this->datelastresult = $this->db->jdate($obj->datelastresult);
491 $this->unitfrequency = $obj->unitfrequency;
492 $this->frequency = $obj->frequency;
493 $this->
status = $obj->status;
494 $this->processing = $obj->processing;
495 $this->pid = $obj->pid;
496 $this->email_alert = $obj->email_alert;
497 $this->fk_user_author = $obj->fk_user_author;
498 $this->fk_user_mod = $obj->fk_user_mod;
499 $this->note_private = $obj->note_private;
500 $this->nbrun = $obj->nbrun;
501 $this->maxrun = $obj->maxrun;
502 $this->libname = $obj->libname;
503 $this->test = $obj->test;
505 $this->db->free($resql);
509 $this->error =
"Error ".$this->db->lasterror();
526 public function fetchAll(
string $sortorder =
'DESC',
string $sortfield =
't.rowid',
int $limit = 0,
int $offset = 0,
int $status = 1, $filter =
'',
int $processing = -1)
528 $this->lines = array();
532 $sql .=
" t.entity,";
535 $sql .=
" t.jobtype,";
537 $sql .=
" t.command,";
538 $sql .=
" t.classesname,";
539 $sql .=
" t.objectname,";
540 $sql .=
" t.methodename,";
541 $sql .=
" t.params,";
542 $sql .=
" t.md5params,";
543 $sql .=
" t.module_name,";
544 $sql .=
" t.priority,";
545 $sql .=
" t.datelastrun,";
546 $sql .=
" t.datenextrun,";
547 $sql .=
" t.dateend,";
548 $sql .=
" t.datestart,";
549 $sql .=
" t.lastresult,";
550 $sql .=
" t.datelastresult,";
551 $sql .=
" t.lastoutput,";
552 $sql .=
" t.unitfrequency,";
553 $sql .=
" t.frequency,";
554 $sql .=
" t.status,";
555 $sql .=
" t.processing,";
557 $sql .=
" t.email_alert,";
558 $sql .=
" t.fk_user_author,";
559 $sql .=
" t.fk_user_mod,";
560 $sql .=
" t.note as note_private,";
562 $sql .=
" t.maxrun,";
563 $sql .=
" t.libname,";
565 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
566 $sql .=
" WHERE 1 = 1";
567 if ($processing >= 0) {
568 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
570 if ($status >= 0 && $status < 2) {
571 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
572 } elseif ($status == 2) {
573 $sql .=
" AND t.status = 2";
577 if (is_array($filter)) {
578 if (count($filter) > 0) {
579 foreach ($filter as $key => $value) {
580 if ($key ==
't.rowid') {
581 $sql .=
" AND ".$this->db->sanitize($key).
" = ".((int) $value);
583 $sql .=
" AND ".$this->db->sanitize($key).
" LIKE '%".$this->db->escape($this->db->escapeforlike($value)).
"%'";
595 $this->errors[] = $errormessage;
596 dol_syslog(__METHOD__.
' '.implode(
',', $this->errors), LOG_ERR);
600 $sql .= $this->db->order($sortfield, $sortorder);
601 if (!empty($limit) && !empty($offset)) {
602 $sql .= $this->db->plimit($limit + 1, $offset);
605 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
606 $resql = $this->db->query($sql);
608 $num = $this->db->num_rows($resql);
613 $obj = $this->db->fetch_object($resql);
614 $cronjob_obj =
new Cronjob($this->db);
616 $cronjob_obj->id = $obj->rowid;
617 $cronjob_obj->ref = $obj->rowid;
618 $cronjob_obj->entity = $obj->entity;
619 $cronjob_obj->tms = $this->db->jdate($obj->tms);
620 $cronjob_obj->datec = $this->db->jdate($obj->datec);
621 $cronjob_obj->label = $obj->label;
622 $cronjob_obj->jobtype = $obj->jobtype;
623 $cronjob_obj->command = $obj->command;
624 $cronjob_obj->classesname = $obj->classesname;
625 $cronjob_obj->objectname = $obj->objectname;
626 $cronjob_obj->methodename = $obj->methodename;
627 $cronjob_obj->params = $obj->params;
628 $cronjob_obj->md5params = $obj->md5params;
629 $cronjob_obj->module_name = $obj->module_name;
630 $cronjob_obj->priority = $obj->priority;
631 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
632 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
633 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
634 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
635 $cronjob_obj->lastresult = $obj->lastresult;
636 $cronjob_obj->lastoutput = $obj->lastoutput;
637 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
638 $cronjob_obj->unitfrequency = $obj->unitfrequency;
639 $cronjob_obj->frequency = $obj->frequency;
640 $cronjob_obj->status = $obj->status;
641 $cronjob_obj->processing = $obj->processing;
642 $cronjob_obj->pid = $obj->pid;
643 $cronjob_obj->email_alert = $obj->email_alert;
644 $cronjob_obj->fk_user_author = $obj->fk_user_author;
645 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
646 $cronjob_obj->note_private = $obj->note_private;
647 $cronjob_obj->nbrun = $obj->nbrun;
648 $cronjob_obj->maxrun = $obj->maxrun;
649 $cronjob_obj->libname = $obj->libname;
650 $cronjob_obj->test = $obj->test;
652 $this->lines[] = $cronjob_obj;
657 $this->db->free($resql);
661 $this->error =
"Error ".$this->db->lasterror();
674 public function update(
User $user =
null,
int $notrigger = 0)
676 global $conf, $langs;
678 $langs->load(
'cron');
683 if (isset($this->label)) {
684 $this->label = trim($this->label);
686 if (isset($this->jobtype)) {
687 $this->jobtype = trim($this->jobtype);
689 if (isset($this->command)) {
690 $this->command = trim($this->command);
692 if (isset($this->classesname)) {
693 $this->classesname = trim($this->classesname);
695 if (isset($this->objectname)) {
696 $this->objectname = trim($this->objectname);
698 if (isset($this->methodename)) {
699 $this->methodename = trim($this->methodename);
701 if (isset($this->params)) {
702 $this->params = trim($this->params);
704 if (isset($this->md5params)) {
705 $this->md5params = trim($this->md5params);
707 if (isset($this->module_name)) {
708 $this->module_name = trim($this->module_name);
710 if (isset($this->priority)) {
711 $this->priority = trim($this->priority);
713 if (isset($this->lastoutput)) {
714 $this->lastoutput = trim($this->lastoutput);
716 if (isset($this->lastresult)) {
717 $this->lastresult = trim($this->lastresult);
719 if (isset($this->unitfrequency)) {
720 $this->unitfrequency = trim($this->unitfrequency);
722 if (isset($this->frequency)) {
723 $this->frequency = (int) $this->frequency;
725 if (isset($this->
status)) {
728 if (isset($this->note_private)) {
729 $this->note_private = trim($this->note_private);
731 if (isset($this->nbrun)) {
732 $this->nbrun = (is_numeric($this->nbrun)) ? (
int) trim((
string) $this->nbrun) : 0;
734 if (isset($this->libname)) {
735 $this->libname = trim($this->libname);
737 if (isset($this->test)) {
738 $this->test = trim($this->test);
741 if (empty($this->maxrun)) {
744 if (empty($this->processing)) {
745 $this->processing = 0;
747 if (empty($this->pid)) {
750 if (empty($this->email_alert)) {
751 $this->email_alert =
'';
753 if (empty($this->datenextrun)) {
754 $this->datenextrun =
dol_now();
759 if (
dol_strlen($this->datenextrun) == 0 && $this->
status == self::STATUS_ENABLED) {
760 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
763 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
764 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
767 if (empty($this->label)) {
768 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
771 if (empty($this->unitfrequency)) {
772 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
775 if (($this->jobtype ==
'command') && (empty($this->command))) {
776 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
779 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
780 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
783 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
784 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
787 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
788 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
792 if (($this->jobtype ==
'function') && (empty($this->libname))) {
793 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
799 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
800 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).
",";
801 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
802 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
803 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
804 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
805 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
806 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
807 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
808 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
809 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
810 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
811 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
812 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
813 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
814 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
815 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
816 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
817 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
818 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ?
"'".$this->db->escape($this->unitfrequency).
"'" :
"null").
",";
819 $sql .=
" frequency=".(isset($this->frequency) ? ((int) $this->frequency) :
"null").
",";
820 $sql .=
" status=".(isset($this->
status) ? ((int) $this->
status) :
"null").
",";
821 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
822 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
823 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
824 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
825 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
826 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
827 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
828 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
829 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
830 $sql .=
" WHERE rowid=".((int) $this->
id);
834 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
835 $resql = $this->db->query($sql);
838 $this->errors[] =
"Error ".$this->db->lasterror();
843 foreach ($this->errors as $errmsg) {
844 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
845 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
847 $this->db->rollback();
1165 global $langs, $conf, $hookmanager;
1167 $hookmanager->initHooks(array(
'cron'));
1172 $langs->load(
'cron');
1174 if (empty($userlogin)) {
1175 $this->error =
"User login is mandatory";
1176 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1182 if ($conf->entity != $this->entity && $this->entity > 0) {
1183 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1185 $savcurrententity = $conf->entity;
1186 $conf->setEntityValues($this->db, $this->entity);
1187 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1189 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1190 $user =
new User($this->db);
1191 $result = $user->fetch(
'', $userlogin);
1193 $this->error =
"User Error:".$user->error;
1194 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1195 $conf->setEntityValues($this->db, $savcurrententity);
1198 if (empty($user->id)) {
1199 $this->error =
"User login: ".$userlogin.
" does not exist";
1200 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1201 $conf->setEntityValues($this->db, $savcurrententity);
1206 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1209 $ExecTimeLimit = 600;
1210 if (!empty($ExecTimeLimit)) {
1211 $err = error_reporting();
1214 @set_time_limit($ExecTimeLimit);
1215 error_reporting($err);
1218 if (!empty($MemoryLimit)) {
1219 @ini_set(
'memory_limit', $MemoryLimit);
1223 $this->datelastrun = $now;
1224 $this->datelastresult =
null;
1225 $this->lastoutput =
'';
1226 $this->lastresult =
'';
1227 $this->processing = 1;
1228 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1229 $this->nbrun = $this->nbrun + 1;
1230 $result = $this->
update($user);
1232 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1233 $conf->setEntityValues($this->db, $savcurrententity);
1238 if ($this->jobtype ==
'method') {
1240 if (!empty($this->entity) && !empty($this->module_name) && !isModEnabled(strtolower($this->module_name))) {
1241 $this->error = $langs->transnoentitiesnoconv(
'CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1242 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1243 $this->lastoutput = $this->error;
1244 $this->lastresult =
'-1';
1251 if ($ret ===
false || (!class_exists($this->objectname))) {
1252 if ($ret ===
false) {
1253 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1255 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1257 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1258 $this->lastoutput = $this->error;
1259 $this->lastresult =
'-1';
1266 if (!method_exists($this->objectname, $this->methodename)) {
1267 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1268 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1269 $this->lastoutput = $this->error;
1270 $this->lastresult =
'-1';
1273 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1274 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1275 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1276 $this->lastoutput = $this->error;
1277 $this->lastresult =
'-1';
1284 $result = $langs->load($this->module_name);
1285 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1288 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1289 $this->error = $langs->error;
1290 $this->lastoutput = $this->error;
1291 $this->lastresult =
'-1';
1297 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);
1300 $nameofclass = (string) $this->objectname;
1301 $object =
new $nameofclass($this->db);
1302 if ($this->entity > 0) {
1303 $object->entity = $this->entity;
1306 $params_arr = array();
1307 if (!empty($this->params) || $this->params ===
'0') {
1308 $params_arr = array_map(
'trim', explode(
",", $this->params));
1311 if (!is_array($params_arr)) {
1312 $result = call_user_func(array(
$object, $this->methodename), $this->params);
1314 $result = call_user_func_array(array(
$object, $this->methodename), $params_arr);
1317 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1318 $langs->load(
"errors");
1324 $errmsg .= (($errmsg ?
', ' :
'').implode(
', ',
$object->errors));
1326 if (empty($errmsg)) {
1327 $errmsg = $langs->trans(
'ErrorUnknown');
1330 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1332 $this->error = $errmsg;
1333 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1334 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1337 dol_syslog(get_class($this).
"::run_jobs END");
1338 $this->lastoutput =
dol_substr((empty(
$object->output) ?
"" :
$object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1339 $this->lastresult = var_export($result,
true);
1344 if ($this->jobtype ==
'function') {
1346 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1348 if ($ret ===
false) {
1349 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1350 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1351 $conf->setEntityValues($this->db, $savcurrententity);
1356 $result = $langs->load($this->module_name);
1357 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1359 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1360 $conf->setEntityValues($this->db, $savcurrententity);
1364 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1365 $params_arr = explode(
", ", $this->params);
1366 if (!is_array($params_arr)) {
1367 $result = call_user_func($this->methodename, $this->params);
1369 $result = call_user_func_array($this->methodename, $params_arr);
1372 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1373 $langs->load(
"errors");
1374 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1375 $this->error = $langs->trans(
'ErrorUnknown');
1376 $this->lastoutput = $this->error;
1377 $this->lastresult = is_numeric($result) ? var_export($result,
true) :
'-1';
1380 $this->lastoutput = var_export($result,
true);
1381 $this->lastresult = var_export($result,
true);
1386 if ($this->jobtype ==
'command') {
1387 global $dolibarr_cron_allow_cli;
1389 if (empty($dolibarr_cron_allow_cli)) {
1390 $langs->load(
"errors");
1391 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1392 $this->lastoutput =
'';
1393 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1395 $outputdir = $conf->cron->dir_temp;
1396 if (empty($outputdir)) {
1397 $outputdir = $conf->cronjob->dir_temp;
1400 if (!empty($outputdir)) {
1402 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1405 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1406 $utils =
new Utils($this->db);
1407 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1409 $this->error = $arrayresult[
'error'];
1410 $this->lastoutput = $arrayresult[
'output'];
1411 $this->lastresult = $arrayresult[
'result'];
1416 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1418 $this->datelastresult =
dol_now();
1419 $this->processing = 0;
1421 $result = $this->
update($user);
1423 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1424 $conf->setEntityValues($this->db, $savcurrententity);
1428 $conf->setEntityValues($this->db, $savcurrententity);
1430 if ($error && !empty($this->email_alert)) {
1431 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1432 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1433 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1435 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1436 $result = $cmailfile->sendfile();
1439 return $error ? -1 : 1;
1454 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1456 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1457 $user =
new User($this->db);
1458 $result = $user->fetch(
'', $userlogin);
1460 $this->error =
"User Error : ".$user->error;
1461 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1464 if (empty($user->id)) {
1465 $this->error =
" User user login:".$userlogin.
" do not exists";
1466 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1471 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1473 if (empty($this->datenextrun)) {
1474 if (empty($this->datestart)) {
1475 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1478 $this->datenextrun = $now + ($this->frequency * (int) $this->unitfrequency);
1481 if (!is_numeric($this->frequency) || (
int) $this->unitfrequency == 2678400) {
1484 $this->datenextrun = $this->datestart + ($this->frequency * (int) $this->unitfrequency);
1489 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1491 while ($this->datenextrun < $now) {
1492 if (!is_numeric($this->unitfrequency) || (
int) $this->unitfrequency == 2678400 || (
int) $this->unitfrequency <= 0) {
1495 $this->datenextrun += ($this->frequency * (int) $this->unitfrequency);
1499 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1504 if ($this->autodelete == 2) {
1505 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1506 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1507 $this->
status = self::STATUS_ARCHIVED;
1508 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1512 $result = $this->
update($user);
1514 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);