This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
227 lines (189 loc) · 7.74 KB
/
control.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
############################################################################
# #
# Copyright (c) 2017 eBay Inc. #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
############################################################################
from __future__ import print_function
from __future__ import division
import time
import configfile
import dependency
import dispatch
import workspace
import database
import methods
from extras import json_save
from setupfile import update_setup
from jobid import resolve_jobid_filename, put_workspaces
from extras import DotDict, Temp
from threading import Thread
from os import unlink
from os.path import join
METHODS_CONFIGFILENAME = 'methods.conf'
#DIRECTORIES = ['analysis', 'default_analysis', ]
#Methods = methods.SubMethods(DIRECTORIES, CONFIGFILENAME)
class Main:
""" This is the main controller behind the daemon. """
def __init__(self, options, daemon_url):
"""
Setup objects:
Methods
WorkSpaces
"""
self.config = configfile.get_config(options.config, verbose=False)
self.debug = options.debug
self.daemon_url = daemon_url
# check config file
configfile.sanity_check(self.config)
self._update_methods()
# initialise workspaces
self.workspaces = {}
for name, data in self.config['workspace'].items():
path = data[0]
slices = data[1]
w = workspace.WorkSpace(name, path, slices)
if w.ok:
# add only if everything whent well in __init__
self.workspaces[name] = w
else:
# hmm, maybe new target workspace
if name == self.config['main_workspace']:
self.workspaces[name] = workspace.WorkSpace(name, path, slices, True)
put_workspaces({k: v.path for k, v in self.workspaces.items()})
# set current workspace pointers
self.set_workspace(self.config['main_workspace'])
self.set_remote_workspaces(self.config.get('remote_workspaces', ''))
# and update contents
self.DataBase = database.DataBase(self)
self.update_database()
self.broken = False
def _update_methods(self):
print('Update methods')
# initialise methods class looking in method_directories from config file
method_directories = self.config['method_directories']
self.Methods = methods.SubMethods(method_directories, METHODS_CONFIGFILENAME, self.config)
def update_methods(self):
try:
self._update_methods()
self.update_database()
self.broken = False
except methods.MethodLoadException as e:
self.broken = e.module_list
return {'broken': e.module_list}
def set_workspace(self, workspacename):
""" set current workspace by name, and clear all remotes, just to be sure """
self.current_workspace = workspacename
self.current_remote_workspaces = set()
self.workspaces[workspacename].make_writeable()
def set_remote_workspaces(self, workspaces):
slices = self.workspaces[self.current_workspace].get_slices()
self.current_remote_workspaces = set()
for name in workspaces:
if self.workspaces[name].get_slices() == slices:
self.current_remote_workspaces.add(name)
else:
print("Warning, could not add remote workspace \"%s\", since it has %d slices (and %d required from \"%s\")" % (
name, self.workspaces[name].get_slices(), slices, self.current_workspace))
def get_workspace_details(self):
""" Some information about main workspace, some parts of config """
return dict(
[(key, getattr(self.workspaces[self.current_workspace], key),) for key in ('name', 'path', 'slices',)] +
[(key, self.config.get(key),) for key in ('source_directory', 'result_directory', 'common_directory', 'urd',)]
)
def list_workspaces(self):
""" Return list of all initiated workspaces """
return self.workspaces
def get_current_workspace(self):
""" return name of current workspace """
return self.current_workspace
def get_current_remote_workspaces(self):
""" return names of current remote workspaces """
return self.current_remote_workspaces
def add_single_jobid(self, jobid):
ws = self.workspaces[jobid.rsplit('-', 1)[0]]
ws.add_single_jobid(jobid)
return self.DataBase.add_single_jobid(jobid)
def update_database(self):
"""Insert all new jobids (from all workspaces) in database,
discard all deleted or with incorrect hash.
"""
t_l = []
for name in self.workspaces:
# Run all updates in parallel. This gets all (sync) listdir calls
# running at the same time. Then each workspace will spawn processes
# to do the post.json checking, to keep disk queues effective. But
# try to run a reasonable total number of post.json checkers.
parallelism = max(3, int(self.workspaces[name].slices / len(self.workspaces)))
t = Thread(
target=self.workspaces[name].update,
kwargs=dict(parallelism=parallelism),
name='Update ' + name,
)
t.daemon = True
t.start()
t_l.append(t)
for t in t_l:
t.join()
# These run one at a time, but they will spawn SLICES workers for
# reading and parsing files. (So unless workspaces are on different
# disks this is probably better.)
self.DataBase._update_begin()
for name in [self.current_workspace] + list(self.current_remote_workspaces):
self.DataBase._update_workspace(self.workspaces[name])
self.DataBase._update_finish(self.Methods.hash)
def initialise_jobs(self, setup):
""" Updata database, check deps, create jobids. """
return dependency.initialise_jobs(
setup,
self.workspaces[self.current_workspace], # target workspace
self.DataBase,
self.Methods,
)
def run_job(self, jobid, subjob_cookie=None, parent_pid=0):
""" Run analysis and synthesis for jobid in current workspace from WorkSpaceStorage """
W = self.workspaces[self.current_workspace]
#
active_workspaces = {}
for name in [self.current_workspace] + list(self.current_remote_workspaces):
active_workspaces[name] = self.workspaces[name].get_path()
slices = self.workspaces[self.current_workspace].get_slices()
t0 = time.time()
setup = update_setup(jobid, starttime=t0)
prof = setup.profile or DotDict()
new_prof, files, subjobs = dispatch.launch(W.path, setup, self.config, self.Methods, active_workspaces, slices, self.debug, self.daemon_url, subjob_cookie, parent_pid)
if self.debug:
delete_from = Temp.TEMP
else:
delete_from = Temp.DEBUG
for filename, temp in list(files.items()):
if temp >= delete_from:
unlink(join(W.path, jobid, filename))
del files[filename]
prof.update(new_prof)
prof.total = 0
prof.total = sum(v for v in prof.values() if isinstance(v, (float, int)))
data = dict(
starttime=t0,
endtime=time.time(),
profile=prof,
)
update_setup(jobid, **data)
data['files'] = files
data['subjobs'] = subjobs
json_save(data, resolve_jobid_filename(jobid, 'post.json'))
def get_methods(self):
return self.Methods.db
def method_info(self, method):
return self.Methods.db.get(method, '')