66
77import logging
88import os
9+ import shutil
10+ from typing import Optional
911
1012from rocrate_validator import services
1113from rocrate_validator .models import ValidationResult
1719 get_validation_status_from_minio
1820)
1921from app .utils .webhook_utils import send_webhook_notification
22+ from app .utils .file_utils import build_metadata_only_rocrate
2023
2124logger = logging .getLogger (__name__ )
2225
@@ -78,15 +81,75 @@ def process_validation_task_by_id(
7881 os .remove (file_path )
7982
8083
84+ @celery .task
85+ def process_validation_task_by_metadata (
86+ crate_json : str , profile_name : str | None , webhook_url : str | None
87+ ) -> ValidationResult | str :
88+ """
89+ Background task to process the RO-Crate validation for a given json metadata string.
90+
91+ :param crate_json: A string containing the RO-Crate JSON metadata to validate.
92+ :param profile_name: The name of the validation profile to use. Defaults to None.
93+ :param webhook_url: The webhook URL to send notifications to. Defaults to None.
94+ :raises Exception: If an error occurs during the validation process.
95+
96+ :todo: Replace the Crate ID with a more comprehensive system, and replace profile name with URI.
97+ """
98+
99+ skip_checks_list = ['ro-crate-1.1_12.1' ]
100+ file_path = None
101+
102+ try :
103+ # Fetch the RO-Crate from MinIO using the provided ID:
104+ file_path = build_metadata_only_rocrate (crate_json )
105+
106+ logging .info (f"Processing validation task for { file_path } " )
107+
108+ # Perform validation:
109+ validation_result = perform_ro_crate_validation (file_path ,
110+ profile_name ,
111+ skip_checks_list
112+ )
113+
114+ if isinstance (validation_result , str ):
115+ logging .error (f"Validation failed: { validation_result } " )
116+ # TODO: Send webhook with failure notification
117+ raise Exception (f"Validation failed: { validation_result } " )
118+
119+ if not validation_result .has_issues ():
120+ logging .info (f"RO Crate { file_path } is valid." )
121+ else :
122+ logging .info (f"RO Crate { file_path } is invalid." )
123+
124+ if webhook_url :
125+ send_webhook_notification (webhook_url , validation_result .to_json ())
126+
127+ except Exception as e :
128+ logging .error (f"Error processing validation task: { e } " )
129+
130+ # Send failure notification via webhook
131+ error_data = {"profile_name" : profile_name , "error" : str (e )}
132+ send_webhook_notification (webhook_url , error_data )
133+
134+ finally :
135+ # Clean up the temporary file if it was created:
136+ if file_path and os .path .exists (file_path ):
137+ shutil .rmtree (file_path )
138+
139+ return validation_result .to_json ()
140+
141+
142+
81143def perform_ro_crate_validation (
82- file_path : str , profile_name : str | None
144+ file_path : str , profile_name : str | None , skip_checks_list : Optional [ list ] = None
83145) -> ValidationResult | str :
84146 """
85147 Validates an RO-Crate using the provided file path and profile name.
86148
87149 :param file_path: The path to the RO-Crate file to validate
88150 :param profile_name: The name of the validation profile to use. Defaults to None. If None, the CRS4 validator will
89151 attempt to determine the profile.
152+ :param skip_checks_list: A list of checks to skip, if needed
90153 :return: The validation result.
91154 :raises Exception: If an error occurs during the validation process.
92155 """
@@ -102,8 +165,8 @@ def perform_ro_crate_validation(
102165 )
103166 settings = services .ValidationSettings (
104167 rocrate_uri = full_file_path ,
105- # Only include profile_identifier if the profile_name is provided:
106168 ** ({"profile_identifier" : profile_name } if profile_name else {}),
169+ ** ({"skip_checks" : skip_checks_list } if skip_checks_list else {})
107170 )
108171
109172 return services .validate (settings )
0 commit comments