forked from NationalGenomicsInfrastructure/Yggdrasil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathygg-mule.py
164 lines (131 loc) · 5.27 KB
/
ygg-mule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
import argparse
import asyncio
from lib.core_utils.common import YggdrasilUtilities as Ygg
from lib.core_utils.config_loader import ConfigLoader
from lib.core_utils.logging_utils import configure_logging, custom_logger
from lib.couchdb.manager import ProjectDBManager, YggdrasilDBManager
from lib.realms.delivery.deliver import DeliveryManager
# Configure logging
configure_logging(debug=True)
logging = custom_logger("Ygg-Mule")
async def launch_realm(realm):
try:
await realm.launch()
except Exception as e:
logging.error(f"Error in realm.launch(): {e}", exc_info=True)
def process_project_doc(doc_id):
"""Fetch a project doc from the ProjectDB by its doc_id,
find which analysis module to load, and execute that module.
Args:
doc_id (str): The ID of the document to process.
"""
# Initialize the database managers
pdm = ProjectDBManager()
ydm = YggdrasilDBManager()
# Fetch the document from the project database
document = pdm.fetch_document_by_id(doc_id)
if not document:
logging.error(f"Project document with ID {doc_id} not found in Project DB.")
return
project_id = document.get("project_id")
# Determine the appropriate module to load
module_loc = get_module_location(document)
if not module_loc:
logging.warning(f"No module found for document with ID {doc_id}.")
return
# Load and execute the module
try:
RealmClass = Ygg.load_realm_class(module_loc)
if RealmClass:
realm = RealmClass(document, ydm)
if realm.proceed:
asyncio.run(launch_realm(realm))
logging.info("Processing complete.")
else:
logging.info(
f"Skipping processing due to missing required information for project: {project_id}"
)
else:
logging.warning(f"Failed to load module '{module_loc}' for doc {doc_id}.")
except Exception as e:
logging.error(f"Error while processing project doc: {e}", exc_info=True)
def process_yggdrasil_doc(doc_id):
"""
Fetch an Yggdrasil document by its doc_id
and pass it to the DeliveryManager flow.
Args:
doc_id (str): The ID of the document to process.
"""
# Initialize the database managers
ydm = YggdrasilDBManager()
# Fetch the document from the yggdrasil database
document = ydm.get_document_by_project_id(doc_id)
if not document:
logging.error(f"Document with ID {doc_id} not found in Yggdrasil DB.")
return
# Load and execute the module
try:
deliv_realm = DeliveryManager(document, ydm)
if deliv_realm.proceed:
asyncio.run(launch_realm(deliv_realm))
logging.info("Delivery processing complete.")
else:
logging.info(f"Skipping delivery: Not enough info in doc {doc_id}.")
except Exception as e:
logging.error(f"Error while processing yggdrasil doc: {e}", exc_info=True)
# TODO: If the module registry doesn’t change often, consider caching it to avoid reloading it every time
def get_module_location(document):
"""Retrieve the module location based on the library construction method.
Args:
document (dict): The document containing details about the library construction method.
Returns:
str or None: The module location if found; otherwise, None.
"""
try:
# Load the module registry configuration
module_registry = ConfigLoader().load_config("module_registry.json")
# Extract the library construction method from the document
method = document["details"]["library_construction_method"]
# Retrieve module configuration for the specified method
# module_config = module_registry.get(method)
# if module_config:
# return module_config["module"]
# Direct match
if method in module_registry:
return module_registry[method]["module"]
# If no exact match, check for prefix matches
for registered_method, config in module_registry.items():
if config.get("prefix") and method.startswith(registered_method):
return config["module"]
logging.warning(f"No module configuration found for method '{method}'.")
return None
except KeyError as e:
logging.error(f"Error accessing module location: {e}")
return None
except Exception as e:
logging.error(f"Unexpected error: {e}", exc_info=True)
return None
def main():
"""Main function to parse arguments and start Yggdrasil."""
logging.info("Ygg-Mule: Standalone Module Executor for Yggdrasil")
# Set up argument parser
parser = argparse.ArgumentParser(
description="Ygg-Mule: Standalone Module Executor for Yggdrasil"
)
parser.add_argument("doc_id", type=str, help="Document ID to process")
parser.add_argument(
"-d",
"--delivery",
action="store_true",
help="Indicate if this is a delivery document in Yggdrasil DB.",
)
# Parse arguments
args = parser.parse_args()
# Process the document
if args.delivery:
process_yggdrasil_doc(args.doc_id)
else:
process_project_doc(args.doc_id)
if __name__ == "__main__":
main()