HostManager.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\host;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Support\Facades\Redis;
  5. class HostManager
  6. {
  7. /**
  8. * @return Host
  9. */
  10. public function localhost()
  11. {
  12. return new Host(gethostname());
  13. }
  14. /**
  15. * @return mixed
  16. */
  17. public function hostnames()
  18. {
  19. $hostnames = Redis::hkeys('hosts');
  20. foreach ($hostnames as $key => $hostname) {
  21. if (!Redis::hkeys('forkPids:' . $hostname)) {
  22. unset($hostnames[$key]);
  23. }
  24. }
  25. return $hostnames;
  26. }
  27. public function hosts()
  28. {
  29. $hosts = [];
  30. foreach ($this->hostnames() as $hostname) {
  31. $hosts[] = new Host($hostname);
  32. }
  33. return $hosts;
  34. }
  35. public function pidkeys()
  36. {
  37. $result = [];
  38. foreach ($this->hosts() as $host) {
  39. /**
  40. * @var Host $host
  41. */
  42. $result = array_merge($result, $host->pidKeys());
  43. }
  44. return $result;
  45. }
  46. public function pidKey()
  47. {
  48. return $this->localhost()->pidKeys();
  49. }
  50. public function show()
  51. {
  52. $result = new Collection();
  53. foreach ($this->hosts() as $host) {
  54. /**
  55. * @var Host $host
  56. */
  57. $result[$host->hostName] = $host->show();
  58. }
  59. return $result;
  60. }
  61. public function command($command)
  62. {
  63. foreach ($this->hosts() as $host) {
  64. /**
  65. * @var Host $host
  66. */
  67. Redis::lpush('daemon:commands:' . $host->hostName, $command);
  68. }
  69. }
  70. public function logout($hostname){
  71. Redis::hdel('hosts', $hostname);
  72. }
  73. public function restartTime(){
  74. return Redis::get('daemonRestart');
  75. }
  76. public function restart(){
  77. return Redis::set('daemonRestart',time());
  78. }
  79. public function refresh(){
  80. $this->localhost()->clearPids();
  81. $this->localhost()->killAll();
  82. }
  83. }