Skip to content

Commit f228982

Browse files
authored
Merge pull request #39 from SciCatProject/background-ingestor
Background ingestor
2 parents f1ec799 + 8a2da22 commit f228982

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

src/background-ingestor.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject)
3+
import json
4+
import logging
5+
from collections.abc import Generator
6+
from contextlib import contextmanager
7+
8+
from scicat_configuration import (
9+
build_background_ingestor_arg_parser,
10+
build_scicat_config,
11+
)
12+
from scicat_logging import build_logger
13+
14+
# import scippnexus as snx
15+
16+
17+
def quit(logger: logging.Logger, unexpected: bool = True) -> None:
18+
"""Log the message and exit the program."""
19+
import sys
20+
21+
logger.info("Exiting ingestor")
22+
sys.exit(1 if unexpected else 0)
23+
24+
25+
@contextmanager
26+
def exit_at_exceptions(logger: logging.Logger) -> Generator[None, None, None]:
27+
"""Exit the program if an exception is raised."""
28+
try:
29+
yield
30+
except KeyboardInterrupt:
31+
logger.info("Received keyboard interrupt.")
32+
quit(logger, unexpected=False)
33+
except Exception as e:
34+
logger.error("An exception occurred: %s", e)
35+
quit(logger, unexpected=True)
36+
else:
37+
logger.error("Loop finished unexpectedly.")
38+
quit(logger, unexpected=True)
39+
40+
41+
def main() -> None:
42+
"""Main entry point of the app."""
43+
arg_parser = build_background_ingestor_arg_parser()
44+
arg_namespace = arg_parser.parse_args()
45+
config = build_scicat_config(arg_namespace)
46+
logger = build_logger(config)
47+
48+
# Log the configuration as dictionary so that it is easier to read from the logs
49+
logger.info(
50+
'Starting the Scicat background Ingestor with the following configuration:'
51+
)
52+
logger.info(config.to_dict())
53+
54+
with exit_at_exceptions(logger):
55+
nexus_file = arg_namespace.nexus_file
56+
logger.info("Nexus file to be ingested : ")
57+
logger.info(nexus_file)
58+
59+
done_writing_message_file = (
60+
arg_namespace.arg_namespace.done_writing_message_file
61+
)
62+
logger.info("Done writing message file linked to nexus file : ")
63+
logger.info(done_writing_message_file)
64+
65+
# open and read done writing message input file
66+
with open(done_writing_message_file, 'r') as f:
67+
done_writing_message = json.load(f)
68+
69+
print(done_writing_message)
70+
# open nexus file
71+
# nxs = snx.File(nexus_file)
72+
73+
# extract instrument
74+
75+
# load instrument metadata configuration
76+
77+
# retrieve information regarding the proposal
78+
79+
# extract and prepare metadata
80+
81+
# create b2blake hash of all the files
82+
83+
# create and populate scicat dataset entry
84+
85+
# create and populate scicat origdatablock entry
86+
# with files and hashes previously computed
87+
88+
pass

src/scicat_configuration.py

+33
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,39 @@ def build_main_arg_parser() -> argparse.ArgumentParser:
9494
return parser
9595

9696

97+
def build_background_ingestor_arg_parser() -> argparse.ArgumentParser:
98+
parser = build_main_arg_parser()
99+
100+
group = parser.add_argument_group('Scicat Background Ingestor Options')
101+
102+
group.add_argument(
103+
'-f',
104+
'--nf',
105+
'--file',
106+
'--nexus-file',
107+
default='',
108+
dest='nexus_file',
109+
help='Full path of the input nexus file to be ingested',
110+
type=str,
111+
)
112+
113+
group.add_argument(
114+
'-m',
115+
'--dm',
116+
'--wrdm',
117+
'--done-writing-message-file',
118+
default='',
119+
dest='done_writing_message_file',
120+
help="""
121+
Full path of the input done writing message
122+
file that match the nexus file to be ingested
123+
""",
124+
type=str,
125+
)
126+
127+
return parser
128+
129+
97130
@dataclass
98131
class GraylogOptions:
99132
host: str = ""

src/scicat_ingestor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def main() -> None:
4141
logger = build_logger(config)
4242

4343
# Log the configuration as dictionary so that it is easier to read from the logs
44-
logger.info('Starting the Scicat Ingestor with the following configuration:')
44+
logger.info('Starting the Scicat online Ingestor with the following configuration:')
4545
logger.info(config.to_dict())
4646

4747
with exit_at_exceptions(logger):

0 commit comments

Comments
 (0)