-
Notifications
You must be signed in to change notification settings - Fork 1
/
other-lanes.js
379 lines (328 loc) · 9.08 KB
/
other-lanes.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
let _;
let Lanes;
let Harbors;
let Shipments;
const name = 'other lanes';
const pkgs = ['lodash'];
const prune_followup = (lane, observers, complete, downstream) => {
console.log(
`Pruning unused followup "${lane.followup.name}" from tracking due to non-zero exit from ${lane.name}`
);
observers[lane.followup._id].stop();
delete observers[lane.followup._id];
delete complete[lane.followup._id];
_.remove(downstream, (followup) => {
return followup._id == lane.followup._id;
});
if (lane.followup?.followup) {
prune_followup(lane.followup, observers, complete, downstream);
}
if (lane.salvage_plan?.salvage_plan) {
prune_salvage_plan(lane.salvage_plan, observers, complete, downstream);
}
};
const prune_salvage_plan = (lane, observers, complete, downstream) => {
console.log(
`Pruning unused salvage plan "${lane.salvage_plan.name}" from tracking due to successful exit from ${lane.name}`
);
observers[lane.salvage_plan._id]?.stop();
delete observers[lane.salvage_plan._id];
delete complete[lane.salvage_plan._id];
_.remove(downstream, (salvage_plan) => {
return salvage_plan._id == lane.salvage_plan._id;
});
if (lane.followup?.followup) {
prune_followup(lane.followup, observers, complete, downstream);
}
if (lane.salvage_plan?.salvage_plan) {
prune_salvage_plan(lane.salvage_plan, observers, complete, downstream);
}
};
const check_completion = function (
ref_lane,
complete,
targets,
downstream,
total_complete,
exit_code,
manifest
) {
console.log('Checking completion for lane:', ref_lane.name);
let all_shipments_successful = _.every(complete, function (value) {
return value === 0;
});
let total_length = targets.length + downstream.length;
if (total_complete.count == total_length && all_shipments_successful) {
console.log('Shipment successful for lane:', ref_lane.name);
exit_code = 0;
}
if (total_complete.count == total_length) {
console.log('Ending shipment for lane:', ref_lane.name);
$H.end_shipment(ref_lane, exit_code, manifest);
}
return all_shipments_successful;
};
const collect_downstream = function (target_lane) {
let collected = [];
if (target_lane.followup) {
let followup_lane = Lanes.findOne(target_lane.followup._id);
collected.push(followup_lane);
collected = collected.concat(collect_downstream(followup_lane));
}
if (target_lane.salvage_plan) {
let salvage_plan_lane = Lanes.findOne(target_lane.salvage_plan._id);
collected.push(salvage_plan_lane);
collected = collected.concat(collect_downstream(salvage_plan_lane));
}
return _.uniqBy(collected, '_id');
};
const verify_lane_key = function (value, key) {
if (
value &&
key != 'shipment_start_date' &&
key != 'prior_manifest' &&
key != 'follow_charter' &&
key != 'shipment_id' &&
key != 'timestamp'
) return true;
return false;
};
const collect_target_lists = function (manifest, targets, downstream) {
_.each(manifest, function (value, key) {
let target_lane;
if ( verify_lane_key(value, key) ) {
target_lane = Lanes.findOne(key);
targets.push(target_lane);
}
if (target_lane && manifest.follow_charter) {
downstream = downstream.concat(collect_downstream(target_lane));
}
});
return { targets, downstream };
};
const check_shipment_status = function (
lane_id,
ref_shipment,
updated_shipment,
observer,
total_complete,
observers,
complete,
targets,
downstream,
exit_code,
manifest,
ref_lane
) {
let lane = Lanes.findOne(lane_id);
if (
updated_shipment.active == false &&
(updated_shipment.exit_code == 0 || updated_shipment.exit_code) &&
complete[lane_id] === false
) {
total_complete.count++;
complete[lane_id] = updated_shipment.exit_code;
let shipment_link = `/lanes/${
lane.name
}/ship/${
updated_shipment.start
}`;
let link_classes = `exit-code code-${updated_shipment.exit_code}`;
let result = `<a
class="${link_classes}"
href="${shipment_link}"
>Lane "${
lane.name
}" exited with code: ${
updated_shipment.exit_code
}</a>`;
const date = new Date();
switch (updated_shipment.exit_code) {
case 0:
if (lane.salvage_plan) {
prune_salvage_plan(lane, observers, complete, downstream);
}
break;
default:
if (lane.followup) {
prune_followup(lane, observers, complete, downstream);
}
break;
}
observer.stop();
let { stdout } = Shipments.findOne(ref_shipment._id);
stdout[date] = stdout[date] ? `${stdout[date]}\n${result}` : result;
ref_shipment.stdout = stdout;
Shipments.update(ref_shipment._id, ref_shipment);
}
if (updated_shipment.exit_code) {
return $H.end_shipment(lane, updated_shipment.exit_code, manifest);
}
return check_completion(
ref_lane,
complete,
targets,
downstream,
total_complete,
exit_code,
manifest
);
};
const observe_targets = function (
targets,
downstream,
complete,
observers,
ref_shipment,
total_complete,
complete,
exit_code,
manifest,
ref_lane
) {
let the_observed = targets.concat(downstream);
_.each(the_observed, function (observed) {
complete[observed._id] = false;
const cursor = Shipments.find({ lane: observed._id });
observers[observed._id] = cursor.observe({
changed: function (updated_shipment) {
let lane_id = updated_shipment.lane;
check_shipment_status(
lane_id,
ref_shipment,
updated_shipment,
observers[observed._id],
total_complete,
observers,
complete,
targets,
downstream,
exit_code,
manifest,
ref_lane
);
},
});
});
return observers;
};
const start_shipments = (targets, start_date) => {
_.each(targets, (target_lane) => {
const harbor = Harbors.findOne(target_lane.type);
let target_manifest = harbor.lanes[target_lane._id].manifest;
$H.start_shipment(target_lane._id, target_manifest, start_date);
});
};
module.exports = {
prune_followup,
prune_salvage_plan,
check_completion,
collect_downstream,
verify_lane_key,
collect_target_lists,
check_shipment_status,
observe_targets,
start_shipments,
next: () => (_ = require('lodash')),
render_input: function (values, rendered_lane) {
let lanes = Lanes.find({}, { sort: { name: 1 } }).fetch().map((lane) => {
if (rendered_lane && lane._id != rendered_lane._id) {
return `
<li>
<label>
<input
name=${lane._id}
type=checkbox
${values && values[lane._id] ? 'checked' : ''}
>
${lane.name}
</label>
</li>
`;
}
return ``;
}).join('');
if (! lanes.length) return '<p>(No other lanes found.)</p>';
return `
<p>To which other lanes would you like to ship?</p>
<ul class="lane-input-list">
${lanes}
</ul>
<label>
<input
name=follow_charter
type=checkbox
${values && values.follow_charter ? 'checked' : ''}
>
Follow Charter?
</label>
`;
},
render_work_preview: function (manifest) {
return `
<p>This shipment will start shipments to the following lanes:</p>
<ul class="other-lane-list">
${Object.keys(manifest).map(key => {
let lane;
if (manifest[key] == 'on') lane = Lanes.findOne(key);
if (lane) {
return `
<li>
<a
href="/lanes/${lane.name}/ship"
class="success button"
>${lane.name}</a>
<a
href="/lanes/${lane.name}/charter"
class="button"
>charter</a>
</li>
`;
}
return '';
}).join('')}
</ul>
<p>Each of these lanes' charter will ${
manifest.follow_charter ? '' : '<em>not</em> '
}be tracked.</p>
`;
},
register: function (lanes, users, harbors, shipments) {
Lanes = lanes;
Users = users;
Harbors = harbors;
Shipments = shipments;
return { name, pkgs };
},
update: function (lane, values) {
if (! values[lane._id]) return true;
return false;
},
work: function (ref_lane, manifest) {
let ref_shipment = Shipments.findOne(manifest.shipment_id);
let total_complete = { count: 0 };
let complete = {};
let observers = {};
let targets = [];
let downstream = [];
let exit_code = 1;
const start_date = manifest.shipment_start_date;
const lists = collect_target_lists(manifest, targets, downstream);
targets = lists.targets;
downstream = lists.downstream;
observers = observe_targets(
targets,
downstream,
complete,
observers,
ref_shipment,
total_complete,
complete,
exit_code,
manifest,
ref_lane
);
start_shipments(targets, start_date);
return manifest;
},
};