Path: blob/master/src/applications/daemon/management/PhabricatorLockLogManagementWorkflow.php
12256 views
<?php12final class PhabricatorLockLogManagementWorkflow3extends PhabricatorLockManagementWorkflow {45protected function didConstruct() {6$this7->setName('log')8->setSynopsis(pht('Enable, disable, or show the lock log.'))9->setArguments(10array(11array(12'name' => 'enable',13'help' => pht('Enable the lock log.'),14),15array(16'name' => 'disable',17'help' => pht('Disable the lock log.'),18),19array(20'name' => 'name',21'param' => 'name',22'help' => pht('Review logs for a specific lock.'),23),24));25}2627public function execute(PhutilArgumentParser $args) {28$is_enable = $args->getArg('enable');29$is_disable = $args->getArg('disable');3031if ($is_enable && $is_disable) {32throw new PhutilArgumentUsageException(33pht(34'You can not both "--enable" and "--disable" the lock log.'));35}3637$with_name = $args->getArg('name');3839if ($is_enable || $is_disable) {40if (strlen($with_name)) {41throw new PhutilArgumentUsageException(42pht(43'You can not both "--enable" or "--disable" with search '.44'parameters like "--name".'));45}4647$gc = new PhabricatorDaemonLockLogGarbageCollector();48$is_enabled = (bool)$gc->getRetentionPolicy();4950$config_key = 'phd.garbage-collection';51$const = $gc->getCollectorConstant();52$value = PhabricatorEnv::getEnvConfig($config_key);5354if ($is_disable) {55if (!$is_enabled) {56echo tsprintf(57"%s\n",58pht('Lock log is already disabled.'));59return 0;60}61echo tsprintf(62"%s\n",63pht('Disabling the lock log.'));6465unset($value[$const]);66} else {67if ($is_enabled) {68echo tsprintf(69"%s\n",70pht('Lock log is already enabled.'));71return 0;72}73echo tsprintf(74"%s\n",75pht('Enabling the lock log.'));7677$value[$const] = phutil_units('24 hours in seconds');78}7980id(new PhabricatorConfigLocalSource())81->setKeys(82array(83$config_key => $value,84));8586echo tsprintf(87"%s\n",88pht('Done.'));8990echo tsprintf(91"%s\n",92pht('Restart daemons to apply changes.'));9394return 0;95}9697$table = new PhabricatorDaemonLockLog();98$conn = $table->establishConnection('r');99100$parts = array();101if (strlen($with_name)) {102$parts[] = qsprintf(103$conn,104'lockName = %s',105$with_name);106}107108if (!$parts) {109$constraint = qsprintf($conn, '1 = 1');110} else {111$constraint = qsprintf($conn, '%LA', $parts);112}113114$logs = $table->loadAllWhere(115'%Q ORDER BY id DESC LIMIT 100',116$constraint);117$logs = array_reverse($logs);118119if (!$logs) {120echo tsprintf(121"%s\n",122pht('No matching lock logs.'));123return 0;124}125126$table = id(new PhutilConsoleTable())127->setBorders(true)128->addColumn(129'id',130array(131'title' => pht('Lock'),132))133->addColumn(134'name',135array(136'title' => pht('Name'),137))138->addColumn(139'acquired',140array(141'title' => pht('Acquired'),142))143->addColumn(144'released',145array(146'title' => pht('Released'),147))148->addColumn(149'held',150array(151'title' => pht('Held'),152))153->addColumn(154'parameters',155array(156'title' => pht('Parameters'),157))158->addColumn(159'context',160array(161'title' => pht('Context'),162));163164$viewer = $this->getViewer();165166foreach ($logs as $log) {167$created = $log->getDateCreated();168$released = $log->getLockReleased();169170if ($released) {171$held = '+'.($released - $created);172} else {173$held = null;174}175176$created = phabricator_datetime($created, $viewer);177$released = phabricator_datetime($released, $viewer);178179$parameters = $log->getLockParameters();180$context = $log->getLockContext();181182$table->addRow(183array(184'id' => $log->getID(),185'name' => $log->getLockName(),186'acquired' => $created,187'released' => $released,188'held' => $held,189'parameters' => $this->flattenParameters($parameters),190'context' => $this->flattenParameters($context),191));192}193194$table->draw();195196return 0;197}198199private function flattenParameters(array $params, $keys = true) {200$flat = array();201foreach ($params as $key => $value) {202if (is_array($value)) {203$value = $this->flattenParameters($value, false);204}205if ($keys) {206$flat[] = "{$key}={$value}";207} else {208$flat[] = "{$value}";209}210}211212if ($keys) {213$flat = implode(', ', $flat);214} else {215$flat = implode(' ', $flat);216}217218return $flat;219}220221}222223224