@runium/core 0.0.3 → 0.0.4

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.
Files changed (3) hide show
  1. package/index.d.ts +21 -3
  2. package/index.js +16 -16
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { ChildProcessWithoutNullStreams } from 'node:child_process';
1
2
  import { EventEmitter } from 'node:events';
3
+ import { WriteStream } from 'node:fs';
2
4
 
3
5
  export declare function applyMacros(text: string, macros: MacrosCollection): string;
4
6
 
@@ -62,7 +64,7 @@ export declare class Project extends EventEmitter {
62
64
  setConfig(config: ProjectConfig): void;
63
65
  getState(): ProjectState;
64
66
  start(): Promise<void>;
65
- stop(): Promise<void>;
67
+ stop(reason?: string): Promise<void>;
66
68
  extendValidationSchema(extensions: ProjectSchemaExtension): void;
67
69
  registerAction(type: string, processor: RuniumActionProcessor): void;
68
70
  registerTask(type: string, processor: RuniumTaskConstructor<unknown, any>): void;
@@ -180,6 +182,7 @@ export declare interface ProjectSchemaExtension {
180
182
  export declare interface ProjectState {
181
183
  status: ProjectStatus;
182
184
  timestamp: number;
185
+ reason?: string;
183
186
  }
184
187
 
185
188
  export declare enum ProjectStatus {
@@ -286,7 +289,7 @@ export declare abstract class RuniumTask<Options = unknown, State = RuniumTaskSt
286
289
  abstract getOptions(): Options;
287
290
  abstract getState(): State;
288
291
  abstract start(): Promise<void>;
289
- abstract stop(): Promise<void>;
292
+ abstract stop(reason?: string): Promise<void>;
290
293
  abstract restart(): Promise<void>;
291
294
  }
292
295
 
@@ -298,6 +301,7 @@ export declare interface RuniumTaskState {
298
301
  iteration: number;
299
302
  exitCode?: number;
300
303
  error?: Error;
304
+ reason?: string;
301
305
  }
302
306
 
303
307
  export declare abstract class RuniumTrigger<Options extends RuniumTriggerOptions> {
@@ -330,12 +334,26 @@ export declare const SILENT_EXIT_CODE = -1;
330
334
 
331
335
  export declare class Task extends RuniumTask<TaskOptions, TaskState> {
332
336
  protected readonly options: TaskOptions;
337
+ protected state: TaskState;
338
+ protected process: ChildProcessWithoutNullStreams | null;
339
+ protected stdoutStream: WriteStream | null;
340
+ protected stderrStream: WriteStream | null;
341
+ protected ttlTimer: NodeJS.Timeout | null;
333
342
  constructor(options: TaskOptions);
334
343
  getState(): TaskState;
335
344
  getOptions(): TaskOptions;
336
345
  start(): Promise<void>;
337
- stop(): Promise<void>;
346
+ stop(reason?: string): Promise<void>;
338
347
  restart(): Promise<void>;
348
+ protected updateState(state: Partial<TaskState>): void;
349
+ protected initLogStreams(): Promise<void>;
350
+ protected setTTLTimer(): void;
351
+ protected addProcessListeners(): void;
352
+ protected onStdOutData(data: Buffer): void;
353
+ protected onStdErrData(data: Buffer): void;
354
+ protected onExit(code: number | null): void;
355
+ protected onError(error: Error): void;
356
+ protected cleanup(): void;
339
357
  }
340
358
 
341
359
  export declare enum TaskEvent {
package/index.js CHANGED
@@ -148,16 +148,16 @@ class et extends C {
148
148
  }
149
149
  }
150
150
  }
151
- async stop() {
152
- if (this.process && this.state.status !== "stopping" && this.state.status === "started") return this.updateState({ status: "stopping" }), new Promise((t) => {
153
- const s = this.options.stopSignal || "SIGTERM";
154
- this.process.kill(s), this.process.on("exit", () => {
155
- t();
151
+ async stop(t = "") {
152
+ if (this.process && this.state.status !== "stopping" && this.state.status === "started") return this.updateState({ status: "stopping", reason: t }), new Promise((s) => {
153
+ const r = this.options.stopSignal || "SIGTERM";
154
+ this.process.kill(r), this.process.on("exit", () => {
155
+ s();
156
156
  });
157
157
  });
158
158
  }
159
159
  async restart() {
160
- await this.stop(), this.start();
160
+ await this.stop("restart"), this.start();
161
161
  }
162
162
  updateState(t) {
163
163
  const s = { ...this.state, ...t, timestamp: Date.now() };
@@ -177,7 +177,7 @@ class et extends C {
177
177
  setTTLTimer() {
178
178
  const { ttl: t } = this.options;
179
179
  t && this.process && (this.ttlTimer = setTimeout(() => {
180
- this.stop();
180
+ this.stop("ttl");
181
181
  }, t));
182
182
  }
183
183
  addProcessListeners() {
@@ -215,7 +215,7 @@ function ot(e, t) {
215
215
  return { type: "object", properties: { ...structuredClone(k), type: { type: "string", const: e }, ...s }, required: ["id", "type", "action", ...r], additionalProperties: !1 };
216
216
  }
217
217
  function nt() {
218
- const e = { $schema: "https://json-schema.org/draft/2020-12/schema", $id: "https://example.com/schemas/project.json", title: "Project", type: "object", properties: { id: { type: "string" }, name: { type: "string" }, tasks: { type: "array", items: { oneOf: [{ $ref: "#/$defs/Runium_TaskConfig" }] }, minItems: 1, uniqueItemProperties: ["id"] }, triggers: { type: "array", items: { $ref: "#/$defs/Runium_Trigger" }, uniqueItemProperties: ["id"] } }, required: ["id", "tasks"], additionalProperties: !1, $defs: { Runium_EnvValue: { type: ["string", "number", "boolean"] }, Runium_Env: { type: "object", additionalProperties: { $ref: "#/$defs/Runium_EnvValue" } }, Runium_TaskStartMode: { type: "string", enum: Object.values(R) }, Runium_TaskHandler: { type: "object", properties: { action: { $ref: "#/$defs/Runium_Action" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["action", "condition"], additionalProperties: !1 }, Runium_TaskStateCondition: { oneOf: [{ type: "string" }, { type: "boolean" }, { $ref: "#/$defs/Runium_TaskState" }] }, Runium_TaskState: { type: "object", properties: { status: { $ref: "#/$defs/Runium_TaskStatus" }, iteration: { type: "number", minimum: 0 }, exitCode: { type: "number", minimum: 0 } }, additionalProperties: !1 }, Runium_TaskStatus: { type: "string", enum: Object.values(h) }, Runium_TaskDependency: { type: "object", properties: { taskId: { type: "string" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["taskId", "condition"], additionalProperties: !1 }, Runium_Trigger: { oneOf: [{ $ref: "#/$defs/Runium_TriggerEvent" }, { $ref: "#/$defs/Runium_TriggerInterval" }, { $ref: "#/$defs/Runium_TriggerTimeout" }] }, Runium_TriggerEvent: { type: "object", properties: { ...structuredClone(k), type: { const: l.EVENT }, event: { type: "string" } }, required: ["id", "type", "event", "action"], additionalProperties: !1 }, Runium_TriggerInterval: { type: "object", properties: { ...structuredClone(k), type: { const: l.INTERVAL }, interval: { type: "number", minimum: 0 } }, required: ["id", "type", "interval", "action"], additionalProperties: !1 }, Runium_TriggerTimeout: { type: "object", properties: { ...structuredClone(k), type: { const: l.TIMEOUT }, timeout: { type: "number", minimum: 0 } }, required: ["id", "type", "timeout", "action"], additionalProperties: !1 }, Runium_Action: { oneOf: [{ $ref: "#/$defs/Runium_ActionEmitEvent" }, { $ref: "#/$defs/Runium_ActionProcessTask" }, { $ref: "#/$defs/Runium_ActionStopProject" }, { $ref: "#/$defs/Runium_ActionToggleTrigger" }] }, Runium_ActionEmitEvent: { type: "object", properties: { type: { type: "string", const: d.EMIT_EVENT }, event: { type: "string" } }, required: ["type", "event"], additionalProperties: !1 }, Runium_ActionProcessTask: { type: "object", properties: { type: { type: "string", enum: [d.START_TASK, d.RESTART_TASK, d.STOP_TASK] }, taskId: { type: "string" } }, required: ["type", "taskId"], additionalProperties: !1 }, Runium_ActionStopProject: { type: "object", properties: { type: { type: "string", const: d.STOP_PROJECT } }, required: ["type"], additionalProperties: !1 }, Runium_ActionToggleTrigger: { type: "object", properties: { type: { type: "string", enum: [d.ENABLE_TRIGGER, d.DISABLE_TRIGGER] }, triggerId: { type: "string" } }, required: ["type", "triggerId"], additionalProperties: !1 }, Runium_TaskConfig: { ...D(f.DEFAULT, { $ref: "#/$defs/Runium_TaskOptions" }) }, Runium_TaskOptions: { type: "object", properties: { command: { type: "string" }, arguments: { type: "array", items: { type: "string" } }, shell: { type: "boolean" }, cwd: { type: "string" }, env: { $ref: "#/$defs/Runium_Env" }, ttl: { type: "number" }, log: { $ref: "#/$defs/Runium_TaskLog" }, stopSignal: { type: "string" } }, required: ["command"], additionalProperties: !1 }, Runium_TaskRestartPolicy: { oneOf: [{ $ref: "#/$defs/Runium_TaskRestartPolicyAlways" }, { $ref: "#/$defs/Runium_TaskRestartPolicyOnFailure" }] }, Runium_TaskRestartPolicyAlways: { type: "object", required: ["policy"], properties: { policy: { const: T.ALWAYS }, delay: { type: "number" } }, additionalProperties: !1 }, Runium_TaskRestartPolicyOnFailure: { type: "object", required: ["policy"], properties: { policy: { const: T.ON_FAILURE }, delay: { type: "number" }, maxRetries: { type: "number" } }, additionalProperties: !1 }, Runium_TaskLog: { type: "object", properties: { stdout: { type: ["string", "null"] }, stderr: { type: ["string", "null"] } }, additionalProperties: !1 } } };
218
+ const e = { $schema: "https://json-schema.org/draft/2020-12/schema", $id: "https://example.com/schemas/project.json", title: "Project", type: "object", properties: { id: { type: "string" }, name: { type: "string" }, tasks: { type: "array", items: { oneOf: [{ $ref: "#/$defs/Runium_TaskConfig" }] }, minItems: 1, uniqueItemProperties: ["id"] }, triggers: { type: "array", items: { $ref: "#/$defs/Runium_Trigger" }, uniqueItemProperties: ["id"] } }, required: ["id", "tasks"], additionalProperties: !1, $defs: { Runium_EnvValue: { type: ["string", "number", "boolean"] }, Runium_Env: { type: "object", additionalProperties: { $ref: "#/$defs/Runium_EnvValue" } }, Runium_TaskStartMode: { type: "string", enum: Object.values(R) }, Runium_TaskHandler: { type: "object", properties: { action: { $ref: "#/$defs/Runium_Action" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["action", "condition"], additionalProperties: !1 }, Runium_TaskStateCondition: { oneOf: [{ type: "string" }, { type: "boolean" }, { $ref: "#/$defs/Runium_TaskState" }] }, Runium_TaskState: { type: "object", properties: { status: { $ref: "#/$defs/Runium_TaskStatus" }, iteration: { type: "number", minimum: 0 }, exitCode: { type: "number", minimum: 0 }, reason: { type: "string" } }, additionalProperties: !1 }, Runium_TaskStatus: { type: "string", enum: Object.values(h) }, Runium_TaskDependency: { type: "object", properties: { taskId: { type: "string" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["taskId", "condition"], additionalProperties: !1 }, Runium_Trigger: { oneOf: [{ $ref: "#/$defs/Runium_TriggerEvent" }, { $ref: "#/$defs/Runium_TriggerInterval" }, { $ref: "#/$defs/Runium_TriggerTimeout" }] }, Runium_TriggerEvent: { type: "object", properties: { ...structuredClone(k), type: { const: l.EVENT }, event: { type: "string" } }, required: ["id", "type", "event", "action"], additionalProperties: !1 }, Runium_TriggerInterval: { type: "object", properties: { ...structuredClone(k), type: { const: l.INTERVAL }, interval: { type: "number", minimum: 0 } }, required: ["id", "type", "interval", "action"], additionalProperties: !1 }, Runium_TriggerTimeout: { type: "object", properties: { ...structuredClone(k), type: { const: l.TIMEOUT }, timeout: { type: "number", minimum: 0 } }, required: ["id", "type", "timeout", "action"], additionalProperties: !1 }, Runium_Action: { oneOf: [{ $ref: "#/$defs/Runium_ActionEmitEvent" }, { $ref: "#/$defs/Runium_ActionProcessTask" }, { $ref: "#/$defs/Runium_ActionStopProject" }, { $ref: "#/$defs/Runium_ActionToggleTrigger" }] }, Runium_ActionEmitEvent: { type: "object", properties: { type: { type: "string", const: d.EMIT_EVENT }, event: { type: "string" } }, required: ["type", "event"], additionalProperties: !1 }, Runium_ActionProcessTask: { type: "object", properties: { type: { type: "string", enum: [d.START_TASK, d.RESTART_TASK, d.STOP_TASK] }, taskId: { type: "string" } }, required: ["type", "taskId"], additionalProperties: !1 }, Runium_ActionStopProject: { type: "object", properties: { type: { type: "string", const: d.STOP_PROJECT } }, required: ["type"], additionalProperties: !1 }, Runium_ActionToggleTrigger: { type: "object", properties: { type: { type: "string", enum: [d.ENABLE_TRIGGER, d.DISABLE_TRIGGER] }, triggerId: { type: "string" } }, required: ["type", "triggerId"], additionalProperties: !1 }, Runium_TaskConfig: { ...D(f.DEFAULT, { $ref: "#/$defs/Runium_TaskOptions" }) }, Runium_TaskOptions: { type: "object", properties: { command: { type: "string" }, arguments: { type: "array", items: { type: "string" } }, shell: { type: "boolean" }, cwd: { type: "string" }, env: { $ref: "#/$defs/Runium_Env" }, ttl: { type: "number" }, log: { $ref: "#/$defs/Runium_TaskLog" }, stopSignal: { type: "string" } }, required: ["command"], additionalProperties: !1 }, Runium_TaskRestartPolicy: { oneOf: [{ $ref: "#/$defs/Runium_TaskRestartPolicyAlways" }, { $ref: "#/$defs/Runium_TaskRestartPolicyOnFailure" }] }, Runium_TaskRestartPolicyAlways: { type: "object", required: ["policy"], properties: { policy: { const: T.ALWAYS }, delay: { type: "number" } }, additionalProperties: !1 }, Runium_TaskRestartPolicyOnFailure: { type: "object", required: ["policy"], properties: { policy: { const: T.ON_FAILURE }, delay: { type: "number" }, maxRetries: { type: "number" } }, additionalProperties: !1 }, Runium_TaskLog: { type: "object", properties: { stdout: { type: ["string", "null"] }, stderr: { type: ["string", "null"] } }, additionalProperties: !1 } } };
219
219
  return Object.freeze(e);
220
220
  }
221
221
  function at(e, t) {
@@ -359,15 +359,15 @@ class jt extends O {
359
359
  }
360
360
  this.updateState({ status: "started" });
361
361
  }
362
- async stop() {
362
+ async stop(t = "") {
363
363
  if (this.state.status !== "started" && this.state.status !== "starting") return;
364
- this.updateState({ status: "stopping" }), this.cleanupTriggers();
365
- const t = this.getTasksStartOrder().reverse(), s = [];
366
- for (const r of t) if (this.tasks.has(r)) {
367
- const { instance: i } = this.tasks.get(r), { status: o } = i.getState();
368
- o === h.STARTED && s.push(i.stop());
364
+ this.updateState({ status: "stopping", reason: t }), this.cleanupTriggers();
365
+ const s = this.getTasksStartOrder().reverse(), r = [];
366
+ for (const i of s) if (this.tasks.has(i)) {
367
+ const { instance: o } = this.tasks.get(i), { status: a } = o.getState();
368
+ a === h.STARTED && r.push(o.stop("project-stop"));
369
369
  }
370
- await Promise.allSettled(s), this.updateState({ status: "stopped" });
370
+ await Promise.allSettled(r), this.updateState({ status: "stopped" });
371
371
  }
372
372
  extendValidationSchema(t) {
373
373
  this.schema = at(this.schema, t);
@@ -445,7 +445,7 @@ class jt extends O {
445
445
  async stopTask(t) {
446
446
  if (this.tasks.has(t)) {
447
447
  const { instance: s } = this.tasks.get(t), { status: r } = s.getState();
448
- if (r === h.STARTED) return this.emit("stop-task", t), s.stop();
448
+ if (r === h.STARTED) return this.emit("stop-task", t), s.stop("action-stop");
449
449
  }
450
450
  }
451
451
  async restartTask(t) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runium/core",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Runium Core",
5
5
  "author": "TheBeastApp",
6
6
  "license": "MIT",