Skip to content

Commit

Permalink
Ensure that 'time' is always set in the result of any collection buil…
Browse files Browse the repository at this point in the history
…der. Remove the 's' from the return value of Result::getExecutionTime(). Add a duration formatter.
  • Loading branch information
greg-1-anderson committed Aug 4, 2016
1 parent 4caf74e commit 8ba0005
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ public function fail()
*/
public function complete()
{
$this->detatchProgressIndicator();
$this->runTaskListIgnoringFailures($this->completionStack);
$this->reset();
return $this;
Expand Down
15 changes: 15 additions & 0 deletions src/Collection/CollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Robo\Config;
use Robo\Common\IO;
use Robo\Common\Timer;
use Psr\Log\LogLevel;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
Expand All @@ -22,6 +23,7 @@ class CollectionBuilder implements NestedCollectionInterface, ConfigAwareInterfa
use ConfigAwareTrait;
use ContainerAwareTrait;
use LoadAllTasks;
use Timer;

protected $collection;
protected $currentTask;
Expand Down Expand Up @@ -240,6 +242,19 @@ protected function fixTask($service, $args)
* When we run the collection builder, run everything in the collection.
*/
public function run()
{
$this->startTimer();
$result = $this->runTasks();
$this->stopTimer();
$result['time'] = $this->getExecutionTime();
return $result;
}

/**
* If there is a single task, run it; if there is a collection, run
* all of its tasks.
*/
protected function runTasks()
{
if (!$this->collection && $this->currentTask) {
return $this->currentTask->run();
Expand Down
5 changes: 5 additions & 0 deletions src/Common/ProgressIndicatorAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ protected function disableProgressIndicator()
$this->progressIndicator->disableProgressIndicator();
}

protected function detatchProgressIndicator()
{
$this->setProgressIndicator(null);
}

protected function advanceProgressIndicator($steps = 1)
{
if (!$this->progressIndicator) {
Expand Down
21 changes: 21 additions & 0 deletions src/Common/TimeKeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

class TimeKeeper
{
const MINUTE = 60;
const HOUR = 3600;
const DAY = 86400;

protected $startedAt;
protected $finishedAt;

Expand All @@ -28,4 +32,21 @@ public function elapsed()
}
return $finished - $this->startedAt;
}

public static function formatDuration($duration)
{
if ($duration >= self::DAY * 2) {
return gmdate('z \d\a\y\s H:i:s', $duration);
}
if ($duration > self::DAY) {
return gmdate('\1 \d\a\y H:i:s', $duration);
}
if ($duration > self::HOUR) {
return gmdate("H:i:s", $duration);
}
if ($duration > self::MINUTE) {
return gmdate("i:s", $duration);
}
return round($duration, 3).'s';
}
}
2 changes: 1 addition & 1 deletion src/Common/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function stopTimer()
protected function getExecutionTime()
{
if (!isset($this->timer)) {
return 0;
return null;
}
return $this->timer->elapsed();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Log/RoboLogStyle.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Robo\Log;

use Robo\Common\TimeKeeper;
use Consolidation\Log\LogOutputStyler;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\OutputStyle;
Expand Down Expand Up @@ -46,7 +47,8 @@ protected function formatMessage($label, $message, $context, $taskNameStyle, $me
$message = parent::formatMessage($label, $message, $context, $taskNameStyle, $messageStyle);

if (array_key_exists('time', $context) && !empty($context['time']) && array_key_exists('timer-label', $context)) {
$message .= ' ' . $context['timer-label'] . ' ' . $this->wrapFormatString($context['time'], 'fg=yellow');
$duration = TimeKeeper::formatDuration($context['time']);
$message .= ' ' . $context['timer-label'] . ' ' . $this->wrapFormatString($duration, 'fg=yellow');
}

return $message;
Expand Down
9 changes: 0 additions & 9 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ public static function success(TaskInterface $task, $message = '', $data = [])
return new self($task, self::EXITCODE_OK, $message, $data);
}

public function getExecutionTime()
{
if (!isset($this['time'])) {
return null;
}
$rawTime = $this['time'];
return round($rawTime, 3).'s';
}

/**
* Return a context useful for logging messages.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/ResultData.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,17 @@ public function merge(ResultData $result)
$this->exchangeArray($mergedData);
return $this;
}

public function hasExecutionTime()
{
return isset($this['time']);
}

public function getExecutionTime()
{
if (!$this->hasExecutionTime()) {
return null;
}
return $this['time'];
}
}
5 changes: 2 additions & 3 deletions src/Task/Docker/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ public function run()
{
$this->cidFile = sys_get_temp_dir() . '/docker_' . uniqid() . '.cid';
$result = parent::run();
$time = $result->getExecutionTime();
$cid = $this->getCid();
return new Result($this, $result->getExitCode(), $result->getMessage(), ['cid' => $cid, 'time' => $time, 'name' => $this->name]);
$result['cid'] = $this->getCid();
return $result;
}

protected function getCid()
Expand Down
5 changes: 3 additions & 2 deletions src/TaskInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public static function formatTaskName($task)
{
$name = get_class($task);
$name = preg_replace('~Stack^~', '', $name);
$name = str_replace('Robo\Task\Base\\', '', $name);
$name = str_replace('Robo\Task\\', '', $name);
$name = str_replace('Robo\\Task\Base\\', '', $name);
$name = str_replace('Robo\\Task\\', '', $name);
$name = str_replace('Robo\\Collection\\', '', $name);
return $name;
}
}

0 comments on commit 8ba0005

Please sign in to comment.