From 9762b1fa9ceba4c96199abefe176e927efb90b96 Mon Sep 17 00:00:00 2001 From: Vithusha Kethiri Date: Tue, 21 Jan 2025 11:32:03 +1100 Subject: [PATCH] fix: ensure proper lock release and error handling in objectfs manipulator --- .../local/object_manipulator/manipulator.php | 18 ++++++++++++------ classes/local/store/object_file_system.php | 2 +- classes/local/store/s3/client.php | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/classes/local/object_manipulator/manipulator.php b/classes/local/object_manipulator/manipulator.php index afd21808..613d8013 100644 --- a/classes/local/object_manipulator/manipulator.php +++ b/classes/local/object_manipulator/manipulator.php @@ -109,13 +109,19 @@ public function execute(array $objectrecords) { continue; } - $newlocation = $this->manipulate_object($objectrecord); - if (!empty($objectrecord->id)) { - manager::upsert_object($objectrecord, $newlocation); - } else { - manager::update_object_by_hash($objectrecord->contenthash, $newlocation); + try { + $newlocation = $this->manipulate_object($objectrecord); + if (!empty($objectrecord->id)) { + manager::upsert_object($objectrecord, $newlocation); + } else { + manager::update_object_by_hash($objectrecord->contenthash, $newlocation); + } + } catch (Exception $e) { + mtrace('Error processing object: ' . $e->getMessage()); + } finally { + $objectlock->release(); + mtrace('Lock released for: ' . $objectrecord->contenthash); } - $objectlock->release(); } $this->logger->end_timing(); diff --git a/classes/local/store/object_file_system.php b/classes/local/store/object_file_system.php index 86942af6..62cd3ac9 100644 --- a/classes/local/store/object_file_system.php +++ b/classes/local/store/object_file_system.php @@ -173,7 +173,7 @@ protected function get_local_path_from_hash($contenthash, $fetchifnotfound = fal * @param string $contenthash * @return string mimetype as stored in mdl_files */ - protected function get_mimetype_from_hash(string $contenthash): string { + protected function get_mimetype_from_hash(string $contenthash): ?string { global $DB; // We limit 1 because multiple files can have the same contenthash. diff --git a/classes/local/store/s3/client.php b/classes/local/store/s3/client.php index 74e37a54..0a27f15d 100644 --- a/classes/local/store/s3/client.php +++ b/classes/local/store/s3/client.php @@ -500,7 +500,7 @@ public function define_client_section($settings, $config) { * * @throws \Exception if fails. */ - public function upload_to_s3($localpath, $contenthash, string $mimetype) { + public function upload_to_s3($localpath, $contenthash, ?string $mimetype) { $filehandle = fopen($localpath, 'rb'); if (!$filehandle) {