Skip to content

Commit

Permalink
refactor: use mainsail for process names (#431)
Browse files Browse the repository at this point in the history
* Remove token from process

* Add contracts

* Use contracts

* Use app name

* Remove token flag

* Remove token flag from api commands

* Fix core tests

* Fix api commands

* Fix tests
  • Loading branch information
sebastijankuzner authored Feb 15, 2024
1 parent e596fd2 commit b21b032
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 95 deletions.
5 changes: 2 additions & 3 deletions packages/api/source/commands/api-log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Commands, Identifiers } from "@mainsail/cli";
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import Joi from "joi";

Expand All @@ -10,14 +10,13 @@ export class Command extends Commands.Command {

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string())
.setFlag("error", "Only display the error output.", Joi.boolean())
.setFlag("lines", "The number of lines to output.", Joi.number().default(15));
}

public async execute(): Promise<void> {
await this.app
.get<any>(Identifiers.ProcessFactory)(this.getFlag("token"), "api")
.get<Contracts.ProcessFactory>(Identifiers.ProcessFactory)("mainsail-api")
.log(this.getFlag("error"), this.getFlag("lines"));
}
}
4 changes: 2 additions & 2 deletions packages/api/source/commands/api-restart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ describe<{
const missing = stub(processManager, "missing").returnValue(true);
const isStopped = stub(processManager, "isStopped").returnValue(false);

await assert.rejects(() => cli.execute(Command), 'The "ark-api" process does not exist.');
await assert.rejects(() => cli.execute(Command), 'The "mainsail-api" process does not exist.');
});

it("should throw if the process is stopped", async ({ processManager, cli }) => {
const missing = stub(processManager, "missing").returnValue(false);
const isStopped = stub(processManager, "isStopped").returnValue(true);

await assert.rejects(() => cli.execute(Command), 'The "ark-api" process is not running.');
await assert.rejects(() => cli.execute(Command), 'The "mainsail-api" process is not running.');
});

it("should restart the process", async ({ processManager, cli }) => {
Expand Down
9 changes: 2 additions & 7 deletions packages/api/source/commands/api-restart.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Commands, Identifiers } from "@mainsail/cli";
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import Joi from "joi";

@injectable()
export class Command extends Commands.Command {
public signature = "api:restart";

public description = "Restart the API process.";

public configure(): void {
this.definition.setFlag("token", "The name of the token.", Joi.string().required());
}

public async execute(): Promise<void> {
this.app.get<any>(Identifiers.ProcessFactory)(this.getFlag("token"), "api").restart();
this.app.get<Contracts.ProcessFactory>(Identifiers.ProcessFactory)("mainsail-api").restart();
}
}
4 changes: 2 additions & 2 deletions packages/api/source/commands/api-start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ describe<{
CORE_ENV: "production",
NODE_ENV: "production",
},
name: "ark-api",
name: "mainsail-api",
node_args: undefined,
script: resolve(__dirname, "../../../../packages/api/bin/run"),
},
{ "kill-timeout": 30_000, "max-restarts": 5, name: "ark-api" },
{ "kill-timeout": 30_000, "max-restarts": 5, name: "mainsail-api" },
);
});
});
2 changes: 1 addition & 1 deletion packages/api/source/commands/api-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Command extends Commands.Command {
await this.actions.daemonizeProcess(
{
args: `core:run ${Utils.Flags.castFlagsToString(flags, ["daemon"])}`,
name: `${flags.token}-api`,
name: `mainsail-api`,
script: resolve(__dirname, "../../bin/run"),
},
flags,
Expand Down
6 changes: 3 additions & 3 deletions packages/api/source/commands/api-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ describe<{
});

it("should throw if the process does not exist", async ({ cli }) => {
await assert.rejects(() => cli.execute(Command), 'The "ark-api" process does not exist.');
await assert.rejects(() => cli.execute(Command), 'The "mainsail-api" process does not exist.');
});

it("should render a table with the process information", async ({ processManager, cli }) => {
stub(processManager, "missing").returnValue(false);
stub(processManager, "describe").returnValue({
monit: { cpu: 2, memory: 2048 },
name: "ark-api",
name: "mainsail-api",
pid: 1,
pm2_env: {
pm_uptime: 1_387_045_673_686,
Expand All @@ -40,7 +40,7 @@ describe<{
assert.true(
[
"1",
"ark-api",
"mainsail-api",
"1.0.0",
"online",
// "5y 267d 19h 31m 28.1s",
Expand Down
9 changes: 2 additions & 7 deletions packages/api/source/commands/api-status.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Commands, Identifiers } from "@mainsail/cli";
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import Joi from "joi";

@injectable()
export class Command extends Commands.Command {
public signature = "api:status";

public description = "Display the status of the API process.";

public configure(): void {
this.definition.setFlag("token", "The name of the token.", Joi.string());
}

public async execute(): Promise<void> {
this.app.get<any>(Identifiers.ProcessFactory)(this.getFlag("token"), "api").status();
this.app.get<Contracts.ProcessFactory>(Identifiers.ProcessFactory)("mainsail-api").status();
}
}
6 changes: 3 additions & 3 deletions packages/api/source/commands/api-stop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ describe<{
const isUnknown = stub(processManager, "isUnknown").returnValue(false);
const isStopped = stub(processManager, "isStopped").returnValue(false);

await assert.rejects(() => cli.execute(Command), 'The "ark-api" process does not exist.');
await assert.rejects(() => cli.execute(Command), 'The "mainsail-api" process does not exist.');
});

it("should throw if the process entered an unknown state", async ({ processManager, cli }) => {
const missing = stub(processManager, "missing").returnValue(false);
const isUnknown = stub(processManager, "isUnknown").returnValue(true);
const isStopped = stub(processManager, "isStopped").returnValue(false);

await assert.rejects(() => cli.execute(Command), 'The "ark-api" process has entered an unknown state.');
await assert.rejects(() => cli.execute(Command), 'The "mainsail-api" process has entered an unknown state.');
});

it("should throw if the process is stopped", async ({ processManager, cli }) => {
const missing = stub(processManager, "missing").returnValue(false);
const isUnknown = stub(processManager, "isUnknown").returnValue(false);
const isStopped = stub(processManager, "isStopped").returnValue(true);

await assert.rejects(() => cli.execute(Command), 'The "ark-api" process is not running.');
await assert.rejects(() => cli.execute(Command), 'The "mainsail-api" process is not running.');
});

it("should stop the process if the [--daemon] flag is not present", async ({ processManager, cli }) => {
Expand Down
8 changes: 3 additions & 5 deletions packages/api/source/commands/api-stop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Commands, Identifiers } from "@mainsail/cli";
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import Joi from "joi";

Expand All @@ -9,12 +9,10 @@ export class Command extends Commands.Command {
public description = "Stop the API process.";

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string())
.setFlag("daemon", "Stop the Core process or daemon.", Joi.boolean());
this.definition.setFlag("daemon", "Stop the Core process or daemon.", Joi.boolean());
}

public async execute(): Promise<void> {
this.app.get<any>(Identifiers.ProcessFactory)(this.getFlag("token"), "api").stop(this.getFlag("daemon"));
this.app.get<Contracts.ProcessFactory>(Identifiers.ProcessFactory)("mainsail-api").stop(this.getFlag("daemon"));
}
}
15 changes: 6 additions & 9 deletions packages/cli/source/application-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,12 @@ export class ApplicationFactory {

app.bind(Identifiers.ComponentFactory).to(ComponentFactory).inSingletonScope();

app.bind(Identifiers.ProcessFactory).toFactory(
(context: interfaces.Context) =>
(token: string, type: string): Process => {
const process: Process = context.container.resolve(Process);
process.initialize(token, type);

return process;
},
);
app.bind(Identifiers.ProcessFactory).toFactory((context: interfaces.Context) => (type: string): Process => {
const process: Process = context.container.resolve(Process);
process.initialize(type);

return process;
});

// Services
app.bind(Identifiers.Output).to(Output).inSingletonScope();
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/source/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ export type ProcessDescription = Record<string, any>;

export type ProcessOptions = Record<"name" | "script" | "args", string>;

export interface Process {
stop(daemon: boolean): void;
restart(): void;
status(): void;
log(showErrors: boolean, lines: number): void;
}

export type ProcessFactory = (name: string) => Process;

// APPLICATION
export interface Application {
bind<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): interfaces.BindingToSyntax<T>;
Expand Down
28 changes: 14 additions & 14 deletions packages/cli/source/utils/process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe<{
return process;
});

context.process = context.cli.app.get(Identifiers.ProcessFactory)("ark", "core");
context.process = context.cli.app.get(Identifiers.ProcessFactory)("mainsail");
});

afterAll(() => setGracefulCleanup());
Expand All @@ -34,23 +34,23 @@ describe<{
stub(processManager, "isUnknown").returnValue(false);
stub(processManager, "isStopped").returnValue(false);

assert.throws(() => process.stop(false), 'The "ark-core" process does not exist.');
assert.throws(() => process.stop(false), 'The "mainsail" process does not exist.');
});

it("#stop - should throw if the process entered an unknown state", ({ process, processManager }) => {
stub(processManager, "missing").returnValue(false);
stub(processManager, "isUnknown").returnValue(true);
stub(processManager, "isStopped").returnValue(false);

assert.throws(() => process.stop(false), 'The "ark-core" process has entered an unknown state.');
assert.throws(() => process.stop(false), 'The "mainsail" process has entered an unknown state.');
});

it("#stop - should throw if the process is stopped", ({ process, processManager }) => {
stub(processManager, "missing").returnValue(false);
stub(processManager, "isUnknown").returnValue(false);
stub(processManager, "isStopped").returnValue(true);

assert.throws(() => process.stop(false), 'The "ark-core" process is not running.');
assert.throws(() => process.stop(false), 'The "mainsail" process is not running.');
});

it("#stop - should delete the process if the [--daemon] flag is not present", ({ process, processManager }) => {
Expand Down Expand Up @@ -79,14 +79,14 @@ describe<{
stub(processManager, "missing").returnValue(true);
stub(processManager, "isStopped").returnValue(false);

assert.throws(() => process.restart(), 'The "ark-core" process does not exist.');
assert.throws(() => process.restart(), 'The "mainsail" process does not exist.');
});

it("#restart - should throw if the process is stopped", ({ process, processManager }) => {
stub(processManager, "missing").returnValue(false);
stub(processManager, "isStopped").returnValue(true);

assert.throws(() => process.restart(), 'The "ark-core" process is not running.');
assert.throws(() => process.restart(), 'The "mainsail" process is not running.');
});

it("#restart - should restart the process", ({ process, processManager }) => {
Expand All @@ -100,14 +100,14 @@ describe<{
});

it("#status - should throw if the process does not exist", async ({ process }) => {
assert.throws(() => process.status(), 'The "ark-core" process does not exist.');
assert.throws(() => process.status(), 'The "mainsail" process does not exist.');
});

it("#status - should render a table with the process information", async ({ process, processManager }) => {
stub(processManager, "missing").returnValue(false);
stub(processManager, "describe").returnValue({
monit: { cpu: 2, memory: 2048 },
name: "ark-core",
name: "mainsail",
pid: 1,
pm2_env: {
pm_uptime: 1_387_045_673_686,
Expand All @@ -128,7 +128,7 @@ describe<{
assert.true(
[
"1",
"ark-core",
"mainsail",
"1.0.0",
"online",
// "5y 267d 19h 31m 28.1s",
Expand All @@ -139,14 +139,14 @@ describe<{
});

it("#log - should throw if the process does not exist", async ({ process }) => {
await assert.rejects(() => process.log(false, 1), 'The "ark-core" process does not exist.');
await assert.rejects(() => process.log(false, 1), 'The "mainsail" process does not exist.');
});

it("#log - should log to pm_out_log_path", async ({ cli, process, processManager }) => {
stub(cli.app.get(Identifiers.AbortMissingProcess), "execute");
stub(processManager, "describe").returnValue({
monit: { cpu: 2, memory: 2048 },
name: "ark-core",
name: "mainsail",
pid: 1,
pm2_env: {
pm_err_log_path: fileSync().name,
Expand All @@ -162,14 +162,14 @@ describe<{
await process.log(false, 15);

spyWatch.calledOnce();
spyLog.calledWith("Tailing last 15 lines for [ark-core] process (change the value with --lines option)");
spyLog.calledWith("Tailing last 15 lines for [mainsail] process (change the value with --lines option)");
});

it("#log - should log to pm_err_log_path", async ({ cli, process, processManager }) => {
stub(cli.app.get(Identifiers.AbortMissingProcess), "execute");
stub(processManager, "describe").returnValue({
monit: { cpu: 2, memory: 2048 },
name: "ark-core",
name: "mainsail",
pid: 1,
pm2_env: {
pm_err_log_path: fileSync().name,
Expand All @@ -185,6 +185,6 @@ describe<{
await process.log(true, 15);

spyWatch.calledOnce();
spyLog.calledWith("Tailing last 15 lines for [ark-core] process (change the value with --lines option)");
spyLog.calledWith("Tailing last 15 lines for [mainsail] process (change the value with --lines option)");
});
});
8 changes: 4 additions & 4 deletions packages/cli/source/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import readLastLines from "read-last-lines";
import type { AbortMissingProcess, AbortStoppedProcess, AbortUnknownProcess } from "../actions";
import { Application } from "../application";
import { Clear, Spinner, Table } from "../components";
import { ProcessDescription } from "../contracts";
import { Process as IProcess, ProcessDescription } from "../contracts";
import { Identifiers } from "../ioc";
import type { ProcessManager } from "../services";

@injectable()
export class Process {
export class Process implements IProcess {
@inject(Identifiers.Application.Instance)
private readonly app!: Application;

Expand All @@ -22,8 +22,8 @@ export class Process {

#processName!: string;

public initialize(token: string, suffix: string): void {
this.#processName = `${token}-${suffix}`;
public initialize(processName: string): void {
this.#processName = processName;
}

public stop(daemon: boolean): void {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/source/commands/core-log.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Commands, Identifiers } from "@mainsail/cli";
import { Commands, Contracts, Identifiers } from "@mainsail/cli";
import { injectable } from "@mainsail/container";
import Joi from "joi";

Expand All @@ -10,14 +10,13 @@ export class Command extends Commands.Command {

public configure(): void {
this.definition
.setFlag("token", "The name of the token.", Joi.string().required())
.setFlag("error", "Only display the error output.", Joi.boolean())
.setFlag("lines", "The number of lines to output.", Joi.number().default(15));
}

public async execute(): Promise<void> {
await this.app
.get<any>(Identifiers.ProcessFactory)(this.getFlag("token"), "core")
.get<Contracts.ProcessFactory>(Identifiers.ProcessFactory)("mainsail")
.log(this.getFlag("error"), this.getFlag("lines"));
}
}
Loading

0 comments on commit b21b032

Please sign in to comment.