Example #1
0
 /**
  * Launch the need cron tasks
  *
  * @param $mode   (internal/external, <0 to force)
  * @param $max    number of task to launch (default 1)
  * @param $name   of task to run (default '')
  *
  * @return the name of last task launched
  **/
 public static function launch($mode, $max = 1, $name = '')
 {
     global $CFG_GLPI;
     // No cron in maintenance mode
     if (isset($CFG_GLPI['maintenance_mode']) && $CFG_GLPI['maintenance_mode']) {
         return false;
     }
     $crontask = new self();
     $taskname = '';
     if (abs($mode) == self::MODE_EXTERNAL) {
         // If cron is launched in command line, and if memory is insufficient,
         // display a warning in the logs
         if (Toolbox::checkMemoryLimit() == 2) {
             Toolbox::logInFile('cron', __('A minimum of 64 Mio is commonly required for GLPI.') . "\n");
         }
         // If no task in CLI mode, call cron.php from command line is not really usefull ;)
         if (!countElementsInTable($crontask->getTable(), ['mode' => abs($mode)])) {
             Toolbox::logInFile('cron', __('No task with Run mode = CLI, fix your tasks configuration') . "\n");
         }
     }
     if (self::get_lock()) {
         for ($i = 1; $i <= $max; $i++) {
             $prefix = abs($mode) == self::MODE_EXTERNAL ? __('External') : __('Internal');
             if ($crontask->getNeedToRun($mode, $name)) {
                 $_SESSION["glpicronuserrunning"] = "cron_" . $crontask->fields['name'];
                 if ($plug = isPluginItemType($crontask->fields['itemtype'])) {
                     Plugin::load($plug['plugin'], true);
                 }
                 $fonction = array($crontask->fields['itemtype'], 'cron' . $crontask->fields['name']);
                 if (is_callable($fonction)) {
                     if ($crontask->start()) {
                         // Lock in DB + log start
                         $taskname = $crontask->fields['name'];
                         //TRANS: %1$s is mode (external or internal), %2$s is an order number,
                         $msgcron = sprintf(__('%1$s #%2$s'), $prefix, $i);
                         $msgcron = sprintf(__('%1$s: %2$s'), $msgcron, sprintf(__('%1$s %2$s') . "\n", __('Launch'), $crontask->fields['name']));
                         Toolbox::logInFile('cron', $msgcron);
                         $retcode = call_user_func($fonction, $crontask);
                         $crontask->end($retcode);
                         // Unlock in DB + log end
                     } else {
                         $msgcron = sprintf(__('%1$s #%2$s'), $prefix, $i);
                         $msgcron = sprintf(__('%1$s: %2$s'), $msgcron, sprintf(__('%1$s %2$s') . "\n", __("Can't start"), $crontask->fields['name']));
                         Toolbox::logInFile('cron', $msgcron);
                     }
                 } else {
                     if (is_array($fonction)) {
                         $fonction = implode('::', $fonction);
                     }
                     Toolbox::logInFile('php-errors', sprintf(__('Undefined function %s (for cron)') . "\n", $fonction));
                     $msgcron = sprintf(__('%1$s #%2$s'), $prefix, $i);
                     $msgcron = sprintf(__('%1$s: %2$s'), $msgcron, sprintf(__('%1$s %2$s') . "\n", __("Can't start"), $crontask->fields['name']));
                     Toolbox::logInFile('cron', $msgcron . "\n" . sprintf(__('Undefined function %s (for cron)') . "\n", $fonction));
                 }
             } else {
                 if ($i == 1) {
                     $msgcron = sprintf(__('%1$s #%2$s'), $prefix, $i);
                     $msgcron = sprintf(__('%1$s: %2$s'), $msgcron, __('Nothing to launch'));
                     Toolbox::logInFile('cron', $msgcron . "\n");
                 }
             }
         }
         // end for
         $_SESSION["glpicronuserrunning"] = '';
         self::release_lock();
     } else {
         Toolbox::logInFile('cron', __("Can't get DB lock") . "\n");
     }
     return $taskname;
 }
 /**
  * Launch the need cron tasks
  *
  * @param $mode (internal/external, <0 to force)
  * @param $max number of task to launch ()
  * @param $name of task to run
  *
  * @return the name of last task launched
  **/
 public static function launch($mode, $max = 1, $name = '')
 {
     $taskname = '';
     if (self::get_lock()) {
         $crontask = new self();
         for ($i = 1; $i <= $max; $i++) {
             $prefix = ($mode == self::MODE_EXTERNAL ? 'External' : 'Internal') . " #{$i}: ";
             if ($crontask->getNeedToRun($mode, $name)) {
                 $_SESSION["glpicronuserrunning"] = "cron_" . $crontask->fields['name'];
                 if ($plug = isPluginItemType($crontask->fields['itemtype'])) {
                     Plugin::load($plug['plugin'], true);
                 }
                 $fonction = array($crontask->fields['itemtype'], 'cron' . $crontask->fields['name']);
                 if (is_callable($fonction)) {
                     if ($crontask->start()) {
                         // Lock in DB + log start
                         $taskname = $crontask->fields['name'];
                         logInFile('cron', $prefix . "Launch " . $crontask->fields['name'] . "\n");
                         $retcode = call_user_func($fonction, $crontask);
                         $crontask->end($retcode);
                         // Unlock in DB + log end
                     } else {
                         logInFile('cron', $prefix . "Can't start " . $crontask->fields['name'] . "\n");
                     }
                 } else {
                     if (is_array($fonction)) {
                         $fonction = implode('::', $fonction);
                     }
                     logInFile('php-errors', "Undefined function '{$fonction}' (for cron)\n");
                     logInFile('cron', $prefix . "Can't start " . $crontask->fields['name'] . "\nUndefined function '{$fonction}'\n");
                 }
             } else {
                 if ($i == 1) {
                     logInFile('cron', $prefix . "Nothing to launch\n");
                 }
             }
         }
         // end for
         $_SESSION["glpicronuserrunning"] = '';
         self::release_lock();
     } else {
         logInFile('cron', "Can't get DB lock'\n");
     }
     return $taskname;
 }