From f77e9433d0d8b1a215fa63f0ca2d0d00e20c618e Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sun, 20 Mar 2016 16:32:04 +0100 Subject: [PATCH] Cli: add housekeeping command --- .../clicommands/HousekeepingCommand.php | 74 +++++++++++++++++++ library/Director/Db/Housekeeping.php | 1 + 2 files changed, 75 insertions(+) create mode 100644 application/clicommands/HousekeepingCommand.php diff --git a/application/clicommands/HousekeepingCommand.php b/application/clicommands/HousekeepingCommand.php new file mode 100644 index 00000000..ffb3f1a6 --- /dev/null +++ b/application/clicommands/HousekeepingCommand.php @@ -0,0 +1,74 @@ +params->shift('pending')) { + $tasks = $this->housekeeping()->getPendingTaskSummary(); + } else { + $tasks = $this->housekeeping()->getTaskSummary(); + } + + $len = array_reduce( + $tasks, + function($max, $task) { + return max( + $max, + strlen($task->title) + strlen($task->name) + 3 + ); + } + ); + + if (count($tasks)) { + print "\n"; + printf(" %-" . $len . "s | %s\n", 'Housekeeping task (name)', 'Count'); + printf("-%-" . $len . "s-|-------\n", str_repeat('-', $len)); + } + + foreach ($tasks as $task) { + printf( + " %-" . $len . "s | %5d\n", + sprintf('%s (%s)', $task->title, $task->name), + $task->count + ); + } + + if (count($tasks)) { + print "\n"; + } + } + + public function runAction() + { + if (!$job = $this->params->shift()) { + throw new MissingParameterException( + 'Job is required, say ALL to run all pending jobs' + ); + } + + if ($job === 'ALL') { + $this->housekeeping()->runAllTasks(); + } else { + $this->housekeeping()->runTask($job); + } + } + + protected function housekeeping() + { + if ($this->housekeeping === null) { + $this->housekeeping = new Housekeeping($this->db()); + } + + return $this->housekeeping; + } +} diff --git a/library/Director/Db/Housekeeping.php b/library/Director/Db/Housekeeping.php index c7f7bfdc..e7de5f35 100644 --- a/library/Director/Db/Housekeeping.php +++ b/library/Director/Db/Housekeeping.php @@ -34,6 +34,7 @@ class Housekeeping foreach ($this->listTasks() as $name => $title) { $func = 'count' . ucfirst($name); $summary[$name] = (object) array( + 'name' => $name, 'title' => $title, 'count' => $this->$func() );