Skip to content

Commit

Permalink
chore: enable core-manager on all processes (#4205)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored Dec 2, 2020
1 parent 57eaabf commit 9eb1ce9
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ describe("LogsDatabaseService", () => {
it("should dispose", async () => {
database.boot();
database.dispose();
});

it("should not throw if add is called after dispose", async () => {
database.boot();
database.dispose();

expect(() => {
database.add("info", "log content");
}).toThrowError();
}).not.toThrowError();
});
});

Expand Down
22 changes: 21 additions & 1 deletion __tests__/unit/core-manager/service-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ beforeEach(() => {

app.bind(Container.Identifiers.LogService).toConstantValue(logger);
app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();
app.bind(Container.Identifiers.ConfigFlags).toConstantValue({ processType: "core" });
app.bind(Container.Identifiers.ConfigFlags).toConstantValue({ processType: "manager" });
app.bind(Container.Identifiers.FilesystemService).toConstantValue({});
app.bind(Container.Identifiers.EventDispatcherService).toConstantValue(mockEventDispatcher);
app.bind(Container.Identifiers.WalletAttributes).toConstantValue({});
Expand Down Expand Up @@ -84,6 +84,26 @@ describe("ServiceProvider", () => {
await expect(serviceProvider.dispose()).toResolve();
});

it("should not boot HTTP server and register actions when process is not manager", async () => {
app.rebind(Container.Identifiers.ConfigFlags).toConstantValue({ processType: "core" });

const usedDefaults = { ...defaults };

usedDefaults.server.http.enabled = true;

setPluginConfiguration(app, serviceProvider, usedDefaults);

await expect(serviceProvider.register()).toResolve();

await expect(serviceProvider.boot()).toResolve();

expect(app.isBound(Identifiers.HTTP)).toBeFalse();
expect(app.isBound(Identifiers.HTTPS)).toBeFalse();
expect(app.isBound(Identifiers.ActionReader)).toBeFalse();

await expect(serviceProvider.dispose()).toResolve();
});

it("should boot and dispose HTTPS server", async () => {
const usedDefaults = { ...defaults };

Expand Down
4 changes: 4 additions & 0 deletions packages/core-manager/src/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class Database {
this.database = new BetterSqlite3(filename);
}

public isOpen(): boolean {
return this.database.open;
}

public boot(flush: boolean = false): void {
this.exec(this.createDatabaseSQL());

Expand Down
4 changes: 4 additions & 0 deletions packages/core-manager/src/database/logs-database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class LogsDatabaseService {
}

public add(level: string, content: string): void {
if (!this.database.isOpen()) {
return;
}

this.database.add("logs", {
process: this.configFlags.processType,
level,
Expand Down
38 changes: 21 additions & 17 deletions packages/core-manager/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ServiceProvider extends Providers.ServiceProvider {
this.app.get<EventsDatabaseService>(Identifiers.WatcherDatabaseService).boot();

this.app.bind(Identifiers.LogsDatabaseService).to(LogsDatabaseService).inSingletonScope();
this.app.get<EventsDatabaseService>(Identifiers.LogsDatabaseService).boot();
this.app.get<LogsDatabaseService>(Identifiers.LogsDatabaseService).boot();

if (this.config().getRequired<{ enabled: boolean }>("watcher").enabled) {
this.app.bind(Identifiers.EventsListener).to(Listener).inSingletonScope();
Expand All @@ -43,30 +43,34 @@ export class ServiceProvider extends Providers.ServiceProvider {
);
}

this.app.bind(Identifiers.ActionReader).to(ActionReader).inSingletonScope();
this.app.bind(Identifiers.PluginFactory).to(PluginFactory).inSingletonScope();
this.app.bind(Identifiers.BasicCredentialsValidator).to(Argon2id).inSingletonScope();
this.app.bind(Identifiers.TokenValidator).to(SimpleTokenValidator).inSingletonScope();
this.app.bind(Identifiers.SnapshotsManager).to(SnapshotsManager).inSingletonScope();
this.app.bind(Identifiers.CliManager).to(CliManager).inSingletonScope();
if (this.app.get<any>(Container.Identifiers.ConfigFlags).processType === "manager") {
this.app.bind(Identifiers.ActionReader).to(ActionReader).inSingletonScope();
this.app.bind(Identifiers.PluginFactory).to(PluginFactory).inSingletonScope();
this.app.bind(Identifiers.BasicCredentialsValidator).to(Argon2id).inSingletonScope();
this.app.bind(Identifiers.TokenValidator).to(SimpleTokenValidator).inSingletonScope();
this.app.bind(Identifiers.SnapshotsManager).to(SnapshotsManager).inSingletonScope();
this.app.bind(Identifiers.CliManager).to(CliManager).inSingletonScope();

const pkg: Types.PackageJson = require("../package.json");
this.app.bind(Identifiers.CLI).toConstantValue(ApplicationFactory.make(new Container.Container(), pkg));
const pkg: Types.PackageJson = require("../package.json");
this.app.bind(Identifiers.CLI).toConstantValue(ApplicationFactory.make(new Container.Container(), pkg));
}
}

/**
* @returns {Promise<void>}
* @memberof ServiceProvider
*/
public async boot(): Promise<void> {
if (this.config().get("server.http.enabled")) {
await this.buildServer("http", Identifiers.HTTP);
await this.app.get<Server>(Identifiers.HTTP).boot();
}
if (this.app.get<any>(Container.Identifiers.ConfigFlags).processType === "manager") {
if (this.config().get("server.http.enabled")) {
await this.buildServer("http", Identifiers.HTTP);
await this.app.get<Server>(Identifiers.HTTP).boot();
}

if (this.config().get("server.https.enabled")) {
await this.buildServer("https", Identifiers.HTTPS);
await this.app.get<Server>(Identifiers.HTTPS).boot();
if (this.config().get("server.https.enabled")) {
await this.buildServer("https", Identifiers.HTTPS);
await this.app.get<Server>(Identifiers.HTTPS).boot();
}
}

if (this.config().getRequired<{ enabled: boolean }>("watcher").enabled) {
Expand All @@ -84,7 +88,7 @@ export class ServiceProvider extends Providers.ServiceProvider {
}

this.app.get<EventsDatabaseService>(Identifiers.WatcherDatabaseService).dispose();
this.app.get<EventsDatabaseService>(Identifiers.LogsDatabaseService).dispose();
this.app.get<LogsDatabaseService>(Identifiers.LogsDatabaseService).dispose();
}

public async required(): Promise<boolean> {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/bin/config/devnet/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-state"
},
Expand Down Expand Up @@ -47,6 +50,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-state"
},
Expand Down Expand Up @@ -84,6 +90,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-forger"
}
Expand All @@ -94,6 +103,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-snapshots"
}
Expand Down
12 changes: 12 additions & 0 deletions packages/core/bin/config/mainnet/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-state"
},
Expand Down Expand Up @@ -44,6 +47,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-state"
},
Expand Down Expand Up @@ -81,6 +87,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-forger"
}
Expand All @@ -91,6 +100,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-snapshots"
}
Expand Down
12 changes: 12 additions & 0 deletions packages/core/bin/config/testnet/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-state"
},
Expand Down Expand Up @@ -47,6 +50,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-state"
},
Expand Down Expand Up @@ -84,6 +90,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-forger"
}
Expand All @@ -94,6 +103,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-manager"
},
{
"package": "@arkecosystem/core-snapshots"
}
Expand Down
10 changes: 0 additions & 10 deletions packages/core/src/commands/core-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ export class Command extends Commands.Command {
networkStart: flags.networkStart,
},
"@arkecosystem/core-forger": await buildBIP38(flags, this.app.getCorePath("config")),
"@arkecosystem/core-manager": {
server: {
http: {
enabled: false,
},
https: {
enabled: false,
},
},
},
},
});

Expand Down
9 changes: 1 addition & 8 deletions packages/core/src/commands/manager-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,7 @@ export class Command extends Commands.Command {

await Utils.buildApplication({
flags,
plugins: {
"@arkecosystem/core-manager": {
watcher: {
enabled: false,
resetDatabase: process.env.CORE_RESET_DATABASE,
},
},
},
plugins: {},
});

// Prevent resolving execute method
Expand Down

0 comments on commit 9eb1ce9

Please sign in to comment.