cron.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // Known limitation - the background process periodicity is NOT taken into account
  31. function CronExec($oP, $aBackgroundProcesses, $bVerbose)
  32. {
  33. $iStarted = time();
  34. $iMaxDuration = MetaModel::GetConfig()->Get('cron_max_execution_time');
  35. $iTimeLimit = $iStarted + $iMaxDuration;
  36. if ($bVerbose)
  37. {
  38. $oP->p("Planned duration = $iMaxDuration seconds");
  39. }
  40. $iCronSleep = MetaModel::GetConfig()->Get('cron_sleep');
  41. while (time() < $iTimeLimit)
  42. {
  43. foreach ($aBackgroundProcesses as $oBackgroundProcess)
  44. {
  45. if ($bVerbose)
  46. {
  47. $oP->p("Processing asynchronous task: ".get_class($oBackgroundProcess));
  48. }
  49. $sMessage = $oBackgroundProcess->Process($iTimeLimit);
  50. if ($bVerbose && !empty($sMessage))
  51. {
  52. $oP->p("Returned: $sMessage");
  53. }
  54. }
  55. if ($bVerbose)
  56. {
  57. $oP->p("Sleeping");
  58. }
  59. sleep($iCronSleep);
  60. }
  61. if ($bVerbose)
  62. {
  63. $oP->p("Reached normal execution time limit (exceeded by ".(time()-$iTimeLimit)."s)");
  64. }
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////
  67. //
  68. // Main
  69. //
  70. if (utils::IsModeCLI())
  71. {
  72. $oP = new CLIPage("iTop - Bulk import");
  73. // Next steps:
  74. // specific arguments: 'csvfile'
  75. //
  76. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  77. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  78. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  79. {
  80. UserRights::Login($sAuthUser); // Login & set the user's language
  81. }
  82. else
  83. {
  84. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  85. exit;
  86. }
  87. }
  88. else
  89. {
  90. $_SESSION['login_mode'] = 'basic';
  91. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  92. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  93. $oP = new WebPage("iTop - CRON");
  94. }
  95. // Enumerate classes implementing BackgroundProcess
  96. //
  97. $aBackgroundProcesses = array();
  98. foreach(get_declared_classes() as $sPHPClass)
  99. {
  100. $oRefClass = new ReflectionClass($sPHPClass);
  101. $oExtensionInstance = null;
  102. if ($oRefClass->implementsInterface('iBackgroundProcess'))
  103. {
  104. if (is_null($oExtensionInstance))
  105. {
  106. $oExecInstance = new $sPHPClass;
  107. }
  108. $aBackgroundProcesses[$sPHPClass] = $oExecInstance;
  109. }
  110. }
  111. $bVerbose = utils::ReadParam('verbose', false);
  112. if ($bVerbose)
  113. {
  114. $aDisplayProcesses = array();
  115. foreach ($aBackgroundProcesses as $oExecInstance)
  116. {
  117. $aDisplayProcesses[] = get_class($oExecInstance);
  118. }
  119. $sDisplayProcesses = implode(', ', $aDisplayProcesses);
  120. $oP->p("Background processes: ".$sDisplayProcesses);
  121. }
  122. $sLockName = 'itop.cron.php';
  123. $oP->p("Starting: ".time());
  124. $res = CMDBSource::QueryToScalar("SELECT GET_LOCK('$sLockName', 1)");// timeout = 1 second (see also IS_FREE_LOCK)
  125. if (is_null($res))
  126. {
  127. // TODO - Log ?
  128. $oP->p("ERROR: Failed to acquire the lock '$sLockName'");
  129. }
  130. elseif ($res === '1')
  131. {
  132. // The current session holds the lock
  133. try
  134. {
  135. CronExec($oP, $aBackgroundProcesses, $bVerbose);
  136. }
  137. catch(Exception $e)
  138. {
  139. // TODO - Log ?
  140. $oP->p("ERROR:".$e->GetMessage());
  141. }
  142. $res = CMDBSource::QueryToScalar("SELECT RELEASE_LOCK('$sLockName')");
  143. }
  144. else
  145. {
  146. // Lock already held by another session
  147. // Exit silently
  148. $oP->p("Already running...");
  149. }
  150. $oP->p("Exiting: ".time());
  151. $oP->Output();
  152. ?>