General developer forum

Retry failed event?

Easter Island / Rapa Nui May 2017
Retry failed event?
Group Plugin developers

I have an observer that responds to the course completed event. Which sends data to an external application.

But if the connection fails, I want Moodle to reattempt.

I tried returning false from the observer function and throwing an error, but the function is never called again. Is it possible to do this?

This is for Moodle 3.0.10.

In local/myplugin/db/events.php
$observers = array(
        'eventname'   => '\core\event\course_completed',
        'callback'  => 'local_myplugin_observer::course_completed',
In local/myplugin/classes/observer.php
class local_myplugin_observer {
     * Triggered when 'course_completed' event is triggered.
     * @param \core\event\course_completed $event
     * @return bool
    public static function course_completed(\core\event\course_completed $event) {
        if (not able to connect to external system) {
            // Tried return false;
            // Tried throw new Exception('local_myplugin: not able to connect so will retry);
        // Otherwise everything worked.
        return true;

Average of ratings: -
Gareth J Barnard
Re: Retry failed event?
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Not quite sure any core API for retry.  However you could create and raise your own event on failure of the 'course_completed' event that calls the same code you already have.

Average of ratings: Useful (1)
Picture of Sam Chaffee
Re: Retry failed event?
Group Core developers
Gareth's suggestion is great and I wanted to add to it. I've found that adhoc tasks are great for this sort of thing. In your failure code, instead of returning false or throwing an exception you would create an instance of the adhoc task, set the event data as the custom data and queue it up.
Average of ratings: -