summaryrefslogtreecommitdiffstats
path: root/application/clicommands/HousekeepingCommand.php
blob: 974e28db242613c4d01104bf9da88e0cc214a027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php

namespace Icinga\Module\Director\Clicommands;

use Icinga\Exception\MissingParameterException;
use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Db\Housekeeping;
use Icinga\Module\Director\Db\MembershipHousekeeping;

class HousekeepingCommand extends Command
{
    protected $housekeeping;

    public function tasksAction()
    {
        if ($pending = $this->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;
    }
}