cron.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 - Bulk import");
  98. // Next steps:
  99. // specific arguments: 'csvfile'
  100. //
  101. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  102. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  103. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  104. {
  105. UserRights::Login($sAuthUser); // Login & set the user's language
  106. }
  107. else
  108. {
  109. $oP->p("Access wrong credentials ('$sAuthUser')");
  110. exit;
  111. }
  112. }
  113. else
  114. {
  115. $_SESSION['login_mode'] = 'basic';
  116. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  117. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  118. $oP = new WebPage("iTop - CRON");
  119. }
  120. if (!UserRights::IsAdministrator())
  121. {
  122. $oP->p("Access restricted to administrators");
  123. $oP->Output();
  124. exit;
  125. }
  126. // Enumerate classes implementing BackgroundProcess
  127. //
  128. $aBackgroundProcesses = array();
  129. foreach(get_declared_classes() as $sPHPClass)
  130. {
  131. $oRefClass = new ReflectionClass($sPHPClass);
  132. $oExtensionInstance = null;
  133. if ($oRefClass->implementsInterface('iBackgroundProcess'))
  134. {
  135. if (is_null($oExtensionInstance))
  136. {
  137. $oExecInstance = new $sPHPClass;
  138. }
  139. $aBackgroundProcesses[$sPHPClass] = $oExecInstance;
  140. }
  141. }
  142. $bVerbose = utils::ReadParam('verbose', false, true /* Allow CLI */);
  143. if ($bVerbose)
  144. {
  145. $aDisplayProcesses = array();
  146. foreach ($aBackgroundProcesses as $oExecInstance)
  147. {
  148. $aDisplayProcesses[] = get_class($oExecInstance);
  149. }
  150. $sDisplayProcesses = implode(', ', $aDisplayProcesses);
  151. $oP->p("Background processes: ".$sDisplayProcesses);
  152. }
  153. $sLockName = 'itop.cron.php';
  154. $oP->p("Starting: ".time());
  155. $res = CMDBSource::QueryToScalar("SELECT GET_LOCK('$sLockName', 1)");// timeout = 1 second (see also IS_FREE_LOCK)
  156. if (is_null($res))
  157. {
  158. // TODO - Log ?
  159. $oP->p("ERROR: Failed to acquire the lock '$sLockName'");
  160. }
  161. elseif ($res === '1')
  162. {
  163. // The current session holds the lock
  164. try
  165. {
  166. CronExec($oP, $aBackgroundProcesses, $bVerbose);
  167. }
  168. catch(Exception $e)
  169. {
  170. // TODO - Log ?
  171. $oP->p("ERROR:".$e->GetMessage());
  172. }
  173. $res = CMDBSource::QueryToScalar("SELECT RELEASE_LOCK('$sLockName')");
  174. }
  175. else
  176. {
  177. // Lock already held by another session
  178. // Exit silently
  179. $oP->p("Already running...");
  180. }
  181. $oP->p("Exiting: ".time());
  182. $oP->Output();
  183. ?>