simple-data-ingester-adapter-memory 0.0.0-stage → 0.1.0-alpha.1

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