cron.php 4.5 KB

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