Skip to content

Commit d840bcf

Browse files
committed
Move InstanceStatus to enums. Send instance event without saparate status
1 parent 0704147 commit d840bcf

File tree

11 files changed

+55
-47
lines changed

11 files changed

+55
-47
lines changed

packages/host/src/lib/cpm-connector.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22
import { Readable } from "stream";
33
import * as http from "http";
44

5-
import { CPMMessageCode, InstanceMessageCode, SequenceMessageCode } from "@scramjet/symbols";
5+
import { CPMMessageCode, SequenceMessageCode } from "@scramjet/symbols";
66
import {
77
STHRestAPI,
88
CPMConnectorOptions,
@@ -582,14 +582,12 @@ export class CPMConnector extends TypedEmitter<Events> {
582582
* @param {string} instance Instance details.
583583
* @param {SequenceMessageCode} instanceStatus Instance status.
584584
*/
585-
async sendInstanceInfo(instance: Instance, instanceStatus: InstanceMessageCode): Promise<void> {
586-
this.logger.trace("Send instance status update", instanceStatus);
585+
async sendInstanceInfo(instance: Instance): Promise<void> {
586+
this.logger.trace("Send instance status update", instance.status);
587587

588588
await this.communicationStream?.whenWrote(
589-
[CPMMessageCode.INSTANCE, { instance, status: instanceStatus }]
589+
[CPMMessageCode.INSTANCE, { instance }]
590590
);
591-
592-
this.logger.trace("Instance status update sent", instanceStatus);
593591
}
594592

595593
/**

packages/host/src/lib/csi-controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
MessageUtilities
77
} from "@scramjet/model";
88
import { development } from "@scramjet/sth-config";
9-
import { CommunicationChannel as CC, RunnerMessageCode } from "@scramjet/symbols";
9+
import { CommunicationChannel as CC, InstanceStatus, RunnerMessageCode } from "@scramjet/symbols";
1010
import {
1111
APIRoute,
1212
AppConfig,
@@ -19,7 +19,6 @@ import {
1919
ILifeCycleAdapterRun,
2020
InstanceLimits,
2121
InstanceStats,
22-
InstanceStatus,
2322
IObjectLogger,
2423
MessageDataType,
2524
MonitoringMessageData,

packages/host/src/lib/csi-dispatcher.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getInstanceAdapter } from "@scramjet/adapters";
22
import { IDProvider } from "@scramjet/model";
33
import { ObjLogger } from "@scramjet/obj-logger";
4-
import { RunnerMessageCode } from "@scramjet/symbols";
5-
import { ContentType, EventMessageData, HostProxy, ICommunicationHandler, IObjectLogger, Instance, InstanceConfig, InstanceStatus, MessageDataType, PangMessageData, PingMessageData, STHConfiguration, STHRestAPI, SequenceInfo, SequenceInfoInstance } from "@scramjet/types";
4+
import { InstanceStatus, RunnerMessageCode } from "@scramjet/symbols";
5+
import { ContentType, EventMessageData, HostProxy, ICommunicationHandler, IObjectLogger, Instance, InstanceConfig, MessageDataType, PangMessageData, PingMessageData, STHConfiguration, STHRestAPI, SequenceInfo, SequenceInfoInstance } from "@scramjet/types";
66
import { TypedEmitter } from "@scramjet/utility";
77
import { CSIController, CSIControllerInfo } from "./csi-controller";
88
import { InstanceStore } from "./instance-store";
@@ -249,13 +249,16 @@ export class CSIDispatcher extends TypedEmitter<Events> {
249249

250250
this.logger.debug("Dispatched. Waiting for connection...", id);
251251

252+
let established = false;
253+
252254
return await Promise.race([
253255
new Promise<void>((resolve, _reject) => {
254256
const resolveFunction = (instance: Instance) => {
255257
if (instance.id === id) {
256258
this.logger.debug("Established", id);
257259

258260
this.off("established", resolveFunction);
261+
established = true;
259262
resolve();
260263
}
261264
};
@@ -271,13 +274,18 @@ export class CSIDispatcher extends TypedEmitter<Events> {
271274
sequence
272275
})),
273276
// handle fast fail - before connection is established.
274-
Promise.resolve()
275-
.then(() => instanceAdapter.waitUntilExit(undefined, id, sequence))
276-
.then(async (exitCode) => {
277-
this.logger.info("Exited before established", id, exitCode);
277+
Promise.resolve().then(
278+
() => instanceAdapter.waitUntilExit(undefined, id, sequence)
279+
.then(async (exitCode: number) => {
280+
if (!established) {
281+
this.logger.info("Exited before established", id, exitCode);
282+
283+
return mapRunnerExitCode(exitCode, sequence);
284+
}
278285

279-
return mapRunnerExitCode(exitCode, sequence);
280-
})
286+
return undefined;
287+
})
288+
)
281289
]);
282290
}
283291
}

packages/host/src/lib/host.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AddressInfo } from "net";
66
import { Duplex } from "stream";
77

88
import { CommunicationHandler, HostError, IDProvider } from "@scramjet/model";
9-
import { HostHeaders, InstanceMessageCode, RunnerMessageCode, SequenceMessageCode } from "@scramjet/symbols";
9+
import { HostHeaders, InstanceMessageCode, InstanceStatus, RunnerMessageCode, SequenceMessageCode } from "@scramjet/symbols";
1010
import {
1111
APIExpose,
1212
CPMConnectorOptions,
@@ -357,7 +357,7 @@ export class Host implements IComponent {
357357
await this.cpmConnector?.sendInstanceInfo({
358358
id: instance.id,
359359
sequence: instance.sequence
360-
}, InstanceMessageCode.INSTANCE_CONNECTED);
360+
});
361361

362362
this.pushTelemetry("Instance connected", {
363363
id: instance.id,
@@ -368,21 +368,22 @@ export class Host implements IComponent {
368368
/**
369369
* Pass information about ended instance to monitoring and platform services.
370370
*
371-
* @param {DispatcherInstanceEndEventData} eventData Event details.
371+
* @param {DispatcherInstanceEndEventData} instance Event details.
372372
*/
373-
async handleDispatcherEndEvent(eventData: DispatcherInstanceEndEventData) {
374-
this.auditor.auditInstance(eventData.id, InstanceMessageCode.INSTANCE_ENDED);
373+
async handleDispatcherEndEvent(instance: DispatcherInstanceEndEventData) {
374+
this.auditor.auditInstance(instance.id, InstanceMessageCode.INSTANCE_ENDED);
375375

376376
await this.cpmConnector?.sendInstanceInfo({
377-
id: eventData.id,
378-
sequence: eventData.sequence
379-
}, InstanceMessageCode.INSTANCE_ENDED);
377+
id: instance.id,
378+
status: InstanceStatus.GONE,
379+
sequence: instance.sequence
380+
});
380381

381382
this.pushTelemetry("Instance ended", {
382-
executionTime: eventData.info.executionTime.toString(),
383-
id: eventData.id,
384-
code: eventData.code.toString(),
385-
seqId: eventData.sequence.id
383+
executionTime: instance.info.executionTime.toString(),
384+
id: instance.id,
385+
code: instance.code.toString(),
386+
seqId: instance.sequence.id
386387
});
387388
}
388389

@@ -1089,7 +1090,7 @@ export class Host implements IComponent {
10891090
try {
10901091
const runner = await this.csiDispatcher.startRunner(sequence, payload);
10911092

1092-
if ("id" in runner) {
1093+
if (runner && "id" in runner) {
10931094
this.logger.debug("Instance limits", runner.limits);
10941095
this.auditor.auditInstanceStart(runner.id, req as AuditedRequest, runner.limits);
10951096
this.pushTelemetry("Instance started", { id: runner.id, language: runner.sequence.config.language, seqId: runner.sequence.id });
@@ -1099,9 +1100,11 @@ export class Host implements IComponent {
10991100
message: `Sequence ${runner.id} starting`,
11001101
id: runner.id
11011102
};
1102-
} else {
1103+
} else if (runner) {
11031104
throw runner;
11041105
}
1106+
1107+
throw Error("Unexpected startup error");
11051108
} catch (error: any) {
11061109
this.pushTelemetry("Instance start failed", { error: error.message }, "error");
11071110
this.logger.error(error.message);

packages/host/src/lib/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { RunnerExitCode } from "@scramjet/symbols";
2-
import { InstanceStatus, SequenceInfo } from "@scramjet/types";
1+
import { InstanceStatus, RunnerExitCode } from "@scramjet/symbols";
2+
import { SequenceInfo } from "@scramjet/types";
33

44
// eslint-disable-next-line complexity
55
export const mapRunnerExitCode = async (exitcode: number, sequence: SequenceInfo): Promise<

packages/runner/src/runner.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RunnerError } from "@scramjet/model";
22
import { ObjLogger } from "@scramjet/obj-logger";
3-
import { RunnerExitCode, RunnerMessageCode } from "@scramjet/symbols";
3+
import { InstanceStatus, RunnerExitCode, RunnerMessageCode } from "@scramjet/symbols";
44
import {
55
AppConfig,
66
ApplicationFunction,
@@ -14,7 +14,6 @@ import {
1414
IComponent,
1515
IHostClient,
1616
IObjectLogger,
17-
InstanceStatus,
1817
MaybePromise,
1918
MonitoringRateMessageData,
2019
PangMessageData,

packages/symbols/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export { SequenceMessageCode } from "./sequence-status-code";
88
export { OpRecordCode } from "./op-record-code";
99
export { APIErrorCode } from "./api-error-codes";
1010
export { DisconnectHubErrors } from "./disconnect-error-codes";
11+
export { InstanceStatus } from "./instance-status";
1112

1213
export * from "./headers";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const enum InstanceStatus {
2+
INITIALIZING = "initializing",
3+
STARTING = "starting",
4+
RUNNING = "running",
5+
STOPPING = "stopping",
6+
KILLING = "killing",
7+
COMPLETED ="completed",
8+
ERRORED = "errored",
9+
GONE = "gone"
10+
}

packages/types/src/instance-store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { InstanceStatus } from "@scramjet/symbols";
12
import { AppConfig } from "./app-config";
2-
import { InstanceArgs, InstanceId, InstanceStatus } from "./instance";
3+
import { InstanceArgs, InstanceId } from "./instance";
34
import { SequenceInfoInstance } from "./sequence-adapter";
45

56
export type Instance = {

packages/types/src/instance.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ export type InstanceId = string;
22

33
export type InstanceArgs = any[];
44

5-
export const enum InstanceStatus {
6-
INITIALIZING = "initializing",
7-
STARTING = "starting",
8-
RUNNING = "running",
9-
STOPPING = "stopping",
10-
KILLING = "killing",
11-
COMPLETED ="completed",
12-
ERRORED = "errored",
13-
}
14-
155
export type InstanceConnectionInfo = {
166

177
}

0 commit comments

Comments
 (0)