Esempio n. 1
0
/**
 * Deletes cached events that are no longer needed by the component.
 *
 * @access protected To be used from eventslib only
 *
 * @param string $component examples: 'moodle', 'mod_forum', 'block_quiz_results'
 * @param array $cachedhandlers array of the cached events definitions that will be
 * @return int number of unused handlers that have been removed
 */
function events_cleanup($component, $cachedhandlers)
{
    global $DB;
    $deletecount = 0;
    foreach ($cachedhandlers as $eventname => $cachedhandler) {
        if ($qhandlers = $DB->get_records('events_queue_handlers', array('handlerid' => $cachedhandler['id']))) {
            //debugging("Removing pending events from queue before deleting of event handler: $component - $eventname");
            foreach ($qhandlers as $qhandler) {
                events_dequeue($qhandler);
            }
        }
        $DB->delete_records('events_handlers', array('eventname' => $eventname, 'component' => $component));
        $deletecount++;
    }
    return $deletecount;
}
Esempio n. 2
0
/**
 * given a queued handler, call the respective event handler to process the event
 *
 * @access protected To be used from eventslib only
 * @deprecated since Moodle 3.1
 * @param stdClass $qhandler events_queued_handler row from db
 * @return boolean true means event processed, false means retry later, NULL means fatal failure
 */
function events_process_queued_handler($qhandler)
{
    global $DB;
    // get handler
    if (!($handler = $DB->get_record('events_handlers', array('id' => $qhandler->handlerid)))) {
        debugging("Error processing queue handler {$qhandler->id}, missing handler id: {$qhandler->handlerid}");
        //irrecoverable error, remove broken queue handler
        events_dequeue($qhandler);
        return NULL;
    }
    // get event object
    if (!($event = $DB->get_record('events_queue', array('id' => $qhandler->queuedeventid)))) {
        // can't proceed with no event object - might happen when two crons running at the same time
        debugging("Error processing queue handler {$qhandler->id}, missing event id: {$qhandler->queuedeventid}");
        //irrecoverable error, remove broken queue handler
        events_dequeue($qhandler);
        return NULL;
    }
    // call the function specified by the handler
    try {
        $errormessage = 'Unknown error';
        if (events_dispatch($handler, unserialize(base64_decode($event->eventdata)), $errormessage)) {
            //everything ok
            events_dequeue($qhandler);
            return true;
        }
    } catch (Exception $e) {
        // the problem here is that we do not want one broken handler to stop all others,
        // cron handlers are very tricky because the needed data might have been deleted before the cron execution
        $errormessage = "Handler function of component {$handler->component}: {$handler->handlerfunction} threw exception :" . $e->getMessage() . "\n" . format_backtrace($e->getTrace(), true);
        if (!empty($e->debuginfo)) {
            $errormessage .= $e->debuginfo;
        }
    }
    //dispatching failed
    $qh = new stdClass();
    $qh->id = $qhandler->id;
    $qh->errormessage = $errormessage;
    $qh->timemodified = time();
    $qh->status = $qhandler->status + 1;
    $DB->update_record('events_queue_handlers', $qh);
    debugging($errormessage);
    return false;
}
Esempio n. 3
0
/**
 * given a queued handler, call the respective event handler to process the event
 *
 * INTERNAL - to be used from eventslib only
 *
 * @global object
 * @global object
 * @param object $qhandler events_queued_handler object from db
 * @return boolean meaning success, or NULL on fatal failure
 */
function events_process_queued_handler($qhandler)
{
    global $CFG, $DB;
    // get handler
    if (!($handler = $DB->get_record('events_handlers', array('id' => $qhandler->handlerid)))) {
        debugging("Error processing queue handler {$qhandler->id}, missing handler id: {$qhandler->handlerid}");
        //irrecoverable error, remove broken queue handler
        events_dequeue($qhandler);
        return NULL;
    }
    // get event object
    if (!($event = $DB->get_record('events_queue', array('id' => $qhandler->queuedeventid)))) {
        // can't proceed with no event object - might happen when two crons running at the same time
        debugging("Error processing queue handler {$qhandler->id}, missing event id: {$qhandler->queuedeventid}");
        //irrecoverable error, remove broken queue handler
        events_dequeue($qhandler);
        return NULL;
    }
    // call the function specified by the handler
    $errormessage = 'Unknown error';
    if (events_dispatch($handler, unserialize(base64_decode($event->eventdata)), $errormessage)) {
        //everything ok
        events_dequeue($qhandler);
        return true;
    } else {
        //dispatching failed
        $qh = new object();
        $qh->id = $qhandler->id;
        $qh->errormessage = $errormessage;
        $qh->timemodified = time();
        $qh->status = $qhandler->status + 1;
        $DB->update_record('events_queue_handlers', $qh);
        return false;
    }
}