1<?php
2
3final class PhagePlanAction
4  extends PhageAction {
5
6  public function isContainerAction() {
7    return true;
8  }
9
10  protected function willAddAction(PhageAction $action) {
11    if (!($action instanceof PhageAgentAction)) {
12      throw new Exception(
13        pht('Only agent actions may be added to a plan.'));
14    }
15  }
16
17  public function executePlan() {
18    $agents = $this->getAgents();
19    foreach ($agents as $agent) {
20      $agent->startAgent();
21    }
22
23    while (true) {
24      $channels = $this->getAllWaitingChannels();
25      PhutilChannel::waitForAny($channels);
26
27      $agents = $this->getActiveAgents();
28      if (!$agents) {
29        break;
30      }
31
32      foreach ($agents as $agent) {
33        $agent->updateAgent();
34      }
35    }
36  }
37
38  protected function getAgents() {
39    return $this->getActions();
40  }
41
42  protected function getActiveAgents() {
43    $agents = $this->getAgents();
44
45    foreach ($agents as $key => $agent) {
46      if (!$agent->isActiveAgent()) {
47        unset($agents[$key]);
48      }
49    }
50
51    return $agents;
52  }
53
54
55}
56