Completed
Push — master ( 437106...78f9b5 )
by Andreas
31:22
created

midcom_services_at_cron_check   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 30.95%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 65
ccs 13
cts 42
cp 0.3095
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle_error() 0 8 1
A process_entry() 0 26 3
A execute() 0 20 3
1
<?php
2
/**
3
 * @package midcom.services.at
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * The Cron handler of the AT service, when executed it checks the database for entries
11
 * that need to be run, then loads their relevant components and calls the interface
12
 * class for the defined method.
13
 *
14
 * @package midcom.services.at
15
 */
16
class midcom_services_at_cron_check extends midcom_baseclasses_components_cron_handler
17
{
18
    /**
19
     * Loads all entries that need to be processed and processes them.
20
     */
21 1
    public function execute()
22
    {
23 1
        $limit = (int) $this->_config->get('limit_per_run');
24
25
        // We load each entry separately to minimize the chances of double executions
26
        // when cron runs overlap or are triggered twice for some reason
27
        // since this is only used by cron, performance is secondary, so better play it safe
28 1
        for ($i = 0; $i < $limit; $i++) {
29 1
            $qb = midcom_services_at_entry_dba::new_query_builder();
30 1
            $qb->add_constraint('start', '<=', time());
31 1
            $qb->add_constraint('status', '=', midcom_services_at_entry_dba::SCHEDULED);
32 1
            $qb->set_limit(1);
33
34 1
            midcom::get()->auth->request_sudo('midcom.services.at');
35 1
            $qbret = $qb->execute();
36 1
            midcom::get()->auth->drop_sudo();
37 1
            if (empty($qbret)) {
38 1
                break;
39
            }
40
            $this->process_entry($qbret[0]);
41
        }
42 1
    }
43
44
    private function process_entry(midcom_services_at_entry_dba $entry)
45
    {
46
        debug_add("Processing entry #{$entry->id}\n");
47
        //Avoid double-execute
48
        $entry->status = midcom_services_at_entry_dba::RUNNING;
49
        midcom::get()->auth->request_sudo('midcom.services.at');
50
        $entry->update();
51
        midcom::get()->auth->drop_sudo();
52
        $args = $entry->arguments;
53
        $args['midcom_services_at_entry_object'] = $entry;
54
        $interface = midcom::get()->componentloader->get_interface_class($entry->component);
55
        $method = $entry->method;
56
        if (!is_callable([$interface, $method])) {
57
            $error = get_class($interface) . "->{$method}() is not callable";
58
            $this->handle_error($entry, $error, $args);
59
            return;
60
        }
61
        $mret = $interface->$method($args, $this);
62
63
        if ($mret !== true) {
64
            $error = get_class($interface) . '->' . $method . '(' . json_encode($args) . ", \$this) returned '{$mret}', errstr: " . midcom_connection::get_error_string();
65
            $this->handle_error($entry, $error, $args);
66
        } else {
67
            midcom::get()->auth->request_sudo('midcom.services.at');
68
            $entry->delete();
69
            midcom::get()->auth->drop_sudo();
70
        }
71
    }
72
73
    private function handle_error(midcom_services_at_entry_dba $entry, $error, array $args)
74
    {
75
        $this->print_error($error, $args);
76
        //PONDER: Delete instead ? (There is currently nothing we do with failed entries)
77
        $entry->status = midcom_services_at_entry_dba::FAILED;
78
        midcom::get()->auth->request_sudo('midcom.services.at');
79
        $entry->update();
80
        midcom::get()->auth->drop_sudo();
81
    }
82
}
83