TaskManagerCommand::actionWatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Craft;
4
5
/**
6
 * Task Manager.
7
 *
8
 * @author    Bob Olde Hampsink <[email protected]>
9
 * @copyright Copyright (c) 2015, Bob Olde Hampsink
10
 * @license   MIT
11
 *
12
 * @link      http://github.com/boboldehampsink
13
 */
14
class TaskManagerCommand extends BaseCommand
15
{
16
    /**
17
     * Runs pending tasks.
18
     *
19
     * @return int
20
     */
21
    public function actionRun()
22
    {
23
        Craft::log(Craft::t('Running new tasks.'));
24
25
        // Start running tasks
26
        craft()->tasks->runPendingTasks();
27
28
        return 1;
29
    }
30
31
    /**
32
     * Watch for tasks and run them.
33
     */
34
    public function actionWatch()
35
    {
36
        Craft::log(Craft::t('Watching for new tasks.'));
37
38
        // Keep on checking for pending tasks
39
        while (true) {
40
41
            // Reset next pending tasks cache
42
            $this->resetCraftNextPendingTasksCache();
43
44
            // Start running tasks
45
            craft()->tasks->runPendingTasks();
46
47
            // Sleep a little
48
            sleep(craft()->config->get('taskInterval'));
49
        }
50
    }
51
52
    /**
53
     * Reset craft next pending task cache using reflection.
54
     */
55
    private function resetCraftNextPendingTasksCache()
56
    {
57
        $obj = craft()->tasks;
58
        $refObject = new \ReflectionObject($obj);
59
        $refProperty = $refObject->getProperty('_nextPendingTask');
60
        $refProperty->setAccessible(true);
61
        $refProperty->setValue($obj, null);
62
    }
63
}
64