-
Notifications
You must be signed in to change notification settings - Fork 19
/
computation-service.js
259 lines (229 loc) · 7.48 KB
/
computation-service.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'use strict';
/**
* @module computation-service
*/
const common = require('coinstac-common');
const Computation = common.models.computation.Computation;
const crypto = require('crypto');
const deepEqual = require('deep-equal');
const getSyncedDatabase = common.utils.getSyncedDatabase;
const ModelService = require('../model-service');
const RemoteComputationResult = common.models.computation.RemoteComputationResult;
/**
* @extends ModelService
*/
class ComputationService extends ModelService {
modelServiceHooks() { // eslint-disable-line class-methods-use-this
return {
dbName: 'computations',
ModelType: Computation,
};
}
/**
* Can a computation start?
*
* @params {string} consortiumId
* @returns {Promise} Resolves if a computation *can* start or rejects with an
* error if a computation *can not* start.
*/
canStartComputation(consortiumId) {
const client = this.client;
return getSyncedDatabase(
client.dbRegistry,
`remote-consortium-${consortiumId}`
)
.then(remoteDatabase => Promise.all([
client.consortia.get(consortiumId),
remoteDatabase.find({
selector: {
complete: false,
},
}),
]))
.then(([consortium, docs]) => {
const activeComputationId = consortium.activeComputationId;
const isConsortiumOwner = consortium.owners.indexOf(client.auth.getUser().username) > -1;
if (!activeComputationId) {
throw new Error(
`Consortium "${consortium.label}" doesn't have an active computation`
);
}
if (!isConsortiumOwner) {
throw new Error('Only consortium owners can start a computation');
}
/**
* This enforces one run per consortium.
*
* @todo Either move this functionality into coinstac-common or refactor
* UI so consortia may run multiple computations.
*/
if (docs.length) {
throw new Error('Only one computation may run at a time');
}
});
}
checkProjectCompInputs({ consortiumId, projectId }) {
const { consortia, projects } = this.client;
return Promise.all([
consortia.get(consortiumId),
projects.setMetaContents(projectId),
])
.then(([consortium, project]) => {
/**
* Ensure project's computation inputs match the consortium; if not,
* throw an error and make the user re-match.
*
* @todo Find better method for guaranteeing project-to-computation
* input alignment.
*
* {@link https://github.com/MRN-Code/coinstac/issues/151}
*/
if (
!deepEqual(
consortium.activeComputationInputs,
project.computationInputs
)
) {
throw new Error(
`Project ${project.name}'s inputs must be re-entered`
);
}
return [consortium, project];
});
}
/**
* Call the local pipeline runner pool's `triggerRunner`.
* @private
*
* @param {Object} options
* @param {string} options.consortiumId Consortium the runId is from
* @param {string} options.projectId Project to run on
* @param {string} options.runId
* @returns {Promise}
*/
doTriggerRunner({ consortiumId, projectId, runId }) {
const { pool } = this.client;
if (!consortiumId) {
return Promise.reject(new Error('Consortium ID required'));
} else if (!projectId) {
return Promise.reject(new Error('Project ID required'));
} else if (!runId) {
return Promise.reject(new Error('Computation run ID required'));
}
return this.checkProjectCompInputs({ consortiumId, projectId })
.then(([consortium, project]) => {
const options = {
_id: runId,
computationId: consortium.activeComputationId,
computationInputs: consortium.activeComputationInputs,
consortiumId,
};
if (
consortium.activeComputationInputs &&
Array.isArray(consortium.activeComputationInputs)
) {
options.pluginState = {
inputs: consortium.activeComputationInputs,
};
}
const result = new RemoteComputationResult(options);
return pool.triggerRunner(result, project);
});
}
/**
* Kick off a remote computation result.
*
* @param {Object} options
* @param {string} options.consortiumId
* @param {string} options.projectId
* @returns {Promise}
*/
kickoff({ consortiumId, projectId }) {
return this.canStartComputation(consortiumId)
.then(() => this.client.consortia.get(consortiumId))
.then(({ activeComputationId }) => {
const runId = crypto.createHash('md5')
.update(`${consortiumId}${activeComputationId}${Date.now()}`)
.digest('hex');
return this.doTriggerRunner({ consortiumId, projectId, runId });
});
}
/**
* Join an already in progress computation.
*
* @param {Object} options
* @param {string} options.consortiumId Consortium the runId is from
* @param {string} options.projectId Project to run on
* @param {string} options.runId
* @return {Promise}
*/
joinRun({ consortiumId, projectId, runId }) {
return this.doTriggerRunner({ consortiumId, projectId, runId });
}
/**
* Allows the client to join a run for which they have no started document for.
* @param {string} options.consortiumId Consortium the runId is from
* @param {string} options.projectId project to run on
* @param {string} options.runId
* @return {Promise}
*/
joinSlavedRun({ consortiumId, projectId, runId }) {
const { pool } = this.client;
this.checkProjectCompInputs({ consortiumId, projectId })
.then(([consortium, project]) => {
return Promise.all([
consortium,
project,
this.client.dbRegistry.get(`remote-consortium-${consortiumId}`).get(runId),
]);
})
.then(([consortium, project, resultDoc]) => {
const options = {};
if (
consortium.activeComputationInputs &&
Array.isArray(consortium.activeComputationInputs) &&
!resultDoc.pluginState.inputs
) {
options.pluginState = {
inputs: consortium.activeComputationInputs,
};
}
const result = new RemoteComputationResult(Object.assign({}, resultDoc, options));
return pool.triggerRunner(result, project);
});
}
/**
* Determine whether the user should join a computation's run.
*
* @param {string} consortiumId
* @param {boolean} notFirstRun
* @returns {Promise} Resolves to a boolean
*/
shouldJoinRun(consortiumId, notFirstRun) {
const { auth, consortia, dbRegistry } = this.client;
return Promise.all([
/**
* @todo coinstac-storage-proxy doesn't allow GET requests to
* `local-consortium-*` databases. Figure out another approach.
*/
dbRegistry.get(`local-consortium-${consortiumId}`).all(),
consortia.getActiveRunId(consortiumId),
])
.then(([localDocs, runId]) => {
if (!runId) {
return false;
}
const { username } = auth.getUser();
// Determine whether the user has a doc with the run ID:
if (notFirstRun) {
return !localDocs.find(({ _id }) => {
return _id.indexOf(runId) > -1 && _id.indexOf(username) > -1;
});
}
// first time this run has been joined since init
// allow resume/first join
return true;
});
}
}
module.exports = ComputationService;