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()) { |
|
|
|
|
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()) { |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.