Retry failed event?

Retry failed event?

by Russell England -
Number of replies: 2
Picture of 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(
    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: -
In reply to Russell England

Re: Retry failed event?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of 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)
In reply to Russell England

Re: Retry failed event?

by Sam Chaffee -
Picture of 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.