PageRenderTime 78ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/class/SectionMaint.php

https://code.google.com/p/yapeal/
PHP | 151 lines | 83 code | 0 blank | 68 comment | 18 complexity | d29cdadae3e06d4ce00451166e6b50a1 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains Section maint class.
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: This file is part of Yet Another Php Eve Api library also know
  8. * as Yapeal which will be used to refer to it in the rest of this license.
  9. *
  10. * Yapeal is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Yapeal is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with Yapeal. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @author Michael Cummings <mgcummings@yahoo.com>
  24. * @copyright Copyright (c) 2008-2012, Michael Cummings
  25. * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
  26. * @package Yapeal
  27. * @link http://code.google.com/p/yapeal/
  28. * @link http://www.eveonline.com/
  29. */
  30. /**
  31. * @internal Allow viewing of the source code in web browser.
  32. */
  33. if (isset($_REQUEST['viewSource'])) {
  34. highlight_file(__FILE__);
  35. exit();
  36. };
  37. /**
  38. * @internal Only let this code be included.
  39. */
  40. if (count(get_included_files()) < 2) {
  41. $mess = basename(__FILE__)
  42. . ' must be included it can not be ran directly.' . PHP_EOL;
  43. if (PHP_SAPI != 'cli') {
  44. header('HTTP/1.0 403 Forbidden', TRUE, 403);
  45. die($mess);
  46. };
  47. fwrite(STDERR, $mess);
  48. exit(1);
  49. };
  50. /**
  51. * Class used to call internal maintenance scripts in Yapeal.
  52. *
  53. * @package Yapeal
  54. * @subpackage maintenance
  55. */
  56. class SectionMaint extends ASection {
  57. /**
  58. * @var array Holds the list of maintenance scripts.
  59. */
  60. //private $scriptList;
  61. /**
  62. * Constructor
  63. *
  64. */
  65. public function __construct() {
  66. $this->section = strtolower(str_replace('Section', '', __CLASS__));
  67. parent::__construct();
  68. //$this->section = strtolower(str_replace('Section', '', __CLASS__));
  69. //$path = YAPEAL_CLASS . $this->section . DS;
  70. //$knownScripts = FilterFileFinder::getStrippedFiles($path, $this->section);
  71. //$this->scriptList = array_intersect($allowedScripts, $knownScripts);
  72. }
  73. /**
  74. * Function called by Yapeal.php to start section running maintenance scripts.
  75. *
  76. * @return bool Returns TRUE if all scripts ran cleanly else FALSE.
  77. */
  78. public function pullXML() {
  79. if ($this->abort === TRUE) {
  80. return FALSE;
  81. };
  82. $scriptCount = 0;
  83. $scriptSuccess = 0;
  84. if (count($this->scriptList) == 0) {
  85. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  86. $mess = 'None of the allowed scripts are currently active for '
  87. . $this->section;
  88. Logger::getLogger('yapeal')->info($mess);
  89. };
  90. return FALSE;
  91. };
  92. // Randomize order in which scripts are tried if there is a list.
  93. if (count($this->scriptList) > 1) {
  94. shuffle($this->scriptList);
  95. };
  96. try {
  97. foreach ($this->scriptList as $script) {
  98. // If timer has expired time to run script again.
  99. if (CachedUntil::cacheExpired($script) === TRUE) {
  100. ++$scriptCount;
  101. $class = $this->section . $script;
  102. $hash = hash('sha1', $class);
  103. // These are passed on to the script class instance and used as part
  104. // of hash for lock.
  105. //$params = array();
  106. // Use lock to keep from wasting time trying to running scripts that
  107. // another Yapeal is already working on.
  108. try {
  109. $con = YapealDBConnection::connect(YAPEAL_DSN);
  110. $sql = 'select get_lock(' . $con->qstr($hash) . ',5)';
  111. if ($con->GetOne($sql) != 1) {
  112. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  113. $mess = 'Failed to get lock for ' . $class . $hash;
  114. Logger::getLogger('yapeal')->info($mess);
  115. };
  116. continue;
  117. };// if $con->GetOne($sql) ...
  118. }
  119. catch(ADODB_Exception $e) {
  120. continue;
  121. }
  122. // Give each script 60 seconds to finish. This should never happen but
  123. // is here to catch runaways.
  124. set_time_limit(60);
  125. $instance = new $class();
  126. if ($instance->doWork()) {
  127. ++$scriptSuccess;
  128. };
  129. $instance = null;
  130. };// if CachedUntil::cacheExpired...
  131. // See if Yapeal has been running for longer than 'soft' limit.
  132. if (YAPEAL_MAX_EXECUTE < time()) {
  133. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  134. $mess = 'Yapeal has been working very hard and needs a break';
  135. Logger::getLogger('yapeal')->info($mess);
  136. };
  137. exit;
  138. };// if YAPEAL_MAX_EXECUTE < time() ...
  139. };// foreach $scripts ...
  140. }
  141. catch (ADODB_Exception $e) {
  142. Logger::getLogger('yapeal')->warn($e);
  143. }
  144. // Only truly successful if all scripts ran successfully.
  145. if ($scriptCount == $scriptSuccess) {
  146. return TRUE;
  147. };
  148. return FALSE;
  149. }// function pullXML
  150. }
  151. ?>