Completed
Push — develop ( b0afbf...396dfb )
by Bob Olde
02:24
created

TaskManagerCommand::actionWatch()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 11

Duplication

Lines 13
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 13
loc 26
rs 8.5806
cc 4
eloc 11
nc 4
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
                // Is there a pending task?
59
                if (craft()->tasks->getNextPendingTask()) {
60
61
                    // Start running tasks
62
                    craft()->tasks->runPendingTasks();
63
                } else {
64
                    Craft::log(Craft::t('No pending tasks found.'));
65
                }
66
            } else {
67
                Craft::log(Craft::t('Tasks are already running, skipping iteration.'));
68
            }
69
70
            // Sleep a little
71
            sleep(10);
72
        }
73
    }
74
}
75