1414// You should have received a copy of the GNU General Public License
1515// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616
17- /**
18- * Process trigger system events.
19- *
20- * @package tool_trigger
21- * @copyright Matt Porritt <[email protected] > 22- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23- */
24-
2517namespace tool_trigger ;
2618
2719use tool_trigger \helper \processor_helper ;
@@ -138,7 +130,7 @@ protected function is_event_ignored(\core\event\base $event) {
138130 // If we do not have the triggers in the cache then return them from the DB.
139131 if ($ sitesubscriptions === false ) {
140132 // Set the array for the cache.
141- $ sitesubscriptions = array () ;
133+ $ sitesubscriptions = [] ;
142134 if ($ subscriptions = $ DB ->get_records_sql ($ sql )) {
143135 foreach ($ subscriptions as $ subscription ) {
144136 $ sitesubscriptions [$ subscription ->event ] = true ;
@@ -198,6 +190,21 @@ private function process_realtime_workflows($evententry) {
198190 }
199191 }
200192
193+ /**
194+ * Processes a real-time workflow by executing its steps immediately in response to an event.
195+ *
196+ * This method:
197+ * - Marks the workflow as triggered and updates its record in the database.
198+ * - Records the workflow trigger in history.
199+ * - Restores the triggering event from stored data.
200+ * - Iterates through workflow steps in order, executing each step and recording results.
201+ * - Stops execution early if a step fails or throws an error.
202+ * - Handles transactional safety for each step to prevent partial writes.
203+ * - If an error occurs, records the failed step and queues the workflow for retry via cron.
204+ *
205+ * @param \stdClass $workflow The workflow object containing workflow metadata.
206+ * @param \stdClass $evententry The event entry object representing the trigger event data.
207+ */
201208 private function process_realtime_workflow ($ workflow , $ evententry ) {
202209 global $ DB ;
203210
@@ -292,20 +299,20 @@ public static function record_workflow_trigger(int $workflowid, $event, int $att
292299
293300 // Get new run number.
294301 $ sqlfrag = "SELECT MAX(number) FROM {tool_trigger_workflow_hist} WHERE workflowid = :wfid " ;
295- $ runnumber = $ DB ->get_field_sql ($ sqlfrag , array ( 'wfid ' => $ workflowid) ) + 1 ;
302+ $ runnumber = $ DB ->get_field_sql ($ sqlfrag , [ 'wfid ' => $ workflowid] ) + 1 ;
296303
297304 // Encode event data as JSON.
298305 $ eventdata = json_encode ($ event );
299306
300- $ id = $ DB ->insert_record ('tool_trigger_workflow_hist ' , array (
307+ $ id = $ DB ->insert_record ('tool_trigger_workflow_hist ' , [
301308 'workflowid ' => $ workflowid ,
302309 'number ' => $ runnumber ,
303310 'timecreated ' => time (),
304311 'event ' => $ eventdata ,
305312 'eventid ' => $ event ->id ,
306313 'userid ' => $ event ->userid ,
307- 'attemptnum ' => $ attemptnum
308- ) , true );
314+ 'attemptnum ' => $ attemptnum,
315+ ] , true );
309316
310317 // Return the id for use in other tables.
311318 return $ id ;
@@ -488,7 +495,7 @@ public static function execute_next_step_historic(int $stepid, int $origrun = 0,
488495 'workflow ' => $ step ->workflowid ,
489496 'run ' => $ origrun ,
490497 'number ' => $ step ->number + 1 ,
491- 'previd ' => $ step ->id
498+ 'previd ' => $ step ->id ,
492499 ];
493500 $ nextstep = $ DB ->get_record_sql ($ nextstepsql , $ params );
494501
@@ -529,7 +536,7 @@ public static function execute_next_step_historic(int $stepid, int $origrun = 0,
529536 'workflow ' => $ nextstep ->workflowid ,
530537 'run ' => $ runid ,
531538 'stepname ' => $ nextstep ->name ,
532- 'previd ' => $ previd
539+ 'previd ' => $ previd,
533540 ]);
534541
535542 // Return the ID just executed for use in moving through the historic chain.
@@ -559,7 +566,7 @@ public static function execute_next_step_current(int $stepid) {
559566 $ nextstep = $ DB ->get_record_sql ($ nextstepsql , [
560567 'workflow ' => $ step ->workflowid ,
561568 'run ' => $ step ->runid ,
562- 'number ' => $ step ->number + 1
569+ 'number ' => $ step ->number + 1 ,
563570 ]);
564571
565572 // If no nextstep is found, jump out.
@@ -582,7 +589,7 @@ public static function execute_next_step_current(int $stepid) {
582589 $ newstepid = $ DB ->get_field_sql ($ newstepsql , [
583590 'workflow ' => $ nextstep ->workflowid ,
584591 'run ' => $ nextstep ->runid ,
585- 'stepname ' => $ nextstep ->name
592+ 'stepname ' => $ nextstep ->name ,
586593 ]);
587594 return $ newstepid ;
588595 }
@@ -619,7 +626,7 @@ public static function execute_step_and_continue_historic(int $stepid, bool $com
619626 $ newstep = $ DB ->get_record_sql ($ newstepsql , [
620627 'workflow ' => $ step ->workflowid ,
621628 'run ' => $ newrunid ,
622- 'stepname ' => $ step ->name
629+ 'stepname ' => $ step ->name ,
623630 ]);
624631
625632 // Now execute the next step in the historic chain.
@@ -660,7 +667,7 @@ public static function execute_step_and_continue_current(int $stepid, bool $comp
660667 $ newstep = $ DB ->get_record_sql ($ newstepsql , [
661668 'workflow ' => $ step ->workflowid ,
662669 'run ' => $ step ->runid ,
663- 'stepname ' => $ step ->name
670+ 'stepname ' => $ step ->name ,
664671 ]);
665672
666673 // Now execute the next step, based on the step we just created.
@@ -809,7 +816,7 @@ public static function record_cancelled_workflow($workflowid, $event, $runid = n
809816
810817 // Get new run number.
811818 $ sqlfrag = "SELECT MAX(number) FROM {tool_trigger_workflow_hist} WHERE workflowid = :wfid " ;
812- $ runnumber = $ DB ->get_field_sql ($ sqlfrag , array ( 'wfid ' => $ workflowid) ) + 1 ;
819+ $ runnumber = $ DB ->get_field_sql ($ sqlfrag , [ 'wfid ' => $ workflowid] ) + 1 ;
813820
814821 // Encode event data as JSON.
815822 $ eventdata = json_encode ($ event );
@@ -822,7 +829,7 @@ public static function record_cancelled_workflow($workflowid, $event, $runid = n
822829 'timecreated ' => time (),
823830 'event ' => $ eventdata ,
824831 'eventid ' => $ event ->id ,
825- 'failedstep ' => $ status
832+ 'failedstep ' => $ status,
826833 ];
827834
828835 if (empty ($ runid )) {
0 commit comments