Hoa central
Status.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Worker\Bin;
38 
39 use Hoa\Console;
40 use Hoa\Core;
41 use Hoa\Worker;
42 
52 {
58  protected $options = [
60  ['help', Console\GetOption::NO_ARGUMENT, 'h'],
61  ['help', Console\GetOption::NO_ARGUMENT, '?']
62  ];
63 
64 
65 
71  public function main()
72  {
73  $run = 'hoa://Data/Variable/Run';
74 
75  while (false !== $c = $this->getOption($v)) {
76  switch ($c) {
77  case 'r':
78  $run = $v;
79 
80  break;
81 
82  case 'h':
83  case '?':
84  return $this->usage();
85 
86  break;
87 
88  case '__ambiguous':
89  $this->resolveOptionAmbiguity($v);
90 
91  break;
92  }
93  }
94 
95  $run = resolve($run);
96  Core::getInstance()->initialize([
97  'protocol.Data/Variable/Run' => "\r" . $run . DS
98  ]);
99  $outi = [['ID', 'PID', 'Socket', 'Uptime', 'Messages', 'Last']];
100  $outm = [];
101  $now = new \DateTime();
102  $t = 0;
103 
105  'Shared worker informations',
106  'foreground(yellow)'
107  ), "\n\n";
108 
109  foreach (glob($run . DS . '*.wid') as $wid) {
110  $worker = new Worker\Shared(substr(basename($wid), 0, -4));
111  $infos = $worker->getInformations();
112  $uptime = new \DateTime();
113  $uptime->setTimestamp((int) $infos['start']);
114  $last = new \DateTime();
115  $last->setTimestamp((int) $infos['last_message']);
116 
117  $outi[] = [
118  $infos['id'],
119  $infos['pid'],
120  $infos['socket'],
121  $uptime->diff($now)->format('%ad%H:%I:%S'),
122  $infos['messages'],
123  0 === $infos['last_message']
124  ? '-'
125  : $last->diff($now)->format('%ad%H:%I:%S')
126  ];
127 
128  $outm[] = $infos;
129 
130  ++$t;
131  }
132 
133  echo Console\Chrome\Text::columnize($outi, 0, 1, '|'), "\n";
134 
135  $max_id = 0;
136  $max_peak = 0;
137 
138  foreach ($outm as $m) {
139  $max_id < strlen($m['id'])
140  and $max_id = strlen($m['id']);
141 
142  $max_peak < $m['memory_peak']
143  and $max_peak = $m['memory_peak'];
144  }
145 
146  foreach ($outm as $m) {
147  $outmm = str_pad($m['id'], $max_id) . ' ';
148  $max = (int) (($m['memory_peak'] * 39) / $max_peak);
149  $peak = (int) (($m['memory_allocated_peak'] * 40) / $max_peak);
150  $memory = (int) (($m['memory_allocated'] * 40) / $max_peak);
151 
152  for ($i = 0; $i < $memory - 1; ++$i) {
154  '|',
155  'foreground(green)'
156  );
157  }
158 
159  for (; $i < $peak; ++$i) {
161  '|',
162  'foreground(yellow)'
163  );
164  }
165 
166  for (; $i < $max; ++$i) {
167  $outmm .= ' ';
168  }
169 
171  '|',
172  'foreground(red)'
173  );
174 
175  for (++$i; $i < 40; ++$i) {
176  $outmm .= ' ';
177  }
178 
179  $outmm .=
180  ' ' .
182  number_format($m['memory_allocated'] / 1024) . 'Kb',
183  'foreground(green)'
184  ) . ' ' .
186  number_format($m['memory_allocated_peak'] / 1024) . 'Kb',
187  'foreground(yellow)'
188  ) . ' ' .
190  number_format($m['memory_peak'] / 1024) . 'Kb',
191  'foreground(red)'
192  );
193 
194  echo $outmm . "\n";
195  }
196 
197  echo
198  "\n", $t,
199  ' shared worker', ($t > 1 ? 's are' : ' is'), ' running.', "\n";
200 
201  return;
202  }
203 
209  public function usage()
210  {
211  echo
212  'Usage : worker:status <options>', "\n",
213  'Options :', "\n",
214  $this->makeUsageOptionsList([
215  'run' => 'Define hoa://Data/Variable/Run/ path.',
216  'help' => 'This help.'
217  ]), "\n",
218  'Legend: ',
220  'allocated',
221  'foreground(green)'
222  ), ', ',
224  'allocated peak',
225  'foreground(yellow)'
226  ), ', ',
228  'peak',
229  'foreground(red)'
230  ), '.', "\n";
231 
232  return;
233  }
234 }
235 
236 __halt_compiler();
237 Get status of all workers.
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149
static getInstance()
Definition: Core.php:193
static columnize(Array $line, $alignement=self::ALIGN_LEFT, $horizontalPadding=2, $verticalPadding=0, $separator=null)
Definition: Text.php:126
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190
static colorize($text, $attributesBefore, $attributesAfter= 'normal')
Definition: Text.php:83