summaryrefslogtreecommitdiffstats
path: root/lib/Command/Updater/Job.php
blob: d6fa4a5aa677c9ae003efbfc5f5fd99f3d3f68e9 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 */

namespace OCA\News\Command\Updater;

use DateTime;
use DateInterval;
use OCP\Util;
use OCP\IConfig;
use OCA\News\AppInfo\Application;
use OCA\News\Service\StatusService;
use OCA\News\Service\UpdaterService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Job extends Command
{
    /**
     * @var IConfig
     */
    private $config;

    /**
     * @var StatusService Status service
     */
    private $statusService;

    /**
     * @var UpdaterService Update service
     */
    private $updaterService;

    public function __construct(IConfig $config, StatusService $statusService, UpdaterService $updaterService)
    {
        parent::__construct();
        $this->config = $config;
        $this->statusService = $statusService;
        $this->updaterService = $updaterService;
    }

    /**
     * @return void
     */
    protected function configure()
    {
        $this->setName('news:updater:job')
            ->addOption(
                'reset',
                null,
                InputOption::VALUE_NONE,
                'If the job should be reset, warning this might lead to issues.'
            )
            ->setDescription('Console API for checking the update job status and to reset it.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $reset = (bool) $input->getOption('reset');

        [$major, $minor, $micro] = Util::getVersion();
        
        if ($major < 26) {
            $output->writeln("Error: This only works with Nextcloud 26 or newer.");
            return 1;
        }
        $output->writeln("Checking update Status");
        $date = new DateTime();
        $date->setTimestamp($this->statusService->getUpdateTime());
        $now = new DateTime('now');
        $elapsedInterval = $now->diff($date);
        $output->writeln("Last Execution was ".$date->format('Y-m-d H:i:s e').
          $elapsedInterval->format("; %h hours, %i minutes, %s seconds ago"));

        if ($reset) {
            $output->writeln("Attempting to reset the job.");
            $this->updaterService->reset();
            $output->writeln("Done, job should execute on next schedule.");
        } else {
            $updateInterval = $this->config->getAppValue(
                Application::NAME,
                'updateInterval',
                Application::DEFAULT_SETTINGS['updateInterval']
            );
            $threshold = ($updateInterval * 2) + 900;
            $elapsedSeconds = $now->getTimestamp() - $date->getTimestamp();
            if ($elapsedSeconds > $threshold) {
                $output->writeln("Something's wrong.");
                return 2;
            }
        }
        return 0;
    }
}