|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorWorkerTriggerManagementFireWorkflow |
| 4 | + extends PhabricatorWorkerTriggerManagementWorkflow { |
| 5 | + |
| 6 | + protected function didConstruct() { |
| 7 | + $this |
| 8 | + ->setName('fire') |
| 9 | + ->setExamples('**fire** --id __id__') |
| 10 | + ->setSynopsis( |
| 11 | + pht( |
| 12 | + 'Activates selected triggers, firing them immediately.')) |
| 13 | + ->setArguments( |
| 14 | + array_merge( |
| 15 | + array( |
| 16 | + array( |
| 17 | + 'name' => 'now', |
| 18 | + 'param' => 'time', |
| 19 | + 'help' => pht( |
| 20 | + 'Fire the trigger as though the current time is a given '. |
| 21 | + 'time. This allows you to test how a trigger would behave '. |
| 22 | + 'if activated in the past or future. Defaults to the actual '. |
| 23 | + 'current time.'), |
| 24 | + ), |
| 25 | + array( |
| 26 | + 'name' => 'last', |
| 27 | + 'param' => 'time', |
| 28 | + 'help' => pht( |
| 29 | + 'Fire the trigger as though the last event occurred at a '. |
| 30 | + 'given time. Defaults to the actual last event time.'), |
| 31 | + ), |
| 32 | + array( |
| 33 | + 'name' => 'next', |
| 34 | + 'param' => 'time', |
| 35 | + 'help' => pht( |
| 36 | + 'Fire the trigger as though the next event was scheduled '. |
| 37 | + 'at a given time. Defaults to the actual time when the '. |
| 38 | + 'event is next scheduled to fire.'), |
| 39 | + ), |
| 40 | + ), |
| 41 | + $this->getTriggerSelectionArguments())); |
| 42 | + } |
| 43 | + |
| 44 | + public function execute(PhutilArgumentParser $args) { |
| 45 | + $console = PhutilConsole::getConsole(); |
| 46 | + $viewer = $this->getViewer(); |
| 47 | + $triggers = $this->loadTriggers($args); |
| 48 | + |
| 49 | + $now = $args->getArg('now'); |
| 50 | + $now = $this->parseTime($now); |
| 51 | + if (!$now) { |
| 52 | + $now = PhabricatorTime::getNow(); |
| 53 | + } |
| 54 | + |
| 55 | + PhabricatorTime::pushTime($now, date_default_timezone_get()); |
| 56 | + |
| 57 | + $console->writeOut( |
| 58 | + "%s\n", |
| 59 | + pht( |
| 60 | + 'Set current time to %s.', |
| 61 | + phabricator_datetime(PhabricatorTime::getNow(), $viewer))); |
| 62 | + |
| 63 | + $last_time = $this->parseTime($args->getArg('last')); |
| 64 | + $next_time = $this->parseTime($args->getArg('next')); |
| 65 | + |
| 66 | + PhabricatorWorker::setRunAllTasksInProcess(true); |
| 67 | + |
| 68 | + foreach ($triggers as $trigger) { |
| 69 | + $console->writeOut( |
| 70 | + "%s\n", |
| 71 | + pht('Executing trigger %s.', $this->describeTrigger($trigger))); |
| 72 | + |
| 73 | + $event = $trigger->getEvent(); |
| 74 | + if ($event) { |
| 75 | + if (!$last_time) { |
| 76 | + $last_time = $event->getLastEventEpoch(); |
| 77 | + } |
| 78 | + if (!$next_time) { |
| 79 | + $next_time = $event->getNextEventEpoch(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (!$next_time) { |
| 84 | + $console->writeOut( |
| 85 | + "%s\n", |
| 86 | + pht( |
| 87 | + 'Trigger is not scheduled to execute. Use --at to simluate '. |
| 88 | + 'a scheduled event.')); |
| 89 | + continue; |
| 90 | + } else { |
| 91 | + $console->writeOut( |
| 92 | + "%s\n", |
| 93 | + pht( |
| 94 | + 'Executing event as though it was scheduled to execute at %s.', |
| 95 | + phabricator_datetime($next_time, $viewer))); |
| 96 | + } |
| 97 | + |
| 98 | + if (!$last_time) { |
| 99 | + $console->writeOut( |
| 100 | + "%s\n", |
| 101 | + pht( |
| 102 | + 'Executing event as though it never previously executed.')); |
| 103 | + } else { |
| 104 | + $console->writeOut( |
| 105 | + "%s\n", |
| 106 | + pht( |
| 107 | + 'Executing event as though it previously executed at %s.', |
| 108 | + phabricator_datetime($last_time, $viewer))); |
| 109 | + } |
| 110 | + |
| 111 | + $trigger->executeTrigger($last_time, $next_time); |
| 112 | + |
| 113 | + $reschedule_time = $trigger->getNextEventEpoch( |
| 114 | + $next_time, |
| 115 | + $is_reschedule = true); |
| 116 | + |
| 117 | + if (!$reschedule_time) { |
| 118 | + $console->writeOut( |
| 119 | + "%s\n", |
| 120 | + pht( |
| 121 | + 'After executing under these conditions, this event would never '. |
| 122 | + 'execute again.')); |
| 123 | + } else { |
| 124 | + $console->writeOut( |
| 125 | + "%s\n", |
| 126 | + pht( |
| 127 | + 'After executing under these conditions, this event would '. |
| 128 | + 'next execute at %s.', |
| 129 | + phabricator_datetime($reschedule_time, $viewer))); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return 0; |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments