MDL-63906 analytics: Release the lock on \Throwable received
authorDavid Monllaó <davidm@moodle.com>
Fri, 15 Mar 2019 14:48:27 +0000 (15:48 +0100)
committerDavid Monllaó <davidm@moodle.com>
Fri, 15 Mar 2019 14:48:27 +0000 (15:48 +0100)
analytics/classes/local/analyser/base.php

index 68f7342..06b7468 100644 (file)
@@ -577,9 +577,14 @@ abstract class base {
             return $result;
         }
 
-        // Remove samples the target consider invalid.
-        $this->analysabletarget->add_sample_data($samplesdata);
-        $this->analysabletarget->filter_out_invalid_samples($sampleids, $analysable, $includetarget);
+        try {
+            // Remove samples the target consider invalid.
+            $this->analysabletarget->add_sample_data($samplesdata);
+            $this->analysabletarget->filter_out_invalid_samples($sampleids, $analysable, $includetarget);
+        } catch (\Throwable $e) {
+            $dataset->close_process();
+            throw $e;
+        }
 
         if (!$sampleids) {
             $result->status = \core_analytics\model::NO_DATASET;
@@ -588,19 +593,24 @@ abstract class base {
             return $result;
         }
 
-        foreach ($this->indicators as $key => $indicator) {
-            // The analyser attaches the main entities the sample depends on and are provided to the
-            // indicator to calculate the sample.
-            $this->indicators[$key]->add_sample_data($samplesdata);
-        }
+        try {
+            foreach ($this->indicators as $key => $indicator) {
+                // The analyser attaches the main entities the sample depends on and are provided to the
+                // indicator to calculate the sample.
+                $this->indicators[$key]->add_sample_data($samplesdata);
+            }
 
-        // Here we start the memory intensive process that will last until $data var is
-        // unset (until the method is finished basically).
-        if ($includetarget) {
-            $data = $timesplitting->calculate($sampleids, $this->get_samples_origin(), $this->indicators, $ranges,
-                $this->analysabletarget);
-        } else {
-            $data = $timesplitting->calculate($sampleids, $this->get_samples_origin(), $this->indicators, $ranges);
+            // Here we start the memory intensive process that will last until $data var is
+            // unset (until the method is finished basically).
+            if ($includetarget) {
+                $data = $timesplitting->calculate($sampleids, $this->get_samples_origin(), $this->indicators, $ranges,
+                    $this->analysabletarget);
+            } else {
+                $data = $timesplitting->calculate($sampleids, $this->get_samples_origin(), $this->indicators, $ranges);
+            }
+        } catch (\Throwable $e) {
+            $dataset->close_process();
+            throw $e;
         }
 
         if (!$data) {
@@ -610,11 +620,16 @@ abstract class base {
             return $result;
         }
 
-        // Add extra metadata.
-        $this->add_model_metadata($data);
+        try {
+            // Add extra metadata.
+            $this->add_model_metadata($data);
 
-        // Write all calculated data to a file.
-        $file = $dataset->store($data);
+            // Write all calculated data to a file.
+            $file = $dataset->store($data);
+        } catch (\Throwable $e) {
+            $dataset->close_process();
+            throw $e;
+        }
 
         // Flag the model + analysable + timesplitting as analysed.
         $dataset->close_process();