File: RunScheduledTasks.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (112 lines) | stat: -rw-r--r-- 3,084 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CoreAdminHome\Commands;

use Piwik\Container\StaticContainer;
use Piwik\FrontController;
use Piwik\Log\LoggerInterface;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Scheduler\Scheduler;

class RunScheduledTasks extends ConsoleCommand
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var Scheduler|null
     */
    private $scheduler = null;

    public function __construct(?LoggerInterface $logger = null)
    {
        parent::__construct();

        $this->logger = $logger ?: StaticContainer::get(LoggerInterface::class);
    }

    public function getSystemSignalsToHandle(): array
    {
        return [\SIGINT, \SIGTERM];
    }

    public function handleSystemSignal(int $signal): void
    {
        if (null === $this->scheduler) {
            // scheduled tasks have not yet started, stop immediately
            exit;
        }

        $this->logger->info('Received system signal to stop scheduled tasks: ' . $signal);
        $this->scheduler->handleSignal($signal);
    }

    protected function configure()
    {
        $this->setName('scheduled-tasks:run');
        $this->setAliases(array('core:run-scheduled-tasks'));
        $this->setDescription('Will run all scheduled tasks due to run at this time.');
        $this->addOptionalArgument('task', 'Optionally pass the name of a task to run (will run even if not scheduled to run now)');
        $this->addNoValueOption('force', null, 'If set, it will execute all tasks even the ones not due to run at this time.');
    }

    /**
     * Execute command like: ./console core:run-scheduled-tasks
     */
    protected function doExecute(): int
    {
        $input = $this->getInput();

        $this->forceRunAllTasksIfRequested();

        FrontController::getInstance()->init();

        // TODO use dependency injection
        $this->scheduler = StaticContainer::get(Scheduler::class);

        $task = $input->getArgument('task');

        if ($task) {
            $this->runSingleTask($this->scheduler, $task);
        } else {
            $this->scheduler->run();
        }

        $this->writeSuccessMessage('Scheduled Tasks executed');

        return self::SUCCESS;
    }

    private function forceRunAllTasksIfRequested()
    {
        $force = $this->getInput()->getOption('force');

        if ($force && !defined('DEBUG_FORCE_SCHEDULED_TASKS')) {
            define('DEBUG_FORCE_SCHEDULED_TASKS', true);
        }
    }

    private function runSingleTask(Scheduler $scheduler, $task)
    {
        try {
            $message = $scheduler->runTaskNow($task);
        } catch (\InvalidArgumentException $e) {
            $message = $e->getMessage() . PHP_EOL
                . 'Available tasks:' . PHP_EOL
                . implode(PHP_EOL, $scheduler->getTaskList());

            throw new \Exception($message);
        }

        $this->getOutput()->writeln($message);
    }
}