Wts.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace easyTask;
  3. use \Closure as Closure;
  4. /**
  5. * Class Wts
  6. * @package easyTask
  7. */
  8. class Wts
  9. {
  10. /**
  11. * 进程锁
  12. * @var Lock
  13. */
  14. private $lock;
  15. /**
  16. * 进程名称列表
  17. * @var array
  18. */
  19. private $processNames = [];
  20. /**
  21. * 构造函数
  22. */
  23. public function __construct()
  24. {
  25. //创建进程锁
  26. $this->lock = new Lock();
  27. //创建进程信息文件
  28. $processFile = $this->getProcessInfoFile();
  29. if (!file_exists($processFile))
  30. {
  31. file_put_contents($processFile, '');
  32. }
  33. }
  34. /**
  35. * 注册进程名称
  36. * @param string $name
  37. */
  38. public function joinProcess($name)
  39. {
  40. $this->processNames[] = $name;
  41. $file = $this->getProcessFile($name);
  42. if (!file_exists($file))
  43. {
  44. file_put_contents($file, $name);
  45. }
  46. }
  47. /**
  48. * 获取进程文件名
  49. * @param string $name 进程名称
  50. * @return string
  51. */
  52. public function getProcessFile($name)
  53. {
  54. $runPath = Helper::getWinPath();
  55. return $runPath . md5($name) . '.win';
  56. }
  57. /**
  58. * 获取进程保存信息的文件名
  59. * @return string
  60. */
  61. public function getProcessInfoFile()
  62. {
  63. $runPath = Helper::getWinPath();
  64. $infoFile = md5(__FILE__) . '.win';
  65. return $runPath . $infoFile;
  66. }
  67. /**
  68. * 获取进程状态
  69. * @param string $name 进程名称
  70. * @return bool
  71. */
  72. public function getProcessStatus($name)
  73. {
  74. $file = $this->getProcessFile($name);
  75. if (!file_exists($file))
  76. {
  77. return false;
  78. }
  79. $fp = fopen($file, "r");
  80. if (flock($fp, LOCK_EX | LOCK_NB))
  81. {
  82. return false;
  83. }
  84. return true;
  85. }
  86. /**
  87. * 获取进程信息(非阻塞)
  88. * @return array
  89. */
  90. public function getProcessInfo()
  91. {
  92. $file = $this->getProcessInfoFile();
  93. $info = file_get_contents($file);
  94. $info = json_decode($info, true);
  95. return is_array($info) ? $info : [];
  96. }
  97. /**
  98. * 清理进程信息
  99. */
  100. public function cleanProcessInfo()
  101. {
  102. //加锁执行
  103. $this->lock->execute(function () {
  104. @file_put_contents($this->getProcessInfoFile(), '');
  105. });
  106. }
  107. /**
  108. * 保存进程信息
  109. * @param array $info
  110. */
  111. public function saveProcessInfo($info)
  112. {
  113. //加锁执行
  114. $this->lock->execute(function () use ($info) {
  115. //进程信息文件
  116. $name = $info['name'];
  117. $file = $this->getProcessInfoFile();
  118. //读取原数据
  119. $content = @file_get_contents($file);
  120. $oldInfo = $content ? json_decode($content, true) : [$name => $info];
  121. //追加数据
  122. $oldInfo ? $oldInfo[$name] = $info : $oldInfo = $info;
  123. file_put_contents($file, json_encode($oldInfo));
  124. });
  125. }
  126. /**
  127. * 分配进程
  128. * @param Closure $func
  129. * @return bool
  130. */
  131. public function allocateProcess($func)
  132. {
  133. $processNames = $this->processNames;
  134. foreach ($processNames as $name)
  135. {
  136. $file = $this->getProcessFile($name);
  137. $fp = fopen($file, 'w');
  138. if (flock($fp, LOCK_EX | LOCK_NB))
  139. {
  140. $func($name);
  141. flock($fp, LOCK_UN);
  142. return true;
  143. }
  144. fclose($fp);
  145. }
  146. return false;
  147. }
  148. }