-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsnapshot.js
37 lines (34 loc) · 1.13 KB
/
snapshot.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
const now = require('performance-now');
const storageManager = require('@hkube/storage-manager');
const TYPE = 'algorithmQueue';
class Snapshot {
async store({ key, data, onStart, onEnd, onError }) {
try {
const start = now();
onStart({ key, length: data.length });
await storageManager.hkubePersistency.put({ type: TYPE, name: key, data });
const end = now();
const timeTook = (end - start).toFixed(3);
onEnd({ key, length: data.length, timeTook });
}
catch (e) {
onError({ key, length: data.length, error: e.message });
}
}
async get({ key, onStart, onEnd, onError }) {
let data;
try {
const start = now();
onStart({ key });
data = await storageManager.hkubePersistency.get({ type: TYPE, name: key });
const end = now();
const timeTook = (end - start).toFixed(3);
onEnd({ key, timeTook });
}
catch (e) {
onError({ key, error: e.message });
}
return data;
}
}
module.exports = new Snapshot();