cronjob.class.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  1. <?php
  2. /* Copyright (C) 2007-2022 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file cron/class/cronjob.class.php
  20. * \ingroup cron
  21. */
  22. // Put here all includes required by your class file
  23. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  24. require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
  25. /**
  26. * Cron Job class
  27. */
  28. class Cronjob extends CommonObject
  29. {
  30. /**
  31. * @var string ID to identify managed object
  32. */
  33. public $element = 'cronjob';
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element = 'cronjob';
  38. /**
  39. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  40. */
  41. public $picto = 'cron';
  42. /**
  43. * @var int Entity
  44. */
  45. public $entity;
  46. /**
  47. * @var string Job type
  48. */
  49. public $jobtype;
  50. /**
  51. * @var string|int Date for last cron object update
  52. */
  53. public $tms = '';
  54. /**
  55. * @var string|int Date for cron job create
  56. */
  57. public $datec = '';
  58. /**
  59. * @var string Cron Job label
  60. */
  61. public $label;
  62. /**
  63. * @var string Job command
  64. */
  65. public $command;
  66. public $classesname;
  67. public $objectname;
  68. public $methodename;
  69. public $params;
  70. public $md5params;
  71. public $module_name;
  72. public $priority;
  73. /**
  74. * @var string|int Date for last job execution
  75. */
  76. public $datelastrun = '';
  77. /**
  78. * @var string|int Date for next job execution
  79. */
  80. public $datenextrun = '';
  81. /**
  82. * @var string|int Date for end job execution
  83. */
  84. public $dateend = '';
  85. /**
  86. * @var string|int Date for first start job execution
  87. */
  88. public $datestart = '';
  89. /**
  90. * @var string|int Date for last result job execution
  91. */
  92. public $datelastresult = '';
  93. /**
  94. * @var string Last result from end job execution
  95. */
  96. public $lastresult;
  97. /**
  98. * @var string Last output from end job execution
  99. */
  100. public $lastoutput;
  101. /**
  102. * @var string Unit frequency of job execution
  103. */
  104. public $unitfrequency;
  105. /**
  106. * @var int Frequency of job execution
  107. */
  108. public $frequency;
  109. /**
  110. * @var int Status
  111. */
  112. public $status;
  113. /**
  114. * @var int Is job running ?
  115. */
  116. public $processing;
  117. /**
  118. * @var int The job current PID
  119. */
  120. public $pid;
  121. /**
  122. * @var string Email when an error occurs
  123. */
  124. public $email_alert;
  125. /**
  126. * @var int User ID of creation
  127. */
  128. public $fk_user_author;
  129. /**
  130. * @var int User ID of last modification
  131. */
  132. public $fk_user_mod;
  133. /**
  134. * @var int Number of run job execution
  135. */
  136. public $nbrun;
  137. /**
  138. * @var int Maximum run job execution
  139. */
  140. public $maxrun;
  141. /**
  142. * @var string Libname
  143. */
  144. public $libname;
  145. /**
  146. * @var string A test condition to know if job is visible/qualified
  147. */
  148. public $test;
  149. /**
  150. * @var string Autodelete
  151. */
  152. public $autodelete;
  153. const STATUS_DISABLED = 0;
  154. const STATUS_ENABLED = 1;
  155. const STATUS_ARCHIVED = 2;
  156. /**
  157. * Constructor
  158. *
  159. * @param DoliDb $db Database handler
  160. */
  161. public function __construct($db)
  162. {
  163. $this->db = $db;
  164. }
  165. /**
  166. * Create object into database
  167. *
  168. * @param User $user User that creates
  169. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  170. * @return int <0 if KO, Id of created object if OK
  171. */
  172. public function create($user, $notrigger = 0)
  173. {
  174. global $conf, $langs;
  175. $error = 0;
  176. $now = dol_now();
  177. // Clean parameters
  178. if (isset($this->label)) {
  179. $this->label = trim($this->label);
  180. }
  181. if (isset($this->jobtype)) {
  182. $this->jobtype = trim($this->jobtype);
  183. }
  184. if (isset($this->command)) {
  185. $this->command = trim($this->command);
  186. }
  187. if (isset($this->classesname)) {
  188. $this->classesname = trim($this->classesname);
  189. }
  190. if (isset($this->objectname)) {
  191. $this->objectname = trim($this->objectname);
  192. }
  193. if (isset($this->methodename)) {
  194. $this->methodename = trim($this->methodename);
  195. }
  196. if (isset($this->params)) {
  197. $this->params = trim($this->params);
  198. }
  199. if (isset($this->md5params)) {
  200. $this->md5params = trim($this->md5params);
  201. }
  202. if (isset($this->module_name)) {
  203. $this->module_name = trim($this->module_name);
  204. }
  205. if (isset($this->priority)) {
  206. $this->priority = trim($this->priority);
  207. }
  208. if (isset($this->lastoutput)) {
  209. $this->lastoutput = trim($this->lastoutput);
  210. }
  211. if (isset($this->lastresult)) {
  212. $this->lastresult = trim($this->lastresult);
  213. }
  214. if (isset($this->unitfrequency)) {
  215. $this->unitfrequency = trim($this->unitfrequency);
  216. }
  217. if (isset($this->frequency)) {
  218. $this->frequency = trim($this->frequency);
  219. }
  220. if (isset($this->status)) {
  221. $this->status = trim($this->status);
  222. }
  223. if (isset($this->note_private)) {
  224. $this->note_private = trim($this->note_private);
  225. }
  226. if (isset($this->nbrun)) {
  227. $this->nbrun = (int) $this->nbrun;
  228. }
  229. if (isset($this->maxrun)) {
  230. $this->maxrun = (int) $this->maxrun;
  231. }
  232. if (isset($this->libname)) {
  233. $this->libname = trim($this->libname);
  234. }
  235. if (isset($this->test)) {
  236. $this->test = trim($this->test);
  237. }
  238. // Check parameters
  239. // Put here code to add a control on parameters values
  240. if (dol_strlen($this->datenextrun) == 0) {
  241. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
  242. $error++;
  243. }
  244. if (empty($this->label)) {
  245. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
  246. $error++;
  247. }
  248. if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
  249. $this->errors[] = $langs->trans('CronErrEndDateStartDt');
  250. $error++;
  251. }
  252. if (empty($this->unitfrequency)) {
  253. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
  254. $error++;
  255. }
  256. if (($this->jobtype == 'command') && (empty($this->command))) {
  257. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
  258. $error++;
  259. }
  260. if (($this->jobtype == 'method') && (empty($this->classesname))) {
  261. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
  262. $error++;
  263. }
  264. if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  265. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
  266. $error++;
  267. }
  268. if (($this->jobtype == 'method') && (empty($this->objectname))) {
  269. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
  270. $error++;
  271. }
  272. if (($this->jobtype == 'function') && (empty($this->libname))) {
  273. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
  274. $error++;
  275. }
  276. // Insert request
  277. $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
  278. $sql .= "entity,";
  279. $sql .= "datec,";
  280. $sql .= "jobtype,";
  281. $sql .= "label,";
  282. $sql .= "command,";
  283. $sql .= "classesname,";
  284. $sql .= "objectname,";
  285. $sql .= "methodename,";
  286. $sql .= "params,";
  287. $sql .= "md5params,";
  288. $sql .= "module_name,";
  289. $sql .= "priority,";
  290. $sql .= "datelastrun,";
  291. $sql .= "datenextrun,";
  292. $sql .= "dateend,";
  293. $sql .= "datestart,";
  294. $sql .= "lastresult,";
  295. $sql .= "datelastresult,";
  296. $sql .= "lastoutput,";
  297. $sql .= "unitfrequency,";
  298. $sql .= "frequency,";
  299. $sql .= "status,";
  300. $sql .= "fk_user_author,";
  301. $sql .= "fk_user_mod,";
  302. $sql .= "note,";
  303. $sql .= "nbrun,";
  304. $sql .= "maxrun,";
  305. $sql .= "libname,";
  306. $sql .= "test";
  307. $sql .= ") VALUES (";
  308. $sql .= " ".(!isset($this->entity) ? $conf->entity : $this->db->escape($this->entity)).",";
  309. $sql .= " '".$this->db->idate($now)."',";
  310. $sql .= " ".(!isset($this->jobtype) ? 'NULL' : "'".$this->db->escape($this->jobtype)."'").",";
  311. $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
  312. $sql .= " ".(!isset($this->command) ? 'NULL' : "'".$this->db->escape($this->command)."'").",";
  313. $sql .= " ".(!isset($this->classesname) ? 'NULL' : "'".$this->db->escape($this->classesname)."'").",";
  314. $sql .= " ".(!isset($this->objectname) ? 'NULL' : "'".$this->db->escape($this->objectname)."'").",";
  315. $sql .= " ".(!isset($this->methodename) ? 'NULL' : "'".$this->db->escape($this->methodename)."'").",";
  316. $sql .= " ".(!isset($this->params) ? 'NULL' : "'".$this->db->escape($this->params)."'").",";
  317. $sql .= " ".(!isset($this->md5params) ? 'NULL' : "'".$this->db->escape($this->md5params)."'").",";
  318. $sql .= " ".(!isset($this->module_name) ? 'NULL' : "'".$this->db->escape($this->module_name)."'").",";
  319. $sql .= " ".(!isset($this->priority) ? '0' : $this->priority).",";
  320. $sql .= " ".(!isset($this->datelastrun) || dol_strlen($this->datelastrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastrun)."'").",";
  321. $sql .= " ".(!isset($this->datenextrun) || dol_strlen($this->datenextrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datenextrun)."'").",";
  322. $sql .= " ".(!isset($this->dateend) || dol_strlen($this->dateend) == 0 ? 'NULL' : "'".$this->db->idate($this->dateend)."'").",";
  323. $sql .= " ".(!isset($this->datestart) || dol_strlen($this->datestart) == 0 ? 'NULL' : "'".$this->db->idate($this->datestart)."'").",";
  324. $sql .= " ".(!isset($this->lastresult) ? 'NULL' : "'".$this->db->escape($this->lastresult)."'").",";
  325. $sql .= " ".(!isset($this->datelastresult) || dol_strlen($this->datelastresult) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastresult)."'").",";
  326. $sql .= " ".(!isset($this->lastoutput) ? 'NULL' : "'".$this->db->escape($this->lastoutput)."'").",";
  327. $sql .= " ".(!isset($this->unitfrequency) ? 'NULL' : "'".$this->db->escape($this->unitfrequency)."'").",";
  328. $sql .= " ".(!isset($this->frequency) ? '0' : $this->frequency).",";
  329. $sql .= " ".(!isset($this->status) ? '0' : $this->status).",";
  330. $sql .= " ".$user->id.",";
  331. $sql .= " ".$user->id.",";
  332. $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'").",";
  333. $sql .= " ".(!isset($this->nbrun) ? '0' : $this->db->escape($this->nbrun)).",";
  334. $sql .= " ".(empty($this->maxrun) ? '0' : $this->db->escape($this->maxrun)).",";
  335. $sql .= " ".(!isset($this->libname) ? 'NULL' : "'".$this->db->escape($this->libname)."'").",";
  336. $sql .= " ".(!isset($this->test) ? 'NULL' : "'".$this->db->escape($this->test)."'")."";
  337. $sql .= ")";
  338. $this->db->begin();
  339. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  340. $resql = $this->db->query($sql);
  341. if (!$resql) {
  342. $error++;
  343. $this->errors[] = "Error ".$this->db->lasterror();
  344. }
  345. if (!$error) {
  346. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
  347. }
  348. // Commit or rollback
  349. if ($error) {
  350. $this->db->rollback();
  351. return -1 * $error;
  352. } else {
  353. $this->db->commit();
  354. return $this->id;
  355. }
  356. }
  357. /**
  358. * Load object in memory from the database
  359. *
  360. * @param int $id Id object
  361. * @param string $objectname Object name
  362. * @param string $methodname Method name
  363. * @return int <0 if KO, >0 if OK
  364. */
  365. public function fetch($id, $objectname = '', $methodname = '')
  366. {
  367. $sql = "SELECT";
  368. $sql .= " t.rowid,";
  369. $sql .= " t.entity,";
  370. $sql .= " t.tms,";
  371. $sql .= " t.datec,";
  372. $sql .= " t.jobtype,";
  373. $sql .= " t.label,";
  374. $sql .= " t.command,";
  375. $sql .= " t.classesname,";
  376. $sql .= " t.objectname,";
  377. $sql .= " t.methodename,";
  378. $sql .= " t.params,";
  379. $sql .= " t.md5params,";
  380. $sql .= " t.module_name,";
  381. $sql .= " t.priority,";
  382. $sql .= " t.datelastrun,";
  383. $sql .= " t.datenextrun,";
  384. $sql .= " t.dateend,";
  385. $sql .= " t.datestart,";
  386. $sql .= " t.lastresult,";
  387. $sql .= " t.datelastresult,";
  388. $sql .= " t.lastoutput,";
  389. $sql .= " t.unitfrequency,";
  390. $sql .= " t.frequency,";
  391. $sql .= " t.status,";
  392. $sql .= " t.processing,";
  393. $sql .= " t.pid,";
  394. $sql .= " t.email_alert,";
  395. $sql .= " t.fk_user_author,";
  396. $sql .= " t.fk_user_mod,";
  397. $sql .= " t.note as note_private,";
  398. $sql .= " t.nbrun,";
  399. $sql .= " t.maxrun,";
  400. $sql .= " t.libname,";
  401. $sql .= " t.test";
  402. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  403. if ($id > 0) {
  404. $sql .= " WHERE t.rowid = ".((int) $id);
  405. } else {
  406. $sql .= " WHERE t.entity IN(0, ".getEntity('cron').")";
  407. $sql .= " AND t.objectname = '".$this->db->escape($objectname)."'";
  408. $sql .= " AND t.methodename = '".$this->db->escape($methodname)."'";
  409. }
  410. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  411. $resql = $this->db->query($sql);
  412. if ($resql) {
  413. if ($this->db->num_rows($resql)) {
  414. $obj = $this->db->fetch_object($resql);
  415. $this->id = $obj->rowid;
  416. $this->ref = $obj->rowid;
  417. $this->entity = $obj->entity;
  418. $this->tms = $this->db->jdate($obj->tms);
  419. $this->datec = $this->db->jdate($obj->datec);
  420. $this->label = $obj->label;
  421. $this->jobtype = $obj->jobtype;
  422. $this->command = $obj->command;
  423. $this->classesname = $obj->classesname;
  424. $this->objectname = $obj->objectname;
  425. $this->methodename = $obj->methodename;
  426. $this->params = $obj->params;
  427. $this->md5params = $obj->md5params;
  428. $this->module_name = $obj->module_name;
  429. $this->priority = $obj->priority;
  430. $this->datelastrun = $this->db->jdate($obj->datelastrun);
  431. $this->datenextrun = $this->db->jdate($obj->datenextrun);
  432. $this->dateend = $this->db->jdate($obj->dateend);
  433. $this->datestart = $this->db->jdate($obj->datestart);
  434. $this->lastresult = $obj->lastresult;
  435. $this->lastoutput = $obj->lastoutput;
  436. $this->datelastresult = $this->db->jdate($obj->datelastresult);
  437. $this->unitfrequency = $obj->unitfrequency;
  438. $this->frequency = $obj->frequency;
  439. $this->status = $obj->status;
  440. $this->processing = $obj->processing;
  441. $this->pid = $obj->pid;
  442. $this->email_alert = $obj->email_alert;
  443. $this->fk_user_author = $obj->fk_user_author;
  444. $this->fk_user_mod = $obj->fk_user_mod;
  445. $this->note_private = $obj->note_private;
  446. $this->nbrun = $obj->nbrun;
  447. $this->maxrun = $obj->maxrun;
  448. $this->libname = $obj->libname;
  449. $this->test = $obj->test;
  450. }
  451. $this->db->free($resql);
  452. return 1;
  453. } else {
  454. $this->error = "Error ".$this->db->lasterror();
  455. return -1;
  456. }
  457. }
  458. /**
  459. * Load list of cron jobs in a memory array from the database
  460. * @TODO Use object CronJob and not CronJobLine.
  461. *
  462. * @param string $sortorder sort order
  463. * @param string $sortfield sort field
  464. * @param int $limit limit page
  465. * @param int $offset page
  466. * @param int $status display active or not
  467. * @param array $filter filter output
  468. * @param int $processing Processing or not
  469. * @return int <0 if KO, >0 if OK
  470. */
  471. public function fetchAll($sortorder = 'DESC', $sortfield = 't.rowid', $limit = 0, $offset = 0, $status = 1, $filter = '', $processing = -1)
  472. {
  473. $this->lines = array();
  474. $sql = "SELECT";
  475. $sql .= " t.rowid,";
  476. $sql .= " t.entity,";
  477. $sql .= " t.tms,";
  478. $sql .= " t.datec,";
  479. $sql .= " t.jobtype,";
  480. $sql .= " t.label,";
  481. $sql .= " t.command,";
  482. $sql .= " t.classesname,";
  483. $sql .= " t.objectname,";
  484. $sql .= " t.methodename,";
  485. $sql .= " t.params,";
  486. $sql .= " t.md5params,";
  487. $sql .= " t.module_name,";
  488. $sql .= " t.priority,";
  489. $sql .= " t.datelastrun,";
  490. $sql .= " t.datenextrun,";
  491. $sql .= " t.dateend,";
  492. $sql .= " t.datestart,";
  493. $sql .= " t.lastresult,";
  494. $sql .= " t.datelastresult,";
  495. $sql .= " t.lastoutput,";
  496. $sql .= " t.unitfrequency,";
  497. $sql .= " t.frequency,";
  498. $sql .= " t.status,";
  499. $sql .= " t.processing,";
  500. $sql .= " t.pid,";
  501. $sql .= " t.email_alert,";
  502. $sql .= " t.fk_user_author,";
  503. $sql .= " t.fk_user_mod,";
  504. $sql .= " t.note as note_private,";
  505. $sql .= " t.nbrun,";
  506. $sql .= " t.libname,";
  507. $sql .= " t.test";
  508. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  509. $sql .= " WHERE 1 = 1";
  510. if ($processing >= 0) {
  511. $sql .= " AND t.processing = ".(empty($processing) ? '0' : '1');
  512. }
  513. if ($status >= 0 && $status < 2) {
  514. $sql .= " AND t.status = ".(empty($status) ? '0' : '1');
  515. } elseif ($status == 2) {
  516. $sql .= " AND t.status = 2";
  517. }
  518. // Manage filter
  519. if (is_array($filter) && count($filter) > 0) {
  520. foreach ($filter as $key => $value) {
  521. if ($key == 't.rowid') {
  522. $sql .= " AND ".$key." = ".((int) $value);
  523. } else {
  524. $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
  525. }
  526. }
  527. }
  528. $sql .= $this->db->order($sortfield, $sortorder);
  529. if (!empty($limit) && !empty($offset)) {
  530. $sql .= $this->db->plimit($limit + 1, $offset);
  531. }
  532. $sqlwhere = array();
  533. if (count($sqlwhere) > 0) {
  534. $sql .= " WHERE ".implode(' AND ', $sqlwhere);
  535. }
  536. dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
  537. $resql = $this->db->query($sql);
  538. if ($resql) {
  539. $num = $this->db->num_rows($resql);
  540. $i = 0;
  541. if ($num) {
  542. while ($i < $num) {
  543. $line = new Cronjobline();
  544. $obj = $this->db->fetch_object($resql);
  545. $line->id = $obj->rowid;
  546. $line->ref = $obj->rowid;
  547. $line->entity = $obj->entity;
  548. $line->tms = $this->db->jdate($obj->tms);
  549. $line->datec = $this->db->jdate($obj->datec);
  550. $line->label = $obj->label;
  551. $line->jobtype = $obj->jobtype;
  552. $line->command = $obj->command;
  553. $line->classesname = $obj->classesname;
  554. $line->objectname = $obj->objectname;
  555. $line->methodename = $obj->methodename;
  556. $line->params = $obj->params;
  557. $line->md5params = $obj->md5params;
  558. $line->module_name = $obj->module_name;
  559. $line->priority = $obj->priority;
  560. $line->datelastrun = $this->db->jdate($obj->datelastrun);
  561. $line->datenextrun = $this->db->jdate($obj->datenextrun);
  562. $line->dateend = $this->db->jdate($obj->dateend);
  563. $line->datestart = $this->db->jdate($obj->datestart);
  564. $line->lastresult = $obj->lastresult;
  565. $line->datelastresult = $this->db->jdate($obj->datelastresult);
  566. $line->lastoutput = $obj->lastoutput;
  567. $line->unitfrequency = $obj->unitfrequency;
  568. $line->frequency = $obj->frequency;
  569. $line->status = $obj->status;
  570. $line->processing = $obj->processing;
  571. $line->pid = $obj->pid;
  572. $line->email_alert = $obj->email_alert;
  573. $line->fk_user_author = $obj->fk_user_author;
  574. $line->fk_user_mod = $obj->fk_user_mod;
  575. $line->note_private = $obj->note_private;
  576. $line->nbrun = $obj->nbrun;
  577. $line->libname = $obj->libname;
  578. $line->test = $obj->test;
  579. $this->lines[] = $line;
  580. $i++;
  581. }
  582. }
  583. $this->db->free($resql);
  584. return 1;
  585. } else {
  586. $this->error = "Error ".$this->db->lasterror();
  587. return -1;
  588. }
  589. }
  590. /**
  591. * Update object into database
  592. *
  593. * @param User $user User that modifies
  594. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  595. * @return int <0 if KO, >0 if OK
  596. */
  597. public function update($user = null, $notrigger = 0)
  598. {
  599. global $conf, $langs;
  600. $langs->load('cron');
  601. $error = 0;
  602. // Clean parameters
  603. if (isset($this->label)) {
  604. $this->label = trim($this->label);
  605. }
  606. if (isset($this->jobtype)) {
  607. $this->jobtype = trim($this->jobtype);
  608. }
  609. if (isset($this->command)) {
  610. $this->command = trim($this->command);
  611. }
  612. if (isset($this->classesname)) {
  613. $this->classesname = trim($this->classesname);
  614. }
  615. if (isset($this->objectname)) {
  616. $this->objectname = trim($this->objectname);
  617. }
  618. if (isset($this->methodename)) {
  619. $this->methodename = trim($this->methodename);
  620. }
  621. if (isset($this->params)) {
  622. $this->params = trim($this->params);
  623. }
  624. if (isset($this->md5params)) {
  625. $this->md5params = trim($this->md5params);
  626. }
  627. if (isset($this->module_name)) {
  628. $this->module_name = trim($this->module_name);
  629. }
  630. if (isset($this->priority)) {
  631. $this->priority = trim($this->priority);
  632. }
  633. if (isset($this->lastoutput)) {
  634. $this->lastoutput = trim($this->lastoutput);
  635. }
  636. if (isset($this->lastresult)) {
  637. $this->lastresult = trim($this->lastresult);
  638. }
  639. if (isset($this->unitfrequency)) {
  640. $this->unitfrequency = trim($this->unitfrequency);
  641. }
  642. if (isset($this->frequency)) {
  643. $this->frequency = trim($this->frequency);
  644. }
  645. if (isset($this->status)) {
  646. $this->status = trim($this->status);
  647. }
  648. if (isset($this->note_private)) {
  649. $this->note_private = trim($this->note_private);
  650. }
  651. if (isset($this->nbrun)) {
  652. $this->nbrun = trim($this->nbrun);
  653. }
  654. if (isset($this->libname)) {
  655. $this->libname = trim($this->libname);
  656. }
  657. if (isset($this->test)) {
  658. $this->test = trim($this->test);
  659. }
  660. if (empty($this->maxrun)) {
  661. $this->maxrun = 0;
  662. }
  663. if (empty($this->processing)) {
  664. $this->processing = 0;
  665. }
  666. if (empty($this->pid)) {
  667. $this->pid = null;
  668. }
  669. if (empty($this->email_alert)) {
  670. $this->email_alert = '';
  671. }
  672. // Check parameters
  673. // Put here code to add a control on parameters values
  674. if (dol_strlen($this->datenextrun) == 0) {
  675. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
  676. $error++;
  677. }
  678. if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
  679. $this->errors[] = $langs->trans('CronErrEndDateStartDt');
  680. $error++;
  681. }
  682. if (empty($this->label)) {
  683. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
  684. $error++;
  685. }
  686. if (empty($this->unitfrequency)) {
  687. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
  688. $error++;
  689. }
  690. if (($this->jobtype == 'command') && (empty($this->command))) {
  691. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
  692. $error++;
  693. }
  694. if (($this->jobtype == 'method') && (empty($this->classesname))) {
  695. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
  696. $error++;
  697. }
  698. if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  699. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
  700. $error++;
  701. }
  702. if (($this->jobtype == 'method') && (empty($this->objectname))) {
  703. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
  704. $error++;
  705. }
  706. if (($this->jobtype == 'function') && (empty($this->libname))) {
  707. $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
  708. $error++;
  709. }
  710. // Update request
  711. $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
  712. $sql .= " entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).",";
  713. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  714. $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").",";
  715. $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").",";
  716. $sql .= " classesname=".(isset($this->classesname) ? "'".$this->db->escape($this->classesname)."'" : "null").",";
  717. $sql .= " objectname=".(isset($this->objectname) ? "'".$this->db->escape($this->objectname)."'" : "null").",";
  718. $sql .= " methodename=".(isset($this->methodename) ? "'".$this->db->escape($this->methodename)."'" : "null").",";
  719. $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").",";
  720. $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").",";
  721. $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").",";
  722. $sql .= " priority=".(isset($this->priority) ? ((int) $this->priority) : "null").",";
  723. $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
  724. $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
  725. $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
  726. $sql .= " datestart=".(dol_strlen($this->datestart) != 0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
  727. $sql .= " datelastresult=".(dol_strlen($this->datelastresult) != 0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
  728. $sql .= " lastresult=".(isset($this->lastresult) ? "'".$this->db->escape($this->lastresult)."'" : "null").",";
  729. $sql .= " lastoutput=".(isset($this->lastoutput) ? "'".$this->db->escape($this->lastoutput)."'" : "null").",";
  730. $sql .= " unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency : "null").",";
  731. $sql .= " frequency=".(isset($this->frequency) ? $this->frequency : "null").",";
  732. $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
  733. $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").",";
  734. $sql .= " pid=".(isset($this->pid) ? ((int) $this->pid) : "null").",";
  735. $sql .= " email_alert = ".(isset($this->email_alert) ? "'".$this->db->escape($this->email_alert)."'" : "null").",";
  736. $sql .= " fk_user_mod = ".((int) $user->id).",";
  737. $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
  738. $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").",";
  739. $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").",";
  740. $sql .= " libname=".(isset($this->libname) ? "'".$this->db->escape($this->libname)."'" : "null").",";
  741. $sql .= " test=".(isset($this->test) ? "'".$this->db->escape($this->test)."'" : "null");
  742. $sql .= " WHERE rowid=".((int) $this->id);
  743. $this->db->begin();
  744. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  745. $resql = $this->db->query($sql);
  746. if (!$resql) {
  747. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  748. }
  749. // Commit or rollback
  750. if ($error) {
  751. foreach ($this->errors as $errmsg) {
  752. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  753. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  754. }
  755. $this->db->rollback();
  756. return -1 * $error;
  757. } else {
  758. $this->db->commit();
  759. return 1;
  760. }
  761. }
  762. /**
  763. * Delete object in database
  764. *
  765. * @param User $user User that deletes
  766. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  767. * @return int <0 if KO, >0 if OK
  768. */
  769. public function delete($user, $notrigger = 0)
  770. {
  771. $error = 0;
  772. $this->db->begin();
  773. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
  774. $sql .= " WHERE rowid=".((int) $this->id);
  775. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  776. $resql = $this->db->query($sql);
  777. if (!$resql) {
  778. $error++;
  779. $this->errors[] = "Error ".$this->db->lasterror();
  780. }
  781. // Commit or rollback
  782. if ($error) {
  783. foreach ($this->errors as $errmsg) {
  784. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  785. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  786. }
  787. $this->db->rollback();
  788. return -1 * $error;
  789. } else {
  790. $this->db->commit();
  791. return 1;
  792. }
  793. }
  794. /**
  795. * Load an object from its id and create a new one in database
  796. *
  797. * @param User $user User making the clone
  798. * @param int $fromid Id of object to clone
  799. * @return int New id of clone
  800. */
  801. public function createFromClone(User $user, $fromid)
  802. {
  803. global $langs;
  804. $error = 0;
  805. $object = new Cronjob($this->db);
  806. $this->db->begin();
  807. // Load source object
  808. $object->fetch($fromid);
  809. $object->id = 0;
  810. // Clear fields
  811. $object->status = self::STATUS_DISABLED;
  812. $object->label = $langs->trans("CopyOf").' '.$langs->trans($object->label);
  813. // Create clone
  814. $object->context['createfromclone'] = 'createfromclone';
  815. $result = $object->create($user);
  816. // Other options
  817. if ($result < 0) {
  818. $this->error = $object->error;
  819. $error++;
  820. }
  821. unset($object->context['createfromclone']);
  822. // End
  823. if (!$error) {
  824. $this->db->commit();
  825. return $object->id;
  826. } else {
  827. $this->db->rollback();
  828. return -1;
  829. }
  830. }
  831. /**
  832. * Initialise object with example values
  833. * Id must be 0 if object instance is a specimen
  834. *
  835. * @return void
  836. */
  837. public function initAsSpecimen()
  838. {
  839. $this->id = 0;
  840. $this->ref = 0;
  841. $this->entity = 0;
  842. $this->tms = '';
  843. $this->datec = '';
  844. $this->label = '';
  845. $this->jobtype = '';
  846. $this->command = '';
  847. $this->classesname = '';
  848. $this->objectname = '';
  849. $this->methodename = '';
  850. $this->params = '';
  851. $this->md5params = '';
  852. $this->module_name = '';
  853. $this->priority = '';
  854. $this->datelastrun = '';
  855. $this->datenextrun = '';
  856. $this->dateend = '';
  857. $this->datestart = '';
  858. $this->datelastresult = '';
  859. $this->lastoutput = '';
  860. $this->lastresult = '';
  861. $this->unitfrequency = '';
  862. $this->frequency = '';
  863. $this->status = 0;
  864. $this->processing = 0;
  865. $this->pid = null;
  866. $this->email_alert = '';
  867. $this->fk_user_author = 0;
  868. $this->fk_user_mod = 0;
  869. $this->note_private = '';
  870. $this->nbrun = '';
  871. $this->maxrun = 100;
  872. $this->libname = '';
  873. }
  874. /**
  875. * Return a link to the object card (with optionaly the picto)
  876. *
  877. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  878. * @param string $option On what the link point to ('nolink', ...)
  879. * @param int $notooltip 1=Disable tooltip
  880. * @param string $morecss Add more css on link
  881. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  882. * @return string String with URL
  883. */
  884. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  885. {
  886. global $db, $conf, $langs;
  887. global $dolibarr_main_authentication, $dolibarr_main_demo;
  888. global $menumanager;
  889. if (!empty($conf->dol_no_mouse_hover)) {
  890. $notooltip = 1; // Force disable tooltips
  891. }
  892. $result = '';
  893. $label = img_picto('', 'object_'.$this->picto).' <u>'.$langs->trans("CronTask").'</u>';
  894. if (isset($this->status)) {
  895. $label .= ' '.$this->getLibStatut(5);
  896. }
  897. $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  898. $label .= '<br><b>'.$langs->trans('Title').':</b> '.$langs->trans($this->label);
  899. if ($this->label != $langs->trans($this->label)) {
  900. $label .= ' <span class="opacitymedium">('.$this->label.')</span>';
  901. }
  902. if (!empty($this->datestart)) {
  903. $label .= '<br><b>'.$langs->trans('CronDtStart').':</b> '.dol_print_date($this->datestart, 'dayhour', 'tzuserrel');
  904. }
  905. if (!empty($this->dateend)) {
  906. $label .= '<br><b>'.$langs->trans('CronDtEnd').':</b> '.dol_print_date($this->dateend, 'dayhour', 'tzuserrel');
  907. }
  908. $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
  909. if ($option != 'nolink') {
  910. // Add param to save lastsearch_values or not
  911. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  912. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  913. $add_save_lastsearch_values = 1;
  914. }
  915. if ($add_save_lastsearch_values) {
  916. $url .= '&save_lastsearch_values=1';
  917. }
  918. }
  919. $linkclose = '';
  920. if (empty($notooltip)) {
  921. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  922. $label = $langs->trans("ShowCronJob");
  923. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  924. }
  925. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  926. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  927. } else {
  928. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  929. }
  930. $linkstart = '<a href="'.$url.'"';
  931. $linkstart .= $linkclose.'>';
  932. $linkend = '</a>';
  933. $result .= $linkstart;
  934. if ($withpicto) {
  935. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  936. }
  937. if ($withpicto != 2) {
  938. $result .= $this->ref;
  939. }
  940. $result .= $linkend;
  941. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  942. return $result;
  943. }
  944. /**
  945. * Load object information
  946. *
  947. * @param int $id ID
  948. * @return int <0 if KO, >0 if OK
  949. */
  950. public function info($id)
  951. {
  952. $sql = "SELECT";
  953. $sql .= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
  954. $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as f";
  955. $sql .= " WHERE f.rowid = ".((int) $id);
  956. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  957. $resql = $this->db->query($sql);
  958. if ($resql) {
  959. if ($this->db->num_rows($resql)) {
  960. $obj = $this->db->fetch_object($resql);
  961. $this->id = $obj->rowid;
  962. $this->user_modification_id = $obj->fk_user_mod;
  963. $this->user_creation_id = $obj->fk_user_author;
  964. $this->date_creation = $this->db->jdate($obj->datec);
  965. $this->date_modification = $this->db->jdate($obj->tms);
  966. }
  967. $this->db->free($resql);
  968. return 1;
  969. } else {
  970. $this->error = "Error ".$this->db->lasterror();
  971. return -1;
  972. }
  973. }
  974. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  975. /**
  976. * Run a job.
  977. * Once job is finished, status and nb of run is updated.
  978. * This function does not plan the next run. This is done by function ->reprogram_jobs
  979. *
  980. * @param string $userlogin User login
  981. * @return int <0 if KO, >0 if OK
  982. */
  983. public function run_jobs($userlogin)
  984. {
  985. // phpcs:enable
  986. global $langs, $conf, $hookmanager;
  987. $hookmanager->initHooks(array('cron'));
  988. $now = dol_now();
  989. $error = 0;
  990. $retval = '';
  991. $langs->load('cron');
  992. if (empty($userlogin)) {
  993. $this->error = "User login is mandatory";
  994. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  995. return -1;
  996. }
  997. // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
  998. // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
  999. if ($conf->entity != $this->entity && $this->entity > 0) {
  1000. dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
  1001. }
  1002. $savcurrententity = $conf->entity;
  1003. $conf->setEntityValues($this->db, $this->entity);
  1004. dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
  1005. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1006. $user = new User($this->db);
  1007. $result = $user->fetch('', $userlogin);
  1008. if ($result < 0) {
  1009. $this->error = "User Error:".$user->error;
  1010. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1011. $conf->setEntityValues($this->db, $savcurrententity);
  1012. return -1;
  1013. } else {
  1014. if (empty($user->id)) {
  1015. $this->error = " User user login:".$userlogin." do not exists";
  1016. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1017. $conf->setEntityValues($this->db, $savcurrententity);
  1018. return -1;
  1019. }
  1020. }
  1021. dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
  1022. // Increase limit of time. Works only if we are not in safe mode
  1023. $ExecTimeLimit = 600;
  1024. if (!empty($ExecTimeLimit)) {
  1025. $err = error_reporting();
  1026. error_reporting(0); // Disable all errors
  1027. //error_reporting(E_ALL);
  1028. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  1029. error_reporting($err);
  1030. }
  1031. $MemoryLimit = 0;
  1032. if (!empty($MemoryLimit)) {
  1033. @ini_set('memory_limit', $MemoryLimit);
  1034. }
  1035. // Update last run date start (to track running jobs)
  1036. $this->datelastrun = $now;
  1037. $this->datelastresult = null;
  1038. $this->lastoutput = '';
  1039. $this->lastresult = '';
  1040. $this->processing = 1; // To know job was started
  1041. $this->pid = function_exists('getmypid') ? getmypid() : null; // Avoid dol_getmypid to get null if the function is not available
  1042. $this->nbrun = $this->nbrun + 1;
  1043. $result = $this->update($user); // This include begin/commit
  1044. if ($result < 0) {
  1045. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1046. $conf->setEntityValues($this->db, $savcurrententity);
  1047. return -1;
  1048. }
  1049. // Run a method
  1050. if ($this->jobtype == 'method') {
  1051. // load classes
  1052. if (!$error) {
  1053. $ret = dol_include_once($this->classesname);
  1054. if ($ret === false || (!class_exists($this->objectname))) {
  1055. if ($ret === false) {
  1056. $this->error = $langs->transnoentitiesnoconv('CronCannotLoadClass', $this->classesname, $this->objectname);
  1057. } else {
  1058. $this->error = $langs->transnoentitiesnoconv('CronCannotLoadObject', $this->classesname, $this->objectname);
  1059. }
  1060. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1061. $this->lastoutput = $this->error;
  1062. $this->lastresult = -1;
  1063. $retval = $this->lastresult;
  1064. $error++;
  1065. }
  1066. }
  1067. // test if method exists
  1068. if (!$error) {
  1069. if (!method_exists($this->objectname, $this->methodename)) {
  1070. $this->error = $langs->transnoentitiesnoconv('CronMethodDoesNotExists', $this->objectname, $this->methodename);
  1071. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1072. $this->lastoutput = $this->error;
  1073. $this->lastresult = -1;
  1074. $retval = $this->lastresult;
  1075. $error++;
  1076. }
  1077. if (in_array(strtolower(trim($this->methodename)), array('executecli'))) {
  1078. $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname);
  1079. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1080. $this->lastoutput = $this->error;
  1081. $this->lastresult = -1;
  1082. $retval = $this->lastresult;
  1083. $error++;
  1084. }
  1085. }
  1086. // Load langs
  1087. if (!$error) {
  1088. $result = $langs->load($this->module_name);
  1089. $result = $langs->load($this->module_name.'@'.$this->module_name, 0, 0, '', 0, 1);
  1090. if ($result < 0) { // If technical error
  1091. dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
  1092. $this->error = $langs->error;
  1093. $this->lastoutput = $this->error;
  1094. $this->lastresult = -1;
  1095. $retval = $this->lastresult;
  1096. $error++;
  1097. }
  1098. }
  1099. if (!$error) {
  1100. dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
  1101. // Create Object for the called module
  1102. $nameofclass = $this->objectname;
  1103. $object = new $nameofclass($this->db);
  1104. if ($this->entity > 0) {
  1105. $object->entity = $this->entity; // We work on a dedicated entity
  1106. }
  1107. $params_arr = array();
  1108. if (!empty($this->params) || $this->params === '0') {
  1109. $params_arr = array_map('trim', explode(",", $this->params));
  1110. }
  1111. if (!is_array($params_arr)) {
  1112. $result = call_user_func(array($object, $this->methodename), $this->params);
  1113. } else {
  1114. $result = call_user_func_array(array($object, $this->methodename), $params_arr);
  1115. }
  1116. if ($result === false || (!is_bool($result) && $result != 0)) {
  1117. $langs->load("errors");
  1118. $errmsg = '';
  1119. if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
  1120. $errmsg .= $object->error;
  1121. }
  1122. if (is_array($object->errors) && count($object->errors)) {
  1123. $errmsg .= (($errmsg ? ', ' : '').join(', ', $object->errors));
  1124. }
  1125. if (empty($errmsg)) {
  1126. $errmsg = $langs->trans('ErrorUnknown');
  1127. }
  1128. dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
  1129. $this->error = $errmsg;
  1130. $this->lastoutput = (!empty($object->output) ? $object->output."\n" : "").$errmsg;
  1131. $this->lastresult = is_numeric($result) ? $result : -1;
  1132. $retval = $this->lastresult;
  1133. $error++;
  1134. } else {
  1135. dol_syslog(get_class($this)."::run_jobs END");
  1136. $this->lastoutput = (!empty($object->output) ? $object->output : "");
  1137. $this->lastresult = var_export($result, true);
  1138. $retval = $this->lastresult;
  1139. }
  1140. }
  1141. }
  1142. if ($this->jobtype == 'function') {
  1143. //load lib
  1144. $libpath = '/'.strtolower($this->module_name).'/lib/'.$this->libname;
  1145. $ret = dol_include_once($libpath);
  1146. if ($ret === false) {
  1147. $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath;
  1148. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1149. $conf->setEntityValues($this->db, $savcurrententity);
  1150. return -1;
  1151. }
  1152. // Load langs
  1153. $result = $langs->load($this->module_name);
  1154. $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing
  1155. if ($result < 0) { // If technical error
  1156. dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
  1157. $conf->setEntityValues($this->db, $savcurrententity);
  1158. return -1;
  1159. }
  1160. dol_syslog(get_class($this)."::run_jobs ".$this->libname."::".$this->methodename."(".$this->params.");", LOG_DEBUG);
  1161. $params_arr = explode(", ", $this->params);
  1162. if (!is_array($params_arr)) {
  1163. $result = call_user_func($this->methodename, $this->params);
  1164. } else {
  1165. $result = call_user_func_array($this->methodename, $params_arr);
  1166. }
  1167. if ($result === false || (!is_bool($result) && $result != 0)) {
  1168. $langs->load("errors");
  1169. dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
  1170. $this->error = $langs->trans('ErrorUnknown');
  1171. $this->lastoutput = $this->error;
  1172. $this->lastresult = is_numeric($result) ? $result : -1;
  1173. $retval = $this->lastresult;
  1174. $error++;
  1175. } else {
  1176. $this->lastoutput = var_export($result, true);
  1177. $this->lastresult = var_export($result, true); // Return code
  1178. $retval = $this->lastresult;
  1179. }
  1180. }
  1181. // Run a command line
  1182. if ($this->jobtype == 'command') {
  1183. global $dolibarr_cron_allow_cli;
  1184. if (empty($dolibarr_cron_allow_cli)) {
  1185. $langs->load("errors");
  1186. $this->error = $langs->trans("FailedToExecutCommandJob");
  1187. $this->lastoutput = '';
  1188. $this->lastresult = $langs->trans("ErrorParameterMustBeEnabledToAllwoThisFeature", 'dolibarr_cron_allow_cli');
  1189. } else {
  1190. $outputdir = $conf->cron->dir_temp;
  1191. if (empty($outputdir)) {
  1192. $outputdir = $conf->cronjob->dir_temp;
  1193. }
  1194. if (!empty($outputdir)) {
  1195. dol_mkdir($outputdir);
  1196. $outputfile = $outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
  1197. // Execute a CLI
  1198. include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  1199. $utils = new Utils($this->db);
  1200. $arrayresult = $utils->executeCLI($this->command, $outputfile);
  1201. $retval = $arrayresult['result'];
  1202. $this->error = $arrayresult['error'];
  1203. $this->lastoutput = $arrayresult['output'];
  1204. $this->lastresult = $arrayresult['result'];
  1205. }
  1206. }
  1207. }
  1208. dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
  1209. $this->datelastresult = dol_now();
  1210. $this->processing = 0;
  1211. $this->pid = null;
  1212. $result = $this->update($user); // This include begin/commit
  1213. if ($result < 0) {
  1214. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1215. $conf->setEntityValues($this->db, $savcurrententity);
  1216. return -1;
  1217. }
  1218. $conf->setEntityValues($this->db, $savcurrententity);
  1219. if ($error && !empty($this->email_alert)) {
  1220. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  1221. $subject = $langs->trans("ErrorInBatch", $this->label);
  1222. $msg = $langs->trans("ErrorInBatch", $this->label);
  1223. $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
  1224. $cmailfile = new CMailFile($subject, $this->email_alert, $from, $msg);
  1225. $result = $cmailfile->sendfile(); // Do not test result
  1226. }
  1227. return $error ?-1 : 1;
  1228. }
  1229. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1230. /**
  1231. * Reprogram a job
  1232. *
  1233. * @param string $userlogin User login
  1234. * @param integer $now Date returned by dol_now()
  1235. * @return int <0 if KO, >0 if OK
  1236. */
  1237. public function reprogram_jobs($userlogin, $now)
  1238. {
  1239. // phpcs:enable
  1240. dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
  1241. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1242. $user = new User($this->db);
  1243. $result = $user->fetch('', $userlogin);
  1244. if ($result < 0) {
  1245. $this->error = "User Error : ".$user->error;
  1246. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1247. return -1;
  1248. } else {
  1249. if (empty($user->id)) {
  1250. $this->error = " User user login:".$userlogin." do not exists";
  1251. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1252. return -1;
  1253. }
  1254. }
  1255. dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
  1256. if (empty($this->datenextrun)) {
  1257. if (empty($this->datestart)) {
  1258. if ($this->unitfrequency == 2678400) {
  1259. $this->datenextrun = dol_time_plus_duree($now, $this->frequency, 'm');
  1260. } else {
  1261. $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
  1262. }
  1263. } else {
  1264. if ($this->unitfrequency == 2678400) {
  1265. $this->datenextrun = dol_time_plus_duree($this->datestart, $this->frequency, 'm');
  1266. } else {
  1267. $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
  1268. }
  1269. }
  1270. }
  1271. if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
  1272. // Loop until date is after future
  1273. while ($this->datenextrun < $now) {
  1274. if ($this->unitfrequency == 2678400) {
  1275. $this->datenextrun = dol_time_plus_duree($this->datenextrun, $this->frequency, 'm');
  1276. } else {
  1277. $this->datenextrun += ($this->frequency * $this->unitfrequency);
  1278. }
  1279. }
  1280. } else {
  1281. dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
  1282. }
  1283. // Archive job
  1284. if ($this->autodelete == 2) {
  1285. if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
  1286. || ($this->dateend && ($this->datenextrun > $this->dateend))) {
  1287. $this->status = self::STATUS_ARCHIVED;
  1288. dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
  1289. }
  1290. }
  1291. $result = $this->update($user);
  1292. if ($result < 0) {
  1293. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1294. return -1;
  1295. }
  1296. return 1;
  1297. }
  1298. /**
  1299. * Return label of status of user (active, inactive)
  1300. *
  1301. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1302. * @return string Label of status
  1303. */
  1304. public function getLibStatut($mode = 0)
  1305. {
  1306. return $this->LibStatut($this->status, $mode, $this->processing, $this->lastresult);
  1307. }
  1308. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1309. /**
  1310. * Renvoi le libelle d'un statut donne
  1311. *
  1312. * @param int $status Id statut
  1313. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  1314. * @param int $processing 0=Not running, 1=Running
  1315. * @param int $lastresult Value of last result (0=no error, error otherwise)
  1316. * @return string Label of status
  1317. */
  1318. public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
  1319. {
  1320. // phpcs:enable
  1321. $this->labelStatus = array(); // Force reset o array because label depends on other fields
  1322. $this->labelStatusShort = array();
  1323. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  1324. global $langs;
  1325. $langs->load('users');
  1326. $moretext = '';
  1327. if ($processing) {
  1328. $moretext = ' ('.$langs->trans("Running").')';
  1329. } elseif ($lastresult) {
  1330. $moretext .= ' ('.$langs->trans("Error").')';
  1331. }
  1332. $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled').$moretext;
  1333. $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled').$moretext;
  1334. $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
  1335. $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled');
  1336. }
  1337. $statusType = 'status4';
  1338. if ($status == 1 && $processing) {
  1339. $statusType = 'status1';
  1340. }
  1341. if ($status == 0) {
  1342. $statusType = 'status5';
  1343. }
  1344. if ($this->lastresult) {
  1345. $statusType = 'status8';
  1346. }
  1347. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  1348. }
  1349. }
  1350. /**
  1351. * Crob Job line class
  1352. */
  1353. class Cronjobline
  1354. {
  1355. /**
  1356. * @var int ID
  1357. */
  1358. public $id;
  1359. public $entity;
  1360. /**
  1361. * @var string Ref
  1362. */
  1363. public $ref;
  1364. public $tms = '';
  1365. public $datec = '';
  1366. /**
  1367. * @var string Cron Job Line label
  1368. */
  1369. public $label;
  1370. public $jobtype;
  1371. public $command;
  1372. public $classesname;
  1373. public $objectname;
  1374. public $methodename;
  1375. public $params;
  1376. public $md5params;
  1377. public $module_name;
  1378. public $priority;
  1379. public $datelastrun = '';
  1380. public $datenextrun = '';
  1381. public $dateend = '';
  1382. public $datestart = '';
  1383. public $datelastresult = '';
  1384. public $lastresult = '';
  1385. public $lastoutput;
  1386. public $unitfrequency;
  1387. public $frequency;
  1388. public $processing;
  1389. /**
  1390. * @var int Status
  1391. */
  1392. public $status;
  1393. /**
  1394. * @var int ID
  1395. */
  1396. public $fk_user_author;
  1397. /**
  1398. * @var int ID
  1399. */
  1400. public $fk_user_mod;
  1401. public $note;
  1402. public $note_private;
  1403. public $nbrun;
  1404. public $libname;
  1405. public $test;
  1406. /**
  1407. * Constructor
  1408. *
  1409. */
  1410. public function __construct()
  1411. {
  1412. return 1;
  1413. }
  1414. }