Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-manager): implement snapshots.create action #3728

Merged
merged 2 commits into from May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions __tests__/unit/core-manager/action-reader.test.ts
Expand Up @@ -12,6 +12,7 @@ beforeEach(() => {
sandbox = new Sandbox();

sandbox.app.bind(Identifiers.ActionReader).to(ActionReader).inSingletonScope();
sandbox.app.bind(Identifiers.SnapshotsManager).toConstantValue({});
sandbox.app.bind(Container.Identifiers.PluginConfiguration).toConstantValue({});
sandbox.app.bind(Container.Identifiers.FilesystemService).toConstantValue({});

Expand Down
42 changes: 42 additions & 0 deletions __tests__/unit/core-manager/actions/snapshots-create.test.ts
@@ -0,0 +1,42 @@
import "jest-extended";

import { Action } from "@packages/core-manager/src/actions/snapshots-create";
import { Identifiers } from "@packages/core-manager/src/ioc";
import { Sandbox } from "@packages/core-test-framework";

let sandbox: Sandbox;
let action: Action;

const mockSnapshotManager = {
start: jest.fn(),
};

beforeEach(() => {
sandbox = new Sandbox();

sandbox.app.bind(Identifiers.SnapshotsManager).toConstantValue(mockSnapshotManager);

sandbox.app.network = jest.fn().mockReturnValue("testnet");

action = sandbox.app.resolve(Action);
});

describe("Snapshots:Create", () => {
it("should have name", () => {
expect(action.name).toEqual("snapshots.create");
});

it("should return empty object if ok", async () => {
const result = await action.execute({});

expect(result).toEqual({});
});

it("should return error if manager throws error", async () => {
mockSnapshotManager.start.mockImplementation(async () => {
throw new Error();
});

await expect(action.execute({})).rejects.toThrow();
});
});
48 changes: 48 additions & 0 deletions __tests__/unit/core-manager/snapshots/snapshot-manager.test.ts
@@ -0,0 +1,48 @@
import "jest-extended";

import { Container } from "@packages/core-kernel";
import { Identifiers } from "@packages/core-manager/src/ioc";
import { SnapshotsManager } from "@packages/core-manager/src/snapshots/snapshots-manager";
import { Sandbox } from "@packages/core-test-framework";

let sandbox: Sandbox;
let snapshotsManager: SnapshotsManager;

const mockSnapshotService = {
dump: () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 500);
});
},
};

beforeEach(() => {
sandbox = new Sandbox();

sandbox.app.bind(Identifiers.SnapshotsManager).to(SnapshotsManager);
sandbox.app.bind(Container.Identifiers.SnapshotService).toConstantValue(mockSnapshotService);

snapshotsManager = sandbox.app.get(Identifiers.SnapshotsManager);
});

describe("Snapshots:Create", () => {
it("should resolve if no actions is running", async () => {
await expect(snapshotsManager.start("dummy_name", {})).toResolve();

// Delay
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 800);
});

await expect(snapshotsManager.start("dummy_name", {})).toResolve();
});

it("should throw if another action is running", async () => {
await expect(snapshotsManager.start("dummy_name", {})).toResolve();
await expect(snapshotsManager.start("dummy_name", {})).rejects.toThrow();
});
});
43 changes: 43 additions & 0 deletions packages/core-manager/src/actions/snapshots-create.ts
@@ -0,0 +1,43 @@
import { Application, Container } from "@arkecosystem/core-kernel";

import { Actions } from "../contracts";
import { Identifiers } from "../ioc";
import { SnapshotsManager } from "../snapshots/snapshots-manager";

@Container.injectable()
export class Action implements Actions.Action {
public name = "snapshots.create";

public schema = {
type: "object",
properties: {
codec: {
type: "string",
},
skipCompression: {
type: "boolean",
},
start: {
type: "number",
},
end: {
type: "number",
},
},
};

@Container.inject(Container.Identifiers.Application)
private readonly app!: Application;

@Container.inject(Identifiers.SnapshotsManager)
private readonly snapshotManager!: SnapshotsManager;

public async execute(params: any): Promise<any> {
await this.snapshotManager.start("snapshot:create", {
network: this.app.network(),
...params,
});

return {};
}
}
1 change: 1 addition & 0 deletions packages/core-manager/src/ioc/identifiers.ts
Expand Up @@ -6,4 +6,5 @@ export const Identifiers = {
PluginFactory: Symbol.for("Factory<Plugin>"),
BasicCredentialsValidator: Symbol.for("Validator<BasicCreadentials>"),
TokenValidator: Symbol.for("Validator<Token>"),
SnapshotsManager: Symbol.for("Manager<Snapshots>"),
};
4 changes: 3 additions & 1 deletion packages/core-manager/src/service-provider.ts
Expand Up @@ -3,17 +3,19 @@ import { Container, Providers, Types } from "@arkecosystem/core-kernel";

import { ActionReader } from "./action-reader";
import { Identifiers } from "./ioc";
import Handlers from "./server/handlers";
import { PluginFactory } from "./server/plugins";
import { Server } from "./server/server";
import { Argon2id, SimpleTokenValidator } from "./server/validators";
import Handlers from "./server/handlers";
import { SnapshotsManager } from "./snapshots/snapshots-manager";

export class ServiceProvider extends Providers.ServiceProvider {
public async register(): Promise<void> {
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();

const pkg: Types.PackageJson = require("../package.json");
this.app.bind(Identifiers.CLI).toConstantValue(ApplicationFactory.make(new Container.Container(), pkg));
Expand Down
22 changes: 22 additions & 0 deletions packages/core-manager/src/snapshots/snapshots-manager.ts
@@ -0,0 +1,22 @@
import { Container, Contracts } from "@arkecosystem/core-kernel";

@Container.injectable()
export class SnapshotsManager {
@Container.inject(Container.Identifiers.SnapshotService)
private readonly snapshotService!: Contracts.Snapshot.SnapshotService;

private processInAction?: string;

// TODO: Contracts.Snapshot.DumpOptions
public async start(name: string, options: any): Promise<void> {
if (this.processInAction) {
throw new Error(`Process ${this.processInAction} is executing`);
}

this.processInAction = name;

this.snapshotService.dump(options).finally(() => {
this.processInAction = undefined;
});
}
}
3 changes: 3 additions & 0 deletions packages/core/bin/config/devnet/app.json
Expand Up @@ -91,6 +91,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-snapshots"
},
{
"package": "@arkecosystem/core-snapshots"
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/bin/config/mainnet/app.json
Expand Up @@ -91,6 +91,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-snapshots"
},
{
"package": "@arkecosystem/core-snapshots"
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/bin/config/testnet/app.json
Expand Up @@ -101,6 +101,9 @@
{
"package": "@arkecosystem/core-logger-pino"
},
{
"package": "@arkecosystem/core-snapshots"
},
{
"package": "@arkecosystem/core-manager"
}
Expand Down