cron.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Heart beat of the application (process asynchron tasks such as broadcasting email)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. echo "coucou\n";
  25. if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
  26. require_once(__DIR__.'/../approot.inc.php');
  27. echo "coucou\n";
  28. require_once(APPROOT.'/application/application.inc.php');
  29. echo "coucou\n";
  30. require_once(APPROOT.'/application/nicewebpage.class.inc.php');
  31. echo "coucou\n";
  32. require_once(APPROOT.'/application/webpage.class.inc.php');
  33. echo "coucou\n";
  34. require_once(APPROOT.'/application/clipage.class.inc.php');
  35. echo "apres cli page\n";
  36. require_once(APPROOT.'/application/startup.inc.php');
  37. echo "apres startup\n";
  38. function ReadMandatoryParam($oP, $sParam)
  39. {
  40. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */);
  41. if (is_null($sValue))
  42. {
  43. $oP->p("ERROR: Missing argument '$sParam'\n");
  44. UsageAndExit($oP);
  45. }
  46. return trim($sValue);
  47. }
  48. function UsageAndExit($oP)
  49. {
  50. $bModeCLI = utils::IsModeCLI();
  51. if ($bModeCLI)
  52. {
  53. $oP->p("USAGE:\n");
  54. $oP->p("php -q cron.php --auth_user=<login> --auth_pwd=<password> [--verbose=1]\n");
  55. }
  56. else
  57. {
  58. $oP->p("Optional parameter: verbose\n");
  59. }
  60. $oP->output();
  61. exit -2;
  62. }
  63. // Known limitation - the background process periodicity is NOT taken into account
  64. function CronExec($oP, $aBackgroundProcesses, $bVerbose)
  65. {
  66. $iStarted = time();
  67. $iMaxDuration = MetaModel::GetConfig()->Get('cron_max_execution_time');
  68. $iTimeLimit = $iStarted + $iMaxDuration;
  69. if ($bVerbose)
  70. {
  71. $oP->p("Planned duration = $iMaxDuration seconds");
  72. }
  73. $iCronSleep = MetaModel::GetConfig()->Get('cron_sleep');
  74. while (time() < $iTimeLimit)
  75. {
  76. foreach ($aBackgroundProcesses as $oBackgroundProcess)
  77. {
  78. if ($bVerbose)
  79. {
  80. $oP->p("Processing asynchronous task: ".get_class($oBackgroundProcess));
  81. }
  82. $sMessage = $oBackgroundProcess->Process($iTimeLimit);
  83. if ($bVerbose && !empty($sMessage))
  84. {
  85. $oP->p("Returned: $sMessage");
  86. }
  87. }
  88. if ($bVerbose)
  89. {
  90. $oP->p("Sleeping");
  91. }
  92. sleep($iCronSleep);
  93. }
  94. if ($bVerbose)
  95. {
  96. $oP->p("Reached normal execution time limit (exceeded by ".(time()-$iTimeLimit)."s)");
  97. }
  98. }
  99. ////////////////////////////////////////////////////////////////////////////////
  100. //
  101. // Main
  102. //
  103. if (utils::IsModeCLI())
  104. {
  105. $oP = new CLIPage("iTop - CRON");
  106. }
  107. else
  108. {
  109. $oP = new WebPage("iTop - CRON");
  110. }
  111. try
  112. {
  113. utils::UseParamFile();
  114. }
  115. catch(Exception $e)
  116. {
  117. $oP->p("Error: ".$e->GetMessage());
  118. $oP->output();
  119. exit -2;
  120. }
  121. if (utils::IsModeCLI())
  122. {
  123. // Next steps:
  124. // specific arguments: 'csvfile'
  125. //
  126. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  127. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  128. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  129. {
  130. UserRights::Login($sAuthUser); // Login & set the user's language
  131. }
  132. else
  133. {
  134. $oP->p("Access wrong credentials ('$sAuthUser')");
  135. $oP->output();
  136. exit -1;
  137. }
  138. }
  139. else
  140. {
  141. $_SESSION['login_mode'] = 'basic';
  142. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  143. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  144. }
  145. if (!UserRights::IsAdministrator())
  146. {
  147. $oP->p("Access restricted to administrators");
  148. $oP->Output();
  149. exit -1;
  150. }
  151. // Enumerate classes implementing BackgroundProcess
  152. //
  153. $aBackgroundProcesses = array();
  154. foreach(get_declared_classes() as $sPHPClass)
  155. {
  156. $oRefClass = new ReflectionClass($sPHPClass);
  157. $oExtensionInstance = null;
  158. if ($oRefClass->implementsInterface('iBackgroundProcess'))
  159. {
  160. if (is_null($oExtensionInstance))
  161. {
  162. $oExecInstance = new $sPHPClass;
  163. }
  164. $aBackgroundProcesses[$sPHPClass] = $oExecInstance;
  165. }
  166. }
  167. $bVerbose = utils::ReadParam('verbose', false, true /* Allow CLI */);
  168. if ($bVerbose)
  169. {
  170. $aDisplayProcesses = array();
  171. foreach ($aBackgroundProcesses as $oExecInstance)
  172. {
  173. $aDisplayProcesses[] = get_class($oExecInstance);
  174. }
  175. $sDisplayProcesses = implode(', ', $aDisplayProcesses);
  176. $oP->p("Background processes: ".$sDisplayProcesses);
  177. }
  178. $sLockName = 'itop.cron.php';
  179. $oP->p("Starting: ".time());
  180. $res = CMDBSource::QueryToScalar("SELECT GET_LOCK('$sLockName', 1)");// timeout = 1 second (see also IS_FREE_LOCK)
  181. if (is_null($res))
  182. {
  183. // TODO - Log ?
  184. $oP->p("ERROR: Failed to acquire the lock '$sLockName'");
  185. }
  186. elseif ($res === '1')
  187. {
  188. // The current session holds the lock
  189. try
  190. {
  191. CronExec($oP, $aBackgroundProcesses, $bVerbose);
  192. }
  193. catch(Exception $e)
  194. {
  195. // TODO - Log ?
  196. $oP->p("ERROR:".$e->GetMessage());
  197. }
  198. $res = CMDBSource::QueryToScalar("SELECT RELEASE_LOCK('$sLockName')");
  199. }
  200. else
  201. {
  202. // Lock already held by another session
  203. // Exit silently
  204. $oP->p("Already running...");
  205. }
  206. $oP->p("Exiting: ".time());
  207. $oP->Output();
  208. ?>