Bladeren bron

[7] Minor code optimisations

Some minor code optimisiations were implemented.
at-robins 1 jaar geleden
bovenliggende
commit
64a1a6fa0a
3 gewijzigde bestanden met toevoegingen van 49 en 49 verwijderingen
  1. 2 2
      backend/src/controller/file_controller.rs
  2. 3 3
      backend/src/main.rs
  3. 44 44
      backend/src/service/temp_file_service.rs

+ 2 - 2
backend/src/controller/file_controller.rs

@@ -315,9 +315,9 @@ pub async fn post_experiment_archive_step_results(
 pub async fn get_experiment_download_step_results(
     database_manager: web::Data<DatabaseManager>,
     app_config: web::Data<Configuration>,
-    experiment_id: web::Path<(i32, String)>,
+    info: web::Path<(i32, String)>,
 ) -> Result<NamedFile, SeqError> {
-    let (experiment_id, archive_id) = experiment_id.into_inner();
+    let (experiment_id, archive_id) = info.into_inner();
     let mut connection = database_manager.database_connection()?;
     Experiment::exists_err(experiment_id, &mut connection)?;
 

+ 3 - 3
backend/src/main.rs

@@ -67,13 +67,13 @@ async fn main() -> Result<(), SeqError> {
     std::thread::spawn(move || {
         let temp_file_manager = TemporaryFileManager::new(temp_file_manager_config);
         loop {
+            if let Err(err) = temp_file_manager.update() {
+                log::error!("Managing temporary data failed with error: {:?}", err);
+            }
             std::thread::sleep(std::time::Duration::new(
                 TEMPORARY_DATA_MANAGEMENT_UPDATE_INTERVALL,
                 0,
             ));
-            if let Err(err) = temp_file_manager.update() {
-                log::error!("Managing temporary data failed with error: {:?}", err);
-            }
         }
     });
     // Setup the application.

+ 44 - 44
backend/src/service/temp_file_service.rs

@@ -29,52 +29,52 @@ impl TemporaryFileManager {
     pub fn update(&self) -> Result<(), SeqError> {
         log::info!("Managing temporary files...");
         let temp_download_path = self.config.temporary_download_path();
+        if temp_download_path.exists() {
+            // Checks all temporary download files / folders and collects errors.
+            let errors: Vec<std::io::Error> = std::fs::read_dir(temp_download_path)?
+                // Log errors and return processable files / folders.
+                // Continues despite errors to at least clean up all the data that can be cleaned up. 
+                .filter_map(Self::filter_log)
+                // Filters old temporary data.
+                .filter_map(|entry| {
+                    Self::filter_log(Self::dir_entry_created(&entry))
+                        .and_then(|created| Self::filter_log(created.elapsed()))
+                        .filter(|lifetime| lifetime.as_secs() > MAX_AGE_TEMPORARY_DOWNLOAD)
+                        .map(|_| entry.path())
+                })
+                // Tries to delete old data and collects errors.
+                .filter_map(|entry| {
+                    match if entry.is_dir() {
+                        log::info!("Deleting temporary download directory {}.", entry.display());
+                        std::fs::remove_dir_all(entry)
+                    } else {
+                        log::info!("Deleting temporary download file {}.", entry.display());
+                        std::fs::remove_file(entry)
+                    } {
+                        Ok(_) => None,
+                        Err(err) => Some(err),
+                    }
+                })
+                .collect();
 
-        // Checks all temporary download files / folders and collects errors.
-        let errors: Vec<std::io::Error> = std::fs::read_dir(temp_download_path)?
-            // Log errors and return processable files / folders.
-            // Continues despite errors to at least clean up all the data that can be cleaned up. 
-            .filter_map(Self::filter_log)
-            // Filters old temporary data.
-            .filter_map(|entry| {
-                Self::filter_log(Self::dir_entry_created(&entry))
-                    .and_then(|created| Self::filter_log(created.elapsed()))
-                    .filter(|lifetime| lifetime.as_secs() > MAX_AGE_TEMPORARY_DOWNLOAD)
-                    .map(|_| entry.path())
-            })
-            // Tries to delete old data and collects errors.
-            .filter_map(|entry| {
-                match if entry.is_dir() {
-                    log::info!("Deleting temporary download directory {}.", entry.display());
-                    std::fs::remove_dir_all(entry)
-                } else {
-                    log::info!("Deleting temporary download file {}.", entry.display());
-                    std::fs::remove_file(entry)
-                } {
-                    Ok(_) => None,
-                    Err(err) => Some(err),
-                }
-            })
-            .collect();
-
-        if !errors.is_empty() {
-            // Returns errors if present.
-            let combined_error = errors.into_iter().fold(String::new(), |mut acc, error| {
-                acc.push_str(&error.to_string());
-                acc.push('\n');
-                acc
-            });
-            Err(SeqError::new(
-                    "std::io::Error",
-                    SeqErrorType::InternalServerError,
-                    combined_error,
-                    "An unforseen error occured during temporary file management. Please consult the logs.",
-                ))
-        } else {
-            // Returns if successful.
-            log::info!("Done managing temporary files.");
-            Ok(())
+            if !errors.is_empty() {
+                // Returns errors if present.
+                let combined_error = errors.into_iter().fold(String::new(), |mut acc, error| {
+                    acc.push_str(&error.to_string());
+                    acc.push('\n');
+                    acc
+                });
+                return Err(SeqError::new(
+                        "std::io::Error",
+                        SeqErrorType::InternalServerError,
+                        combined_error,
+                        "An unforseen error occured during temporary file management. Please consult the logs.",
+                    ))
+            }
         }
+        // Returns if successful.
+        log::info!("Done managing temporary files.");
+        Ok(())
     }
 
     /// Gets the metadata of the directory entry.