PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/library/IO/PCNTL/class.JobLauncher.php

https://github.com/jasherai/libwebta
PHP | 133 lines | 100 code | 7 blank | 26 comment | 5 complexity | 7b386ef72b89c1a85c8527cc92fa3b0d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?
  2. /**
  3. * This file is a part of LibWebta, PHP class library.
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 2 of the GPL license,
  8. * that is bundled with this package in the file license.txt and is
  9. * available through the world-wide-web at the following url:
  10. * http://www.gnu.org/copyleft/gpl.html
  11. *
  12. * @category LibWebta
  13. * @package IO
  14. * @subpackage PCNTL
  15. * @copyright Copyright (c) 2003-2007 Webta Inc, http://www.gnu.org/licenses/gpl.html
  16. * @license http://www.gnu.org/licenses/gpl.html
  17. */
  18. Core::Load("System/Independent/Shell/class.Getopt.php");
  19. /**
  20. * @name JobLauncher
  21. * @category LibWebta
  22. * @package IO
  23. * @subpackage PCNTL
  24. * @version 1.0
  25. * @author Igor Savchenko <http://webta.net/company.html>
  26. * @example tests.php
  27. * @see tests.php
  28. */
  29. class JobLauncher extends Core
  30. {
  31. private $ProcessName;
  32. private $Logger;
  33. private $PIDDir;
  34. function __construct($process_classes_folder)
  35. {
  36. $processes = @glob("{$process_classes_folder}/class.*Process.php");
  37. $this->Logger = LoggerManager::getLogger('JobLauncher');
  38. $jobs = array();
  39. if (count($processes) > 0)
  40. {
  41. foreach ($processes as $process)
  42. {
  43. $filename = basename($process);
  44. $directory = dirname($process);
  45. Core::Load($filename, $directory);
  46. preg_match("/class.(.*)Process.php/s", $filename, $tmp);
  47. $process_name = $tmp[1];
  48. if (class_exists("{$process_name}Process"))
  49. {
  50. $reflect = new ReflectionClass("{$process_name}Process");
  51. if ($reflect)
  52. {
  53. if ($reflect->implementsInterface("IProcess"))
  54. {
  55. $job = array(
  56. "name" => $process_name,
  57. "description" => $reflect->getProperty("ProcessDescription")->getValue($reflect->newInstance())
  58. );
  59. array_push($jobs, $job);
  60. }
  61. else
  62. Core::RaiseError("Class '{$process_name}Process' doesn't implement 'IProcess' interface.", E_ERROR);
  63. }
  64. else
  65. Core::RaiseError("Cannot use ReflectionAPI for class '{$process_name}Process'", E_ERROR);
  66. }
  67. else
  68. Core::RaiseError("'{$process}' does not contain definition for '{$process_name}Process'", E_ERROR);
  69. }
  70. }
  71. else
  72. Core::RaiseError(_("No job classes found in {$ProcessClassesFolder}"), E_ERROR);
  73. $options = array();
  74. foreach($jobs as $job)
  75. $options[$job["name"]] = $job["description"];
  76. $options["help"] = "Print this help";
  77. $options["piddir=s"] = "PID directory";
  78. $Getopt = new Getopt($options);
  79. $opts = $Getopt->getOptions();
  80. if (in_array("help", $opts) || count($opts) == 0 || !$options[$opts[0]])
  81. {
  82. print $Getopt->getUsageMessage();
  83. exit();
  84. }
  85. else
  86. {
  87. $this->ProcessName = $opts[0];
  88. if (in_array("piddir", $opts))
  89. {
  90. $piddir = trim($Getopt->getOption("piddir"));
  91. if (substr($piddir, 0, 1) != '/')
  92. $this->PIDDir = realpath($process_classes_folder . "/" . $piddir);
  93. else
  94. $this->PIDDir = $piddir;
  95. }
  96. }
  97. }
  98. /**
  99. * Return Process name
  100. *
  101. * @return string
  102. */
  103. function GetProcessName()
  104. {
  105. return $this->ProcessName;
  106. }
  107. function Launch($max_chinds = 5, $child_exec_timeout = 0)
  108. {
  109. $proccess = new ReflectionClass("{$this->ProcessName}Process");
  110. $sig_handler = new ReflectionClass("SignalHandler");
  111. $PR = new ProcessManager($sig_handler->newInstance());
  112. $PR->SetChildExecLimit($child_exec_timeout);
  113. $PR->SetMaxChilds($max_chinds);
  114. if ($this->PIDDir)
  115. $PR->SetPIDDir($this->PIDDir);
  116. $PR->Run($proccess->newInstance());
  117. }
  118. }
  119. ?>