MDL-42040 refactor core Moodle shutdown handlers logic