-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
177 lines (157 loc) · 5.53 KB
/
runner.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
165
166
167
168
169
170
171
172
173
174
175
176
177
import dotenv_switch.auto
import importlib
import multiprocessing as mp
import os
import time
from experts_dw import db, pure_json
from experts_etl import loggers, sync_file_rotator
from experts_etl.pure_to_edw import changes, collection
experts_etl_logger = loggers.experts_etl_logger()
default_interval = 14400 # 4 hours, in seconds
extractor_loaders = list(map(
lambda x: 'experts_etl.extractor_loaders.' + x,
[
'pure_api_changes',
'pure_api_external_organisations',
'pure_api_organisational_units',
'pure_api_external_persons',
'pure_api_persons',
'pure_api_research_outputs',
]
))
transformer_loaders = list(map(
lambda x: 'experts_etl.transformer_loaders.' + x,
[
'pure_api_external_org',
'pure_api_internal_org',
'pure_api_external_person',
'pure_api_internal_person',
'pure_api_pub',
]
))
syncers = list(map(
lambda x: 'experts_etl.' + x,
[
'oit_to_edw.person',
'edw_to_pure.person',
'edw_to_pure.user',
'umn_data_error',
'sync_file_rotator',
]
))
def subprocess(module_name):
module = importlib.import_module(module_name)
experts_etl_logger.info(
'starting: ' + module_name,
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
try:
module.run(
experts_etl_logger=experts_etl_logger
)
except Exception as e:
formatted_exception = loggers.format_exception(e)
experts_etl_logger.error(
f'attempt to execute {module_name}.run() failed: {formatted_exception}',
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
experts_etl_logger.info(
'ending: ' + module_name,
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
def extract_load_changes(params):
changes.run(**params)
def extract_load_collection(collection_api_name, params):
collection.run(collection_api_name, **params)
def run():
experts_etl_logger.info(
'starting: experts etl',
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
for module_name in extractor_loaders + transformer_loaders + syncers:
try:
# Must include a comma after a single arg, or multiprocessing seems to
# get confused and think we're passing multiple arguments:
p = mp.Process(target=subprocess, args=(module_name,))
p.start()
p.join()
except Exception as e:
formatted_exception = loggers.format_exception(e)
experts_etl_logger.error(
f'attempt to execute {module_name} in a new process failed: {formatted_exception}',
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
api_version_collections_map = {}
with db.cx_oracle_connection() as connection:
cursor = connection.cursor()
#for api_version in pure_json.api_versions(cursor): # Later!
for api_version in ['524']: # For now, until we create tables for other versions.
collections = pure_json.collection_api_names_for_api_version(cursor, api_version=api_version)
api_version_collections_map[api_version] = collections
# Just 1 process for now, until we add other api_versions. But do we even need param? Defaults to os.cpu_count.
with mp.Pool(processes=1) as pool:
results = []
for api_version in api_version_collections_map:
try:
results.append(
pool.apply_async(extract_load_changes, ({'api_version': api_version},))
)
except Exception as e:
formatted_exception = loggers.format_exception(e)
experts_etl_logger.error(
f'attempt to extract/load changes raw json in a new process failed: {formatted_exception}',
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
for result in results:
result.get()
with mp.Pool() as pool:
results = []
for api_version, collection_api_names in api_version_collections_map.items():
for collection_api_name in collection_api_names:
try:
results.append(
pool.apply_async(extract_load_collection, (collection_api_name, {'api_version': api_version},))
)
except Exception as e:
formatted_exception = loggers.format_exception(e)
experts_etl_logger.error(
f'attempt to extract/load {collection_api_name} raw json in a new process failed: {formatted_exception}',
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
for result in results:
result.get()
experts_etl_logger.info(
'ending: experts etl',
extra={
'pid': str(os.getpid()),
'ppid': str(os.getppid()),
}
)
loggers.rollover(experts_etl_logger)
def daemon(interval=default_interval):
while True:
run()
time.sleep(interval)
if __name__ == '__main__':
daemon()