Completed
Push — develop ( 396dfb...fb161c )
by Bob Olde
03:19
created

TaskManagerCommand::actionRun()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 15
Ratio 65.22 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 15
loc 23
rs 9.0856
cc 3
eloc 11
nc 3
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
        // Make sure tasks aren't already running
26 View Code Duplication
        if (!craft()->tasks->isTaskRunning()) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
28
            // Is there a pending task?
29
            if (craft()->tasks->getNextPendingTask()) {
30
31
                // Start running tasks
32
                craft()->tasks->runPendingTasks();
33
34
                return 1;
35
            } else {
36
                Craft::log(Craft::t('No pending tasks found.'));
37
            }
38
        } else {
39
            Craft::log(Craft::t('Tasks are already running.'));
40
        }
41
42
        return 0;
43
    }
44
45
    /**
46
     * Watch for tasks and run them.
47
     */
48
    public function actionWatch()
49
    {
50
        Craft::log(Craft::t('Watching for new tasks.'));
51
52
        // Keep on checking for pending tasks
53
        while (true) {
54
55
            // Make sure tasks aren't already running
56 View Code Duplication
            if (!craft()->tasks->isTaskRunning()) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
58
                // Reset next pending tasks cache
59
                $this->resetCraftNextPendingTasksCache();
60
61
                // Is there a pending task?
62
                if (craft()->tasks->getNextPendingTask()) {
63
64
                    // Start running tasks
65
                    craft()->tasks->runPendingTasks();
66
                } else {
67
                    Craft::log(Craft::t('No pending tasks found.'));
68
                }
69
            } else {
70
                Craft::log(Craft::t('Tasks are already running, skipping iteration.'));
71
            }
72
73
            // Sleep a little
74
            sleep(10);
75
        }
76
    }
77
78
    /**
79
     * Reset craft next pending task cache using reflection.
80
     *
81
     * @param TasksService $service
0 ignored issues
show
Bug introduced by
There is no parameter named $service. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
     */
83
    private function resetCraftNextPendingTasksCache()
84
    {
85
        $obj = craft()->tasks;
86
        $refObject = new \ReflectionObject($obj);
87
        $refProperty = $refObject->getProperty('_nextPendingTask');
88
        $refProperty->setAccessible(true);
89
        $refProperty->setValue($obj, null);
90
    }
91
}
92