@kapeta/local-cluster-service 0.15.2 → 0.15.3
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/CHANGELOG.md +7 -0
- package/dist/cjs/src/assetManager.js +2 -2
- package/dist/cjs/src/containerManager.js +0 -1
- package/dist/cjs/src/identities/routes.js +2 -1
- package/dist/cjs/src/instanceManager.d.ts +0 -2
- package/dist/cjs/src/instanceManager.js +12 -34
- package/dist/cjs/src/progressListener.d.ts +5 -6
- package/dist/cjs/src/progressListener.js +54 -36
- package/dist/cjs/src/repositoryManager.js +3 -3
- package/dist/cjs/src/socketManager.d.ts +18 -6
- package/dist/cjs/src/socketManager.js +35 -1
- package/dist/esm/src/assetManager.js +3 -3
- package/dist/esm/src/containerManager.js +0 -1
- package/dist/esm/src/identities/routes.js +2 -1
- package/dist/esm/src/instanceManager.d.ts +0 -2
- package/dist/esm/src/instanceManager.js +12 -34
- package/dist/esm/src/progressListener.d.ts +5 -6
- package/dist/esm/src/progressListener.js +53 -36
- package/dist/esm/src/repositoryManager.js +3 -3
- package/dist/esm/src/socketManager.d.ts +18 -6
- package/dist/esm/src/socketManager.js +34 -0
- package/package.json +1 -1
- package/src/assetManager.ts +4 -4
- package/src/containerManager.ts +0 -5
- package/src/identities/routes.ts +3 -1
- package/src/instanceManager.ts +18 -34
- package/src/progressListener.ts +59 -39
- package/src/repositoryManager.ts +6 -6
- package/src/socketManager.ts +44 -5
@@ -3,7 +3,7 @@ import request from 'request';
|
|
3
3
|
import AsyncLock from 'async-lock';
|
4
4
|
import { BlockInstanceRunner } from './utils/BlockInstanceRunner';
|
5
5
|
import { storageService } from './storageService';
|
6
|
-
import { socketManager } from './socketManager';
|
6
|
+
import { EVENT_INSTANCE_CREATED, EVENT_INSTANCE_EXITED, EVENT_STATUS_CHANGED, socketManager, } from './socketManager';
|
7
7
|
import { serviceManager } from './serviceManager';
|
8
8
|
import { assetManager } from './assetManager';
|
9
9
|
import { containerManager, HEALTH_CHECK_TIMEOUT } from './containerManager';
|
@@ -16,10 +16,6 @@ import { definitionsManager } from './definitionsManager';
|
|
16
16
|
import { Task, taskManager } from './taskManager';
|
17
17
|
const CHECK_INTERVAL = 5000;
|
18
18
|
const DEFAULT_HEALTH_PORT_TYPE = 'rest';
|
19
|
-
const EVENT_STATUS_CHANGED = 'status-changed';
|
20
|
-
const EVENT_INSTANCE_CREATED = 'instance-created';
|
21
|
-
const EVENT_INSTANCE_EXITED = 'instance-exited';
|
22
|
-
const EVENT_INSTANCE_LOG = 'instance-log';
|
23
19
|
const MIN_TIME_RUNNING = 30000; //If something didnt run for more than 30 secs - it failed
|
24
20
|
export class InstanceManager {
|
25
21
|
_interval = undefined;
|
@@ -115,11 +111,11 @@ export class InstanceManager {
|
|
115
111
|
if (existingInstance) {
|
116
112
|
const ix = this._instances.indexOf(existingInstance);
|
117
113
|
this._instances.splice(ix, 1, instance);
|
118
|
-
|
114
|
+
socketManager.emitSystemEvent(instance.systemId, EVENT_STATUS_CHANGED, instance);
|
119
115
|
}
|
120
116
|
else {
|
121
117
|
this._instances.push(instance);
|
122
|
-
|
118
|
+
socketManager.emitSystemEvent(instance.systemId, EVENT_INSTANCE_CREATED, instance);
|
123
119
|
}
|
124
120
|
this.save();
|
125
121
|
return instance;
|
@@ -162,7 +158,7 @@ export class InstanceManager {
|
|
162
158
|
if (healthUrl) {
|
163
159
|
instance.health = healthUrl;
|
164
160
|
}
|
165
|
-
|
161
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
166
162
|
}
|
167
163
|
else {
|
168
164
|
//If instance was not found - then we're receiving an externally started instance
|
@@ -178,7 +174,7 @@ export class InstanceManager {
|
|
178
174
|
address,
|
179
175
|
};
|
180
176
|
this._instances.push(instance);
|
181
|
-
|
177
|
+
socketManager.emitSystemEvent(systemId, EVENT_INSTANCE_CREATED, instance);
|
182
178
|
}
|
183
179
|
this.save();
|
184
180
|
return instance;
|
@@ -203,7 +199,7 @@ export class InstanceManager {
|
|
203
199
|
instance.status = InstanceStatus.STOPPED;
|
204
200
|
instance.pid = null;
|
205
201
|
instance.health = null;
|
206
|
-
|
202
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
207
203
|
this.save();
|
208
204
|
}
|
209
205
|
});
|
@@ -261,7 +257,7 @@ export class InstanceManager {
|
|
261
257
|
instance.desiredStatus = DesiredInstanceStatus.STOP;
|
262
258
|
}
|
263
259
|
instance.status = InstanceStatus.STOPPING;
|
264
|
-
|
260
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
265
261
|
console.log('Stopping instance: %s::%s [desired: %s]', systemId, instanceId, instance.desiredStatus);
|
266
262
|
this.save();
|
267
263
|
try {
|
@@ -272,7 +268,7 @@ export class InstanceManager {
|
|
272
268
|
try {
|
273
269
|
await container.stop();
|
274
270
|
instance.status = InstanceStatus.STOPPED;
|
275
|
-
|
271
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
276
272
|
this.save();
|
277
273
|
}
|
278
274
|
catch (e) {
|
@@ -291,7 +287,7 @@ export class InstanceManager {
|
|
291
287
|
}
|
292
288
|
process.kill(instance.pid, 'SIGTERM');
|
293
289
|
instance.status = InstanceStatus.STOPPED;
|
294
|
-
|
290
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
295
291
|
this.save();
|
296
292
|
}
|
297
293
|
catch (e) {
|
@@ -416,8 +412,8 @@ export class InstanceManager {
|
|
416
412
|
status: InstanceStatus.FAILED,
|
417
413
|
errorMessage: e.message ?? 'Failed to start - Check logs for details.',
|
418
414
|
});
|
419
|
-
|
420
|
-
|
415
|
+
socketManager.emitInstanceLog(systemId, instanceId, logs[0]);
|
416
|
+
socketManager.emitInstanceEvent(systemId, blockInstance.id, EVENT_INSTANCE_EXITED, {
|
421
417
|
error: `Failed to start instance: ${e.message}`,
|
422
418
|
status: EVENT_INSTANCE_EXITED,
|
423
419
|
instanceId: blockInstance.id,
|
@@ -506,7 +502,7 @@ export class InstanceManager {
|
|
506
502
|
const oldStatus = instance.status;
|
507
503
|
instance.status = newStatus;
|
508
504
|
console.log('Instance status changed: %s %s: %s -> %s', instance.systemId, instance.instanceId, oldStatus, instance.status);
|
509
|
-
|
505
|
+
socketManager.emitSystemEvent(instance.systemId, EVENT_STATUS_CHANGED, instance);
|
510
506
|
changed = true;
|
511
507
|
}
|
512
508
|
}
|
@@ -638,24 +634,6 @@ export class InstanceManager {
|
|
638
634
|
});
|
639
635
|
});
|
640
636
|
}
|
641
|
-
emitSystemEvent(systemId, type, payload) {
|
642
|
-
systemId = normalizeKapetaUri(systemId);
|
643
|
-
try {
|
644
|
-
socketManager.emit(`${systemId}/instances`, type, payload);
|
645
|
-
}
|
646
|
-
catch (e) {
|
647
|
-
console.warn('Failed to emit instance event: %s', e.message);
|
648
|
-
}
|
649
|
-
}
|
650
|
-
emitInstanceEvent(systemId, instanceId, type, payload) {
|
651
|
-
systemId = normalizeKapetaUri(systemId);
|
652
|
-
try {
|
653
|
-
socketManager.emit(`${systemId}/instances/${instanceId}`, type, payload);
|
654
|
-
}
|
655
|
-
catch (e) {
|
656
|
-
console.warn('Failed to emit instance event: %s', e.message);
|
657
|
-
}
|
658
|
-
}
|
659
637
|
}
|
660
638
|
export const instanceManager = new InstanceManager();
|
661
639
|
process.on('exit', async () => {
|
@@ -1,8 +1,9 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
-
|
3
|
-
|
4
|
-
private
|
5
|
-
constructor(
|
2
|
+
export declare class ProgressListener {
|
3
|
+
private readonly systemId;
|
4
|
+
private readonly instanceId;
|
5
|
+
constructor(systemId?: string, instanceId?: string);
|
6
|
+
private emitLog;
|
6
7
|
run(command: string, directory?: string): Promise<{
|
7
8
|
exit: number;
|
8
9
|
signal: NodeJS.Signals | null;
|
@@ -17,5 +18,3 @@ declare class ProgressListener {
|
|
17
18
|
info(msg: string, ...args: any[]): void;
|
18
19
|
debug(msg: string, ...args: any[]): void;
|
19
20
|
}
|
20
|
-
export declare const progressListener: ProgressListener;
|
21
|
-
export {};
|
@@ -1,16 +1,31 @@
|
|
1
1
|
import { spawn } from '@kapeta/nodejs-process';
|
2
2
|
import { socketManager } from './socketManager';
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
import { format } from 'node:util';
|
4
|
+
export class ProgressListener {
|
5
|
+
systemId;
|
6
|
+
instanceId;
|
7
|
+
constructor(systemId, instanceId) {
|
8
|
+
this.systemId = systemId;
|
9
|
+
this.instanceId = instanceId;
|
10
|
+
}
|
11
|
+
emitLog(payload) {
|
12
|
+
const logEntry = {
|
13
|
+
...payload,
|
14
|
+
source: 'stdout',
|
15
|
+
time: Date.now(),
|
16
|
+
};
|
17
|
+
if (this.systemId && this.instanceId) {
|
18
|
+
socketManager.emitInstanceLog(this.systemId, this.instanceId, logEntry);
|
19
|
+
return;
|
20
|
+
}
|
21
|
+
if (this.systemId) {
|
22
|
+
socketManager.emitSystemLog(this.systemId, logEntry);
|
23
|
+
return;
|
24
|
+
}
|
25
|
+
socketManager.emitGlobalLog(logEntry);
|
7
26
|
}
|
8
27
|
run(command, directory) {
|
9
|
-
this.
|
10
|
-
type: 'info',
|
11
|
-
message: `Running command "${command}"`,
|
12
|
-
});
|
13
|
-
const firstCommand = command.split(' ')[0];
|
28
|
+
this.info(`Running command "${command}"`);
|
14
29
|
return new Promise(async (resolve, reject) => {
|
15
30
|
try {
|
16
31
|
const chunks = [];
|
@@ -20,7 +35,10 @@ class ProgressListener {
|
|
20
35
|
shell: true,
|
21
36
|
});
|
22
37
|
child.onData((data) => {
|
23
|
-
this.
|
38
|
+
this.emitLog({
|
39
|
+
level: data.type === 'stdout' ? 'INFO' : 'WARN',
|
40
|
+
message: data.line,
|
41
|
+
});
|
24
42
|
});
|
25
43
|
if (child.process.stdout) {
|
26
44
|
child.process.stdout.on('data', (data) => {
|
@@ -29,25 +47,16 @@ class ProgressListener {
|
|
29
47
|
}
|
30
48
|
child.process.on('exit', (exit, signal) => {
|
31
49
|
if (exit !== 0) {
|
32
|
-
this.
|
33
|
-
type: 'info',
|
34
|
-
message: `"${command}" failed: "${exit}"`,
|
35
|
-
});
|
50
|
+
this.warn(`Command "${command}" failed: ${exit}`);
|
36
51
|
reject(new Error(`Command "${command}" exited with code ${exit}`));
|
37
52
|
}
|
38
53
|
else {
|
39
|
-
this.
|
40
|
-
type: 'info',
|
41
|
-
message: `Command OK: "${command}"`,
|
42
|
-
});
|
54
|
+
this.info(`Command OK: "${command}"`);
|
43
55
|
resolve({ exit, signal, output: Buffer.concat(chunks).toString() });
|
44
56
|
}
|
45
57
|
});
|
46
58
|
child.process.on('error', (err) => {
|
47
|
-
this.
|
48
|
-
type: 'info',
|
49
|
-
message: `"${command}" failed: "${err.message}"`,
|
50
|
-
});
|
59
|
+
this.warn(`"${command}" failed: "${err.message}"`);
|
51
60
|
reject(err);
|
52
61
|
});
|
53
62
|
await child.wait();
|
@@ -58,41 +67,49 @@ class ProgressListener {
|
|
58
67
|
});
|
59
68
|
}
|
60
69
|
async progress(label, callback) {
|
61
|
-
this.
|
70
|
+
this.info(`${label}: started`);
|
62
71
|
try {
|
63
72
|
const result = await callback();
|
64
|
-
this.
|
73
|
+
this.info(`${label}: done`);
|
65
74
|
return result;
|
66
75
|
}
|
67
76
|
catch (e) {
|
68
|
-
this.
|
69
|
-
type: 'info',
|
70
|
-
message: `${label}: failed. ${e.message}`,
|
71
|
-
});
|
77
|
+
this.warn(`${label}: failed. ${e.message}`);
|
72
78
|
throw e;
|
73
79
|
}
|
74
80
|
}
|
75
81
|
async check(message, ok) {
|
76
82
|
const wasOk = await ok;
|
77
|
-
this.
|
83
|
+
this.info(`${message}: ${wasOk}`);
|
78
84
|
}
|
79
85
|
start(label) {
|
80
|
-
this.
|
86
|
+
this.info(label);
|
81
87
|
}
|
82
88
|
showValue(label, value) {
|
83
|
-
this.
|
89
|
+
this.info(`${label}: ${value}`);
|
84
90
|
}
|
85
91
|
error(msg, ...args) {
|
86
|
-
this.
|
92
|
+
this.emitLog({
|
93
|
+
message: format(msg, args),
|
94
|
+
level: 'ERROR',
|
95
|
+
});
|
87
96
|
}
|
88
97
|
warn(msg, ...args) {
|
89
|
-
this.
|
98
|
+
this.emitLog({
|
99
|
+
message: format(msg, args),
|
100
|
+
level: 'WARN',
|
101
|
+
});
|
90
102
|
}
|
91
103
|
info(msg, ...args) {
|
92
|
-
this.
|
104
|
+
this.emitLog({
|
105
|
+
message: format(msg, args),
|
106
|
+
level: 'INFO',
|
107
|
+
});
|
93
108
|
}
|
94
109
|
debug(msg, ...args) {
|
95
|
-
this.
|
110
|
+
this.emitLog({
|
111
|
+
message: format(msg, args),
|
112
|
+
level: 'DEBUG',
|
113
|
+
});
|
96
114
|
}
|
97
115
|
}
|
98
|
-
export const progressListener = new ProgressListener(socketManager);
|
@@ -6,12 +6,12 @@ import FSExtra from 'fs-extra';
|
|
6
6
|
import ClusterConfiguration from '@kapeta/local-cluster-config';
|
7
7
|
import { parseKapetaUri } from '@kapeta/nodejs-utils';
|
8
8
|
import { socketManager } from './socketManager';
|
9
|
-
import { progressListener } from './progressListener';
|
10
9
|
import { Actions, Config, RegistryService } from '@kapeta/nodejs-registry-utils';
|
11
10
|
import { definitionsManager } from './definitionsManager';
|
12
11
|
import { taskManager } from './taskManager';
|
13
12
|
import { normalizeKapetaUri } from './utils/utils';
|
14
13
|
import { assetManager } from './assetManager';
|
14
|
+
import { ProgressListener } from './progressListener';
|
15
15
|
const EVENT_DEFAULT_PROVIDERS_START = 'default-providers-start';
|
16
16
|
const EVENT_DEFAULT_PROVIDERS_END = 'default-providers-end';
|
17
17
|
const DEFAULT_PROVIDERS = [
|
@@ -120,7 +120,7 @@ class RepositoryManager {
|
|
120
120
|
ensureDefaultProviders() {
|
121
121
|
socketManager.emitGlobal(EVENT_DEFAULT_PROVIDERS_START, { providers: DEFAULT_PROVIDERS });
|
122
122
|
const tasks = this._install(DEFAULT_PROVIDERS);
|
123
|
-
Promise.allSettled(tasks.map(t => t.wait())).then(() => {
|
123
|
+
Promise.allSettled(tasks.map((t) => t.wait())).then(() => {
|
124
124
|
socketManager.emitGlobal(EVENT_DEFAULT_PROVIDERS_END, {});
|
125
125
|
});
|
126
126
|
}
|
@@ -142,7 +142,7 @@ class RepositoryManager {
|
|
142
142
|
process.chdir(os.tmpdir());
|
143
143
|
//Disable change events while installing
|
144
144
|
this.setChangeEventsEnabled(false);
|
145
|
-
await Actions.install(
|
145
|
+
await Actions.install(new ProgressListener(), [ref], {});
|
146
146
|
}
|
147
147
|
catch (e) {
|
148
148
|
console.error(`Failed to install asset: ${ref}`, e);
|
@@ -1,16 +1,28 @@
|
|
1
|
-
import {
|
1
|
+
import { Server } from 'socket.io';
|
2
|
+
import { LogEntry } from './types';
|
3
|
+
export declare const EVENT_STATUS_CHANGED = "status-changed";
|
4
|
+
export declare const EVENT_INSTANCE_CREATED = "instance-created";
|
5
|
+
export declare const EVENT_INSTANCE_EXITED = "instance-exited";
|
6
|
+
export declare const EVENT_INSTANCE_LOG = "instance-log";
|
7
|
+
export declare const EVENT_SYSTEM_LOG = "system-log";
|
8
|
+
export declare const EVENT_LOG = "log";
|
2
9
|
export declare class SocketManager {
|
3
10
|
private _io;
|
4
|
-
private _sockets;
|
11
|
+
private readonly _sockets;
|
5
12
|
constructor();
|
6
13
|
setIo(io: Server): void;
|
7
14
|
isAlive(): boolean;
|
8
15
|
private get io();
|
9
16
|
emit(context: string, type: string, payload: any): void;
|
10
17
|
emitGlobal(type: string, payload: any): void;
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
18
|
+
emitSystemEvent(systemId: string, type: string, payload: any): void;
|
19
|
+
emitInstanceLog(systemId: string, instanceId: string, payload: LogEntry): void;
|
20
|
+
emitSystemLog(systemId: string, payload: LogEntry): void;
|
21
|
+
emitGlobalLog(payload: LogEntry): void;
|
22
|
+
emitInstanceEvent(systemId: string, instanceId: string, type: string, payload: any): void;
|
23
|
+
private _bindIO;
|
24
|
+
private _handleSocketCreated;
|
25
|
+
private _bindSocket;
|
26
|
+
private _handleSocketDestroyed;
|
15
27
|
}
|
16
28
|
export declare const socketManager: SocketManager;
|
@@ -1,4 +1,11 @@
|
|
1
1
|
import _ from 'lodash';
|
2
|
+
import { normalizeKapetaUri } from './utils/utils';
|
3
|
+
export const EVENT_STATUS_CHANGED = 'status-changed';
|
4
|
+
export const EVENT_INSTANCE_CREATED = 'instance-created';
|
5
|
+
export const EVENT_INSTANCE_EXITED = 'instance-exited';
|
6
|
+
export const EVENT_INSTANCE_LOG = 'instance-log';
|
7
|
+
export const EVENT_SYSTEM_LOG = 'system-log';
|
8
|
+
export const EVENT_LOG = 'log';
|
2
9
|
export class SocketManager {
|
3
10
|
_io;
|
4
11
|
_sockets;
|
@@ -26,6 +33,33 @@ export class SocketManager {
|
|
26
33
|
emitGlobal(type, payload) {
|
27
34
|
this.io.emit(type, payload);
|
28
35
|
}
|
36
|
+
emitSystemEvent(systemId, type, payload) {
|
37
|
+
systemId = normalizeKapetaUri(systemId);
|
38
|
+
try {
|
39
|
+
socketManager.emit(`${systemId}/instances`, type, payload);
|
40
|
+
}
|
41
|
+
catch (e) {
|
42
|
+
console.warn('Failed to emit instance event: %s', e.message);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
emitInstanceLog(systemId, instanceId, payload) {
|
46
|
+
this.emitInstanceEvent(systemId, instanceId, EVENT_INSTANCE_LOG, payload);
|
47
|
+
}
|
48
|
+
emitSystemLog(systemId, payload) {
|
49
|
+
this.emitSystemEvent(systemId, EVENT_SYSTEM_LOG, payload);
|
50
|
+
}
|
51
|
+
emitGlobalLog(payload) {
|
52
|
+
this.emitGlobal(EVENT_LOG, payload);
|
53
|
+
}
|
54
|
+
emitInstanceEvent(systemId, instanceId, type, payload) {
|
55
|
+
systemId = normalizeKapetaUri(systemId);
|
56
|
+
try {
|
57
|
+
socketManager.emit(`${systemId}/instances/${instanceId}`, type, payload);
|
58
|
+
}
|
59
|
+
catch (e) {
|
60
|
+
console.warn('Failed to emit instance event: %s', e.message);
|
61
|
+
}
|
62
|
+
}
|
29
63
|
_bindIO() {
|
30
64
|
this.io.on('connection', (socket) => this._handleSocketCreated(socket));
|
31
65
|
}
|
package/package.json
CHANGED
package/src/assetManager.ts
CHANGED
@@ -3,9 +3,9 @@ import FS from 'node:fs';
|
|
3
3
|
import FSExtra from 'fs-extra';
|
4
4
|
import YAML from 'yaml';
|
5
5
|
import NodeCache from 'node-cache';
|
6
|
-
import
|
6
|
+
import { Definition, DefinitionInfo } from '@kapeta/local-cluster-config';
|
7
7
|
import { codeGeneratorManager } from './codeGeneratorManager';
|
8
|
-
import {
|
8
|
+
import { ProgressListener } from './progressListener';
|
9
9
|
import { parseKapetaUri } from '@kapeta/nodejs-utils';
|
10
10
|
import { repositoryManager } from './repositoryManager';
|
11
11
|
import { BlockDefinition } from '@kapeta/schemas';
|
@@ -209,7 +209,7 @@ class AssetManager {
|
|
209
209
|
|
210
210
|
const assetInfos = YAML.parseAllDocuments(FS.readFileSync(filePath).toString()).map((doc) => doc.toJSON());
|
211
211
|
|
212
|
-
await Actions.link(
|
212
|
+
await Actions.link(new ProgressListener(), Path.dirname(filePath));
|
213
213
|
|
214
214
|
const version = 'local';
|
215
215
|
const refs = assetInfos.map((assetInfo) => `kapeta://${assetInfo.metadata.name}:${version}`);
|
@@ -223,7 +223,7 @@ class AssetManager {
|
|
223
223
|
throw new Error('Asset does not exists: ' + ref);
|
224
224
|
}
|
225
225
|
this.cache.flushAll();
|
226
|
-
await Actions.uninstall(
|
226
|
+
await Actions.uninstall(new ProgressListener(), [asset.ref]);
|
227
227
|
}
|
228
228
|
|
229
229
|
async installAsset(ref: string) {
|
package/src/containerManager.ts
CHANGED
@@ -11,14 +11,9 @@ import uuid from 'node-uuid';
|
|
11
11
|
import md5 from 'md5';
|
12
12
|
import { getBlockInstanceContainerName } from './utils/utils';
|
13
13
|
import { InstanceInfo, LogEntry, LogSource } from './types';
|
14
|
-
import { socketManager } from './socketManager';
|
15
|
-
import { handlers as ArtifactHandlers } from '@kapeta/nodejs-registry-utils';
|
16
|
-
import { progressListener } from './progressListener';
|
17
14
|
import { KapetaAPI } from '@kapeta/nodejs-api-client';
|
18
15
|
import { taskManager, Task } from './taskManager';
|
19
16
|
|
20
|
-
const EVENT_IMAGE_PULL = 'docker-image-pull';
|
21
|
-
|
22
17
|
type StringMap = { [key: string]: string };
|
23
18
|
|
24
19
|
export type PortMap = {
|
package/src/identities/routes.ts
CHANGED
@@ -5,12 +5,13 @@ import { corsHandler } from '../middleware/cors';
|
|
5
5
|
import { Request, Response } from 'express';
|
6
6
|
|
7
7
|
const router = Router();
|
8
|
-
|
8
|
+
|
9
9
|
|
10
10
|
router.use('/', corsHandler);
|
11
11
|
|
12
12
|
router.get('/current', async (req: Request, res: Response) => {
|
13
13
|
try {
|
14
|
+
const api = new KapetaAPI();
|
14
15
|
if (api.hasToken()) {
|
15
16
|
res.send(await api.getCurrentIdentity());
|
16
17
|
} else {
|
@@ -23,6 +24,7 @@ router.get('/current', async (req: Request, res: Response) => {
|
|
23
24
|
|
24
25
|
router.get('/:identityId/memberships', async (req: Request, res: Response) => {
|
25
26
|
try {
|
27
|
+
const api = new KapetaAPI();
|
26
28
|
if (api.hasToken()) {
|
27
29
|
res.send(await api.getMemberships(req.params.identityId));
|
28
30
|
} else {
|
package/src/instanceManager.ts
CHANGED
@@ -3,7 +3,13 @@ import request from 'request';
|
|
3
3
|
import AsyncLock from 'async-lock';
|
4
4
|
import { BlockInstanceRunner } from './utils/BlockInstanceRunner';
|
5
5
|
import { storageService } from './storageService';
|
6
|
-
import {
|
6
|
+
import {
|
7
|
+
EVENT_INSTANCE_CREATED,
|
8
|
+
EVENT_INSTANCE_EXITED,
|
9
|
+
EVENT_INSTANCE_LOG,
|
10
|
+
EVENT_STATUS_CHANGED,
|
11
|
+
socketManager,
|
12
|
+
} from './socketManager';
|
7
13
|
import { serviceManager } from './serviceManager';
|
8
14
|
import { assetManager } from './assetManager';
|
9
15
|
import { containerManager, HEALTH_CHECK_TIMEOUT } from './containerManager';
|
@@ -19,10 +25,6 @@ import { Task, taskManager } from './taskManager';
|
|
19
25
|
const CHECK_INTERVAL = 5000;
|
20
26
|
const DEFAULT_HEALTH_PORT_TYPE = 'rest';
|
21
27
|
|
22
|
-
const EVENT_STATUS_CHANGED = 'status-changed';
|
23
|
-
const EVENT_INSTANCE_CREATED = 'instance-created';
|
24
|
-
const EVENT_INSTANCE_EXITED = 'instance-exited';
|
25
|
-
const EVENT_INSTANCE_LOG = 'instance-log';
|
26
28
|
const MIN_TIME_RUNNING = 30000; //If something didnt run for more than 30 secs - it failed
|
27
29
|
|
28
30
|
export class InstanceManager {
|
@@ -153,10 +155,10 @@ export class InstanceManager {
|
|
153
155
|
if (existingInstance) {
|
154
156
|
const ix = this._instances.indexOf(existingInstance);
|
155
157
|
this._instances.splice(ix, 1, instance);
|
156
|
-
|
158
|
+
socketManager.emitSystemEvent(instance.systemId, EVENT_STATUS_CHANGED, instance);
|
157
159
|
} else {
|
158
160
|
this._instances.push(instance);
|
159
|
-
|
161
|
+
socketManager.emitSystemEvent(instance.systemId, EVENT_INSTANCE_CREATED, instance);
|
160
162
|
}
|
161
163
|
|
162
164
|
this.save();
|
@@ -222,7 +224,7 @@ export class InstanceManager {
|
|
222
224
|
instance.health = healthUrl;
|
223
225
|
}
|
224
226
|
|
225
|
-
|
227
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
226
228
|
} else {
|
227
229
|
//If instance was not found - then we're receiving an externally started instance
|
228
230
|
instance = {
|
@@ -239,7 +241,7 @@ export class InstanceManager {
|
|
239
241
|
|
240
242
|
this._instances.push(instance);
|
241
243
|
|
242
|
-
|
244
|
+
socketManager.emitSystemEvent(systemId, EVENT_INSTANCE_CREATED, instance);
|
243
245
|
}
|
244
246
|
|
245
247
|
this.save();
|
@@ -268,7 +270,7 @@ export class InstanceManager {
|
|
268
270
|
instance.status = InstanceStatus.STOPPED;
|
269
271
|
instance.pid = null;
|
270
272
|
instance.health = null;
|
271
|
-
|
273
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
272
274
|
this.save();
|
273
275
|
}
|
274
276
|
});
|
@@ -343,7 +345,7 @@ export class InstanceManager {
|
|
343
345
|
|
344
346
|
instance.status = InstanceStatus.STOPPING;
|
345
347
|
|
346
|
-
|
348
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
347
349
|
console.log('Stopping instance: %s::%s [desired: %s]', systemId, instanceId, instance.desiredStatus);
|
348
350
|
this.save();
|
349
351
|
|
@@ -355,7 +357,7 @@ export class InstanceManager {
|
|
355
357
|
try {
|
356
358
|
await container.stop();
|
357
359
|
instance.status = InstanceStatus.STOPPED;
|
358
|
-
|
360
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
359
361
|
this.save();
|
360
362
|
} catch (e) {
|
361
363
|
console.error('Failed to stop container', e);
|
@@ -374,7 +376,7 @@ export class InstanceManager {
|
|
374
376
|
|
375
377
|
process.kill(instance.pid as number, 'SIGTERM');
|
376
378
|
instance.status = InstanceStatus.STOPPED;
|
377
|
-
|
379
|
+
socketManager.emitSystemEvent(systemId, EVENT_STATUS_CHANGED, instance);
|
378
380
|
this.save();
|
379
381
|
} catch (e) {
|
380
382
|
console.error('Failed to stop process', e);
|
@@ -528,9 +530,9 @@ export class InstanceManager {
|
|
528
530
|
errorMessage: e.message ?? 'Failed to start - Check logs for details.',
|
529
531
|
});
|
530
532
|
|
531
|
-
|
533
|
+
socketManager.emitInstanceLog(systemId, instanceId, logs[0]);
|
532
534
|
|
533
|
-
|
535
|
+
socketManager.emitInstanceEvent(systemId, blockInstance.id, EVENT_INSTANCE_EXITED, {
|
534
536
|
error: `Failed to start instance: ${e.message}`,
|
535
537
|
status: EVENT_INSTANCE_EXITED,
|
536
538
|
instanceId: blockInstance.id,
|
@@ -645,7 +647,7 @@ export class InstanceManager {
|
|
645
647
|
oldStatus,
|
646
648
|
instance.status
|
647
649
|
);
|
648
|
-
|
650
|
+
socketManager.emitSystemEvent(instance.systemId, EVENT_STATUS_CHANGED, instance);
|
649
651
|
changed = true;
|
650
652
|
}
|
651
653
|
}
|
@@ -802,24 +804,6 @@ export class InstanceManager {
|
|
802
804
|
});
|
803
805
|
});
|
804
806
|
}
|
805
|
-
|
806
|
-
private emitSystemEvent(systemId: string, type: string, payload: any) {
|
807
|
-
systemId = normalizeKapetaUri(systemId);
|
808
|
-
try {
|
809
|
-
socketManager.emit(`${systemId}/instances`, type, payload);
|
810
|
-
} catch (e: any) {
|
811
|
-
console.warn('Failed to emit instance event: %s', e.message);
|
812
|
-
}
|
813
|
-
}
|
814
|
-
|
815
|
-
private emitInstanceEvent(systemId: string, instanceId: string, type: string, payload: any) {
|
816
|
-
systemId = normalizeKapetaUri(systemId);
|
817
|
-
try {
|
818
|
-
socketManager.emit(`${systemId}/instances/${instanceId}`, type, payload);
|
819
|
-
} catch (e: any) {
|
820
|
-
console.warn('Failed to emit instance event: %s', e.message);
|
821
|
-
}
|
822
|
-
}
|
823
807
|
}
|
824
808
|
|
825
809
|
export const instanceManager = new InstanceManager();
|