Store.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. namespace catcher\library\crontab;
  12. trait Store
  13. {
  14. /**
  15. * 存储 pid
  16. *
  17. * @time 2020年07月05日
  18. * @param $pid
  19. * @return false|int
  20. */
  21. public function storeMasterPid($pid)
  22. {
  23. $path = $this->getMasterPidPath();
  24. return file_put_contents($path, $pid);
  25. }
  26. /**
  27. * 清除退出的 worker 信息
  28. *
  29. * @time 2020年07月08日
  30. * @param $pid
  31. * @return void
  32. */
  33. protected function unsetWorkerStatus($pid)
  34. {
  35. $this->table->del($this->getColumnKey($pid));
  36. }
  37. /**
  38. * 输出
  39. *
  40. * @time 2020年07月07日
  41. * @return false|string
  42. */
  43. public function output()
  44. {
  45. // 等待信号输出
  46. sleep(1);
  47. return $this->getProcessStatusInfo();
  48. }
  49. /**
  50. * 获取 pid
  51. *
  52. * @time 2020年07月05日
  53. * @return int
  54. */
  55. public function getMasterPid()
  56. {
  57. $pid = file_get_contents($this->getMasterPidPath());
  58. return intval($pid);
  59. }
  60. /**
  61. * 获取配置地址
  62. *
  63. * @time 2020年07月05日
  64. * @return string
  65. */
  66. protected function getMasterPidPath()
  67. {
  68. return config('catch.schedule.master_pid_file');
  69. }
  70. /**
  71. * 创建任务调度文件夹
  72. *
  73. * @time 2020年07月09日
  74. * @return string
  75. */
  76. protected function schedulePath()
  77. {
  78. $path = config('catch.schedule.store_path');
  79. if (!is_dir($path)) {
  80. mkdir($path, 0777, true);
  81. }
  82. return $path;
  83. }
  84. /**
  85. * 进程状态文件
  86. *
  87. * @time 2020年07月09日
  88. * @return string
  89. */
  90. protected function getSaveProcessStatusFile()
  91. {
  92. return $this->schedulePath() . '.worker-status';
  93. }
  94. /**
  95. * 保存进程状态
  96. *
  97. * @time 2020年07月09日
  98. * @return void
  99. */
  100. protected function saveProcessStatus()
  101. {
  102. file_put_contents($this->getSaveProcessStatusFile(), $this->renderProcessesStatusToString());
  103. }
  104. /**
  105. * 获取进程状态
  106. *
  107. * @time 2020年07月09日
  108. * @return false|string
  109. */
  110. protected function getProcessStatusInfo()
  111. {
  112. return file_get_contents($this->getSaveProcessStatusFile());
  113. }
  114. }