TaskManagerController::actionRerunAllFailedTasks()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 26
rs 8.8571
cc 3
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 TaskManagerController extends BaseController
15
{
16
    /**
17
     * Allow anonymous access (for a cronjob perhaps).
18
     *
19
     * @var bool
20
     */
21
    public $allowAnonymous = true;
22
23
    /**
24
     * Rerun all failed tasks.
25
     */
26
    public function actionRerunAllFailedTasks()
27
    {
28
        // Get all failed tasks
29
        $query = craft()->db->createCommand()
30
            ->select('id')
31
            ->from('tasks')
32
            ->where(array('and', 'lft = 1', 'status = :status'), array(':status' => TaskStatus::Error));
33
34
        // Get all hanging? tasks
35
        if ($timeout = craft()->config->get('taskTimeout')) {
36
            $query->orWhere(array('and', 'lft = 1', 'dateCreated < (NOW() - INTERVAL :seconds SECOND)'), array(':seconds' => $timeout));
37
        }
38
39
        // Get all
40
        $tasks = $query->queryAll();
41
42
        // Loop through failed tasks
43
        foreach ($tasks as $task) {
44
45
            // Rerun task
46
            craft()->tasks->rerunTaskById($task['id']);
47
        }
48
49
        // Run pending tasks controller
50
        craft()->runController('tasks/runPendingTasks');
51
    }
52
}
53