cron.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. require_once('../approot.inc.php');
  25. require_once(APPROOT.'/application/application.inc.php');
  26. require_once(APPROOT.'/application/nicewebpage.class.inc.php');
  27. require_once(APPROOT.'/application/webpage.class.inc.php');
  28. require_once(APPROOT.'/application/clipage.class.inc.php');
  29. require_once(APPROOT.'/application/startup.inc.php');
  30. function ReadMandatoryParam($oP, $sParam)
  31. {
  32. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */);
  33. if (is_null($sValue))
  34. {
  35. $oP->p("ERROR: Missing argument '$sParam'\n");
  36. UsageAndExit($oP);
  37. }
  38. return trim($sValue);
  39. }
  40. function UsageAndExit($oP)
  41. {
  42. $bModeCLI = utils::IsModeCLI();
  43. if ($bModeCLI)
  44. {
  45. $oP->p("USAGE:\n");
  46. $oP->p("php -q cron.php --auth_user=<login> --auth_pwd=<password> [--verbose=1]\n");
  47. }
  48. else
  49. {
  50. $oP->p("Optional parameter: verbose\n");
  51. }
  52. $oP->output();
  53. exit -2;
  54. }
  55. // Known limitation - the background process periodicity is NOT taken into account
  56. function CronExec($oP, $aBackgroundProcesses, $bVerbose)
  57. {
  58. $iStarted = time();
  59. $iMaxDuration = MetaModel::GetConfig()->Get('cron_max_execution_time');
  60. $iTimeLimit = $iStarted + $iMaxDuration;
  61. if ($bVerbose)
  62. {
  63. $oP->p("Planned duration = $iMaxDuration seconds");
  64. }
  65. $iCronSleep = MetaModel::GetConfig()->Get('cron_sleep');
  66. while (time() < $iTimeLimit)
  67. {
  68. foreach ($aBackgroundProcesses as $oBackgroundProcess)
  69. {
  70. if ($bVerbose)
  71. {
  72. $oP->p("Processing asynchronous task: ".get_class($oBackgroundProcess));
  73. }
  74. $sMessage = $oBackgroundProcess->Process($iTimeLimit);
  75. if ($bVerbose && !empty($sMessage))
  76. {
  77. $oP->p("Returned: $sMessage");
  78. }
  79. }
  80. if ($bVerbose)
  81. {
  82. $oP->p("Sleeping");
  83. }
  84. sleep($iCronSleep);
  85. }
  86. if ($bVerbose)
  87. {
  88. $oP->p("Reached normal execution time limit (exceeded by ".(time()-$iTimeLimit)."s)");
  89. }
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////
  92. //
  93. // Main
  94. //
  95. if (utils::IsModeCLI())
  96. {
  97. $oP = new CLIPage("iTop - CRON");
  98. }
  99. else
  100. {
  101. $oP = new WebPage("iTop - CRON");
  102. }
  103. try
  104. {
  105. utils::UseParamFile();
  106. }
  107. catch(Exception $e)
  108. {
  109. $oP->p("Error: ".$e->GetMessage());
  110. $oP->output();
  111. exit -2;
  112. }
  113. if (utils::IsModeCLI())
  114. {
  115. // Next steps:
  116. // specific arguments: 'csvfile'
  117. //
  118. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  119. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  120. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  121. {
  122. UserRights::Login($sAuthUser); // Login & set the user's language
  123. }
  124. else
  125. {
  126. $oP->p("Access wrong credentials ('$sAuthUser')");
  127. $oP->output();
  128. exit -1;
  129. }
  130. }
  131. else
  132. {
  133. $_SESSION['login_mode'] = 'basic';
  134. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  135. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  136. }
  137. if (!UserRights::IsAdministrator())
  138. {
  139. $oP->p("Access restricted to administrators");
  140. $oP->Output();
  141. exit -1;
  142. }
  143. // Enumerate classes implementing BackgroundProcess
  144. //
  145. $aBackgroundProcesses = array();
  146. foreach(get_declared_classes() as $sPHPClass)
  147. {
  148. $oRefClass = new ReflectionClass($sPHPClass);
  149. $oExtensionInstance = null;
  150. if ($oRefClass->implementsInterface('iBackgroundProcess'))
  151. {
  152. if (is_null($oExtensionInstance))
  153. {
  154. $oExecInstance = new $sPHPClass;
  155. }
  156. $aBackgroundProcesses[$sPHPClass] = $oExecInstance;
  157. }
  158. }
  159. $bVerbose = utils::ReadParam('verbose', false, true /* Allow CLI */);
  160. if ($bVerbose)
  161. {
  162. $aDisplayProcesses = array();
  163. foreach ($aBackgroundProcesses as $oExecInstance)
  164. {
  165. $aDisplayProcesses[] = get_class($oExecInstance);
  166. }
  167. $sDisplayProcesses = implode(', ', $aDisplayProcesses);
  168. $oP->p("Background processes: ".$sDisplayProcesses);
  169. }
  170. $sLockName = 'itop.cron.php';
  171. $oP->p("Starting: ".time());
  172. $res = CMDBSource::QueryToScalar("SELECT GET_LOCK('$sLockName', 1)");// timeout = 1 second (see also IS_FREE_LOCK)
  173. if (is_null($res))
  174. {
  175. // TODO - Log ?
  176. $oP->p("ERROR: Failed to acquire the lock '$sLockName'");
  177. }
  178. elseif ($res === '1')
  179. {
  180. // The current session holds the lock
  181. try
  182. {
  183. CronExec($oP, $aBackgroundProcesses, $bVerbose);
  184. }
  185. catch(Exception $e)
  186. {
  187. // TODO - Log ?
  188. $oP->p("ERROR:".$e->GetMessage());
  189. }
  190. $res = CMDBSource::QueryToScalar("SELECT RELEASE_LOCK('$sLockName')");
  191. }
  192. else
  193. {
  194. // Lock already held by another session
  195. // Exit silently
  196. $oP->p("Already running...");
  197. }
  198. $oP->p("Exiting: ".time());
  199. $oP->Output();
  200. ?>