simple-data-ingester-adapter-memory 0.0.0-stage → 0.1.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/index.js +3 -0
- package/package.json +29 -4
- package/src/MemoryIngesterStore.js +195 -0
package/README.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# simple-data-ingester-adapter-memory
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An in-memory ingester registry for [simple-data-ingester](../../core): for an app that runs its API
|
|
4
|
+
and worker in one process without a database, and for tests.
|
|
5
|
+
|
|
6
|
+
```js
|
|
7
|
+
import { MemoryIngesterStore } from "simple-data-ingester-adapter-memory";
|
|
8
|
+
|
|
9
|
+
const store = new MemoryIngesterStore();
|
|
10
|
+
```
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simple-data-ingester-adapter-memory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"description": "An in-memory ingester registry for simple-data-ingester: one process, or tests",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "John Murphy-Teixidor",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./index.js"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "node --test"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"tag": "alpha"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"simple-data-ingester": "^0.1.0-alpha.3"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"simple-data-ingester": "file:../../core"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
import { IngesterStore } from "simple-data-ingester";
|
|
3
|
+
|
|
4
|
+
const {CONTINUOUS, ON_DEMAND} = IngesterStore.MODES;
|
|
5
|
+
|
|
6
|
+
const sameJSON = (a, b) => JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
|
|
7
|
+
|
|
8
|
+
const later = (a, b) => (!a ? b : !b ? a : (a > b ? a : b));
|
|
9
|
+
|
|
10
|
+
const text = value => (typeof value === "string" && value.trim()) || null;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The registry in this process's memory: for an app that runs its API and its worker together
|
|
14
|
+
* without a database, and for tests. Notifications are delivered asynchronously, as the other
|
|
15
|
+
* adapters' are.
|
|
16
|
+
*/
|
|
17
|
+
class MemoryIngesterStore extends IngesterStore {
|
|
18
|
+
constructor({now = () => new Date()} = {}) {
|
|
19
|
+
super();
|
|
20
|
+
|
|
21
|
+
this._rows = new Map();
|
|
22
|
+
this._events = new EventEmitter();
|
|
23
|
+
this._events.setMaxListeners(0);
|
|
24
|
+
this._now = now;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async request({id, name = null, description = null, config, worker, lease, count = true}) {
|
|
28
|
+
const now = this._now();
|
|
29
|
+
const leaseUntil = new Date(now.getTime() + lease);
|
|
30
|
+
const existing = this._rows.get(id);
|
|
31
|
+
|
|
32
|
+
if(!existing) {
|
|
33
|
+
const row = {
|
|
34
|
+
id, name: text(name) ?? id, description: text(description),
|
|
35
|
+
config, mode: ON_DEMAND, active: true, worker,
|
|
36
|
+
status: "UNKNOWN", state: null, error: null,
|
|
37
|
+
lease_until: leaseUntil, requested_at: now, request_count: count ? 1 : 0,
|
|
38
|
+
ready_at: null, cycled_at: null, reported_at: null, created: now, modified: now
|
|
39
|
+
};
|
|
40
|
+
this._write(null, row);
|
|
41
|
+
return {...this._rows.get(id), isNew: true};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const next = {...existing, requested_at: now, request_count: existing.request_count + (count ? 1 : 0)};
|
|
45
|
+
|
|
46
|
+
if(existing.mode === ON_DEMAND) {
|
|
47
|
+
next.config = config;
|
|
48
|
+
next.active = true;
|
|
49
|
+
next.lease_until = later(existing.lease_until, leaseUntil);
|
|
50
|
+
|
|
51
|
+
if(!existing.active) {
|
|
52
|
+
next.worker = worker;
|
|
53
|
+
next.status = "UNKNOWN";
|
|
54
|
+
next.error = null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this._write(existing, next);
|
|
59
|
+
return {...this._rows.get(id), isNew: false};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async put({id, name = undefined, description = undefined, config, worker, mode = CONTINUOUS, active = true, lease = undefined}) {
|
|
63
|
+
const now = this._now();
|
|
64
|
+
const existing = this._rows.get(id);
|
|
65
|
+
|
|
66
|
+
let leaseUntil = null;
|
|
67
|
+
if(mode === ON_DEMAND)
|
|
68
|
+
leaseUntil = lease !== undefined ? new Date(now.getTime() + lease) : existing?.lease_until ?? null;
|
|
69
|
+
|
|
70
|
+
const row = {
|
|
71
|
+
status: "UNKNOWN", state: null, error: null,
|
|
72
|
+
requested_at: null, request_count: 0, ready_at: null, cycled_at: null, reported_at: null, created: now,
|
|
73
|
+
...existing,
|
|
74
|
+
id, config, worker, mode, active,
|
|
75
|
+
name: name !== undefined || !existing ? text(name) ?? id : existing.name,
|
|
76
|
+
description: description !== undefined || !existing ? text(description) : existing.description,
|
|
77
|
+
lease_until: leaseUntil
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
this._write(existing ?? null, row);
|
|
81
|
+
return {...this._rows.get(id)};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async get(ids) {
|
|
85
|
+
return new Map(ids.filter(id => this._rows.has(id)).map(id => [id, {...this._rows.get(id)}]));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async activeRows(workerID) {
|
|
89
|
+
return Array.from(this._rows.values()).filter(row => row.active && row.worker === workerID).map(row => ({...row}));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async setStatus(id, workerID, {status, state = null, error = null}) {
|
|
93
|
+
const existing = this._rows.get(id);
|
|
94
|
+
if(!existing || !existing.active || existing.worker !== workerID)
|
|
95
|
+
return null;
|
|
96
|
+
|
|
97
|
+
const now = this._now();
|
|
98
|
+
const succeeded = IngesterStore.SUCCESS_STATUSES.includes(status);
|
|
99
|
+
|
|
100
|
+
const next = {
|
|
101
|
+
...existing, status, state, error,
|
|
102
|
+
ready_at: succeeded ? existing.ready_at ?? now : existing.ready_at,
|
|
103
|
+
cycled_at: succeeded ? now : existing.cycled_at,
|
|
104
|
+
reported_at: now
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
this._write(existing, next);
|
|
108
|
+
return {...this._rows.get(id)};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async expire(workerID) {
|
|
112
|
+
const now = this._now();
|
|
113
|
+
const expired = Array.from(this._rows.values())
|
|
114
|
+
.filter(row => row.worker === workerID && row.mode === ON_DEMAND && row.active && row.lease_until < now);
|
|
115
|
+
|
|
116
|
+
for(let row of expired)
|
|
117
|
+
this._write(row, {...row, active: false});
|
|
118
|
+
|
|
119
|
+
return expired.map(row => row.id);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async deactivate(id) {
|
|
123
|
+
const existing = this._rows.get(id);
|
|
124
|
+
if(existing?.active)
|
|
125
|
+
this._write(existing, {...existing, active: false});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async prune(olderThan) {
|
|
129
|
+
const cutoff = this._now().getTime() - olderThan;
|
|
130
|
+
let pruned = 0;
|
|
131
|
+
|
|
132
|
+
for(let row of Array.from(this._rows.values())) {
|
|
133
|
+
if(row.mode === ON_DEMAND && !row.active && (row.requested_at?.getTime() ?? 0) < cutoff) {
|
|
134
|
+
this._write(row, null);
|
|
135
|
+
pruned++;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return pruned;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async subscribeActivations(workerID, callback) {
|
|
143
|
+
const channel = `worker:${workerID}`;
|
|
144
|
+
this._events.on(channel, callback);
|
|
145
|
+
return async () => {
|
|
146
|
+
this._events.off(channel, callback);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async subscribeStatuses(callback) {
|
|
151
|
+
this._events.on("status", callback);
|
|
152
|
+
return async () => {
|
|
153
|
+
this._events.off("status", callback);
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Store a change, and send the notifications the other adapters send
|
|
159
|
+
*/
|
|
160
|
+
_write(before, after) {
|
|
161
|
+
if(after)
|
|
162
|
+
this._rows.set(after.id, {...after, modified: this._now()});
|
|
163
|
+
else
|
|
164
|
+
this._rows.delete(before.id);
|
|
165
|
+
|
|
166
|
+
const notify = (event, payload) => setImmediate(() => this._events.emit(event, payload));
|
|
167
|
+
|
|
168
|
+
if(before?.active && before.worker
|
|
169
|
+
&& (!after || !after.active || after.worker !== before.worker))
|
|
170
|
+
notify(`worker:${before.worker}`, {id: before.id, active: false});
|
|
171
|
+
|
|
172
|
+
if(after?.active && after.worker
|
|
173
|
+
&& (!before || !before.active || after.worker !== before.worker || !sameJSON(after.config, before.config)))
|
|
174
|
+
notify(`worker:${after.worker}`, {id: after.id, active: true});
|
|
175
|
+
|
|
176
|
+
if(before && after && (
|
|
177
|
+
before.status !== after.status
|
|
178
|
+
|| before.active !== after.active
|
|
179
|
+
|| before.ready_at !== after.ready_at
|
|
180
|
+
|| before.cycled_at !== after.cycled_at
|
|
181
|
+
|| before.reported_at !== after.reported_at
|
|
182
|
+
|| !sameJSON(before.error, after.error)
|
|
183
|
+
))
|
|
184
|
+
notify("status", {
|
|
185
|
+
id: after.id,
|
|
186
|
+
status: after.status,
|
|
187
|
+
active: after.active,
|
|
188
|
+
ready_at: after.ready_at,
|
|
189
|
+
cycled_at: after.cycled_at,
|
|
190
|
+
reported_at: after.reported_at
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export default MemoryIngesterStore;
|