Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
feat(core): storage service
Browse files Browse the repository at this point in the history
Adding storage service to keep references to storage clients for use.
  • Loading branch information
markmcdowell committed Jun 18, 2020
1 parent b39296e commit aade7d3
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/desktop-core/src/main/instances/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CompositeInstanceService } from "./compositeInstanceService";
import { WindowInstanceService } from "./windowInstanceService";
import { StorageInstanceService } from "./storageInstanceService";

export * from "./iInstanceService";

const windowInstanceService = new WindowInstanceService();
const storageInstanceService = new StorageInstanceService();

export const instanceService = new CompositeInstanceService(windowInstanceService);
export const instanceService = new CompositeInstanceService(windowInstanceService, storageInstanceService);
30 changes: 30 additions & 0 deletions packages/desktop-core/src/main/instances/storageInstanceService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IInstanceService } from "./iInstanceService";
import { storageService } from "../storage";

export class StorageInstanceService implements IInstanceService {
public get(uid: string) {
return storageService.from(uid)?.configuration;
}

public list() {
return storageService.all().map((instance) => instance.configuration);
}

public kill() {
const error = new Error("You can't kill storage");

return Promise.reject(error);
}

public restart() {
const error = new Error("You can't restart storage");

return Promise.reject(error);
}

public stop() {
const error = new Error("You can't stop storage");

return Promise.reject(error);
}
}
18 changes: 11 additions & 7 deletions packages/desktop-core/src/main/launcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@ import { logger } from "../logging";
import { processExec, processFork } from "../processes";
import { sessionService } from "../session";
import { windowService } from "../windowing";

import { ApplicationLauncherService } from "./applicationLauncherService";
import { CompositeLauncherService } from "./compositeLauncherService";
import { ExternalLauncherService } from "./externalLauncherService";
import { ILauncherService } from "./iLauncherService";
import { NodeServiceLauncherService } from "./nodeServiceLauncherService";
import { SessionConfigurationLauncherService } from "./sessionConfigurationLauncherService";
import { TrayConfigurationLauncherService } from "./trayConfigurationLauncherService";
import { SessionLauncherService } from "./sessionLauncherService";
import { TrayLauncherService } from "./trayLauncherService";
import { trayService } from "../tray";
import { StorageLauncherService } from "./storageLauncherService";
import { storageService } from "../storage";

const applicationLauncherService = new ApplicationLauncherService(logger, windowService);

const externalLauncherService = new ExternalLauncherService(logger, processExec);

const serviceLauncherService = new NodeServiceLauncherService(logger, processFork);

const sessionConfigurationLauncherService = new SessionConfigurationLauncherService(logger, sessionService);
const sessionLauncherService = new SessionLauncherService(logger, sessionService);

const trayLauncherService = new TrayLauncherService(logger, trayService);

const trayConfigurationLauncherService = new TrayConfigurationLauncherService(logger, trayService);
const storageLauncherService = new StorageLauncherService(logger, storageService);

export const launcherService: ILauncherService = new CompositeLauncherService(
applicationLauncherService,
serviceLauncherService,
sessionConfigurationLauncherService,
trayConfigurationLauncherService,
trayLauncherService,
storageLauncherService,
sessionLauncherService,
externalLauncherService,
);
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ISessionService } from "../session";

import { ILauncherService } from "./iLauncherService";

export class SessionConfigurationLauncherService implements ILauncherService {
export class SessionLauncherService implements ILauncherService {
private readonly logger: ILogger;
private readonly sessionService: ISessionService;

Expand Down
28 changes: 28 additions & 0 deletions packages/desktop-core/src/main/launcher/storageLauncherService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { IConfiguration, ConfigurationKind, WellKnownNamespaces } from "@reactivemarkets/desktop-types";
import { ILauncherService } from "./iLauncherService";
import { ILogger } from "../logging";
import { IStorageService } from "../storage";

export class StorageLauncherService implements ILauncherService {
private readonly logger: ILogger;
private readonly storageService: IStorageService;

public constructor(logger: ILogger, storageService: IStorageService) {
this.logger = logger;
this.storageService = storageService;
}

public canLaunch({ kind }: IConfiguration) {
return kind === ConfigurationKind.Storage;
}

public async launch(configuration: IConfiguration) {
const { name, namespace = WellKnownNamespaces.default } = configuration.metadata;

this.logger.verbose(`Configuring storage provisioner: ${name} in ${namespace}`);

const instance = await this.storageService.create(configuration);

return instance.configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ITrayService } from "../tray";
import { ILogger } from "../logging";
import { ILauncherService } from "./iLauncherService";

export class TrayConfigurationLauncherService implements ILauncherService {
export class TrayLauncherService implements ILauncherService {
private readonly logger: ILogger;
private readonly trayService: ITrayService;

Expand Down
56 changes: 56 additions & 0 deletions packages/desktop-core/src/main/storage/defaultStorageService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { IConfiguration, StorageState } from "@reactivemarkets/desktop-types";
import { find } from "ix/iterable";
import { v4 as uuid } from "uuid";
import { IStorageService } from "./iStorageService";
import { IStorageProvisioner } from "./iStorageProvisioner";
import { StorageInstance } from "./storageInstance";

export class DefaultStorageService implements IStorageService {
private readonly storageProvisioner: IStorageProvisioner;
private readonly storageRegistry = new Map<string, StorageInstance>();

public constructor(storageProvisioner: IStorageProvisioner) {
this.storageProvisioner = storageProvisioner;
}

public all() {
return Array.from(this.storageRegistry.values());
}

public from(identifier: string | IConfiguration) {
if (typeof identifier === "string") {
return this.storageRegistry.get(identifier);
}

return find(this.storageRegistry.values(), (instance) => {
const { metadata } = instance.configuration;
return metadata.namespace === identifier.metadata.namespace && metadata.name === identifier.metadata.name;
});
}

public async create(configuration: IConfiguration) {
const storageClient = await this.storageProvisioner.provision(configuration);

const startTime = new Date();
const state = StorageState.provisioned;
const uid = uuid();

const runningConfiguration = {
...configuration,
metadata: {
...configuration.metadata,
uid,
},
status: {
startTime,
state,
},
};

const instance = new StorageInstance(runningConfiguration, storageClient);

this.storageRegistry.set(uid, instance);

return instance;
}
}
22 changes: 22 additions & 0 deletions packages/desktop-core/src/main/storage/iStorageService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IConfiguration } from "@reactivemarkets/desktop-types";
import { StorageInstance } from "./storageInstance";

export interface IStorageService {
/**
* Get all storage clients.
*/
all(): StorageInstance[];

/**
* Get the storage client for the namespace
* @param namespace the application namespace
*/
from(identifier: string | IConfiguration): StorageInstance | undefined;

/**
* Register a new storage client.
* @param namespace the application namespace
* @param storageClient `IStorageClient`
*/
create(configuration: IConfiguration): Promise<StorageInstance>;
}
4 changes: 4 additions & 0 deletions packages/desktop-core/src/main/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { CompositeStorageProvisioner } from "./compositeStorageProvisioner";
import { YamlLocalStorageProvisioner } from "./yamlLocalStorageProvisioner";
import { TransientStorageProvisioner } from "./transientStorageProvisioner";
import { DefaultStorageService } from "./defaultStorageService";

export * from "./iStorageClient";
export * from "./iStorageProvisioner";
export * from "./iStorageService";

const local = new YamlLocalStorageProvisioner();

const transient = new TransientStorageProvisioner();

export const storageProvisioner = new CompositeStorageProvisioner(local, transient);

export const storageService = new DefaultStorageService(storageProvisioner);
20 changes: 20 additions & 0 deletions packages/desktop-core/src/main/storage/storageInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IConfiguration } from "@reactivemarkets/desktop-types";
import { IStorageClient } from "./iStorageClient";

export class StorageInstance {
#configuration: IConfiguration;
#instance: IStorageClient;

public constructor(configuration: IConfiguration, instance: IStorageClient) {
this.#configuration = configuration;
this.#instance = instance;
}

public get configuration() {
return this.#configuration;
}

public get instance() {
return this.#instance;
}
}
3 changes: 2 additions & 1 deletion packages/desktop-types/src/configuration/iConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ITraySpecification } from "./iTraySpecification";
import { IApplicationStatus } from "./iApplicationStatus";
import { IExternalStatus } from "./iExternalStatus";
import { IServiceStatus } from "./iServiceStatus";
import { IStorageStatus } from "./iStorageStatus";

type Spec =
| IApplicationSpecification
Expand All @@ -18,7 +19,7 @@ type Spec =
| IStorageSpecification
| ITraySpecification;

type Status = IApplicationStatus | IExternalStatus | IServiceStatus;
type Status = IApplicationStatus | IExternalStatus | IServiceStatus | IStorageStatus;

export interface IConfiguration {
readonly apiVersion?: string;
Expand Down
7 changes: 7 additions & 0 deletions packages/desktop-types/src/configuration/iStorageStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { StorageState } from "./storageState";

export interface IStorageStatus {
readonly message?: string;
readonly state: StorageState;
readonly startTime?: Date;
}
2 changes: 2 additions & 0 deletions packages/desktop-types/src/configuration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export * from "./iServiceParameters";
export * from "./iServiceSpecification";
export * from "./iServiceStatus";
export * from "./iSessionSpecification";
export * from "./iStorageStatus";
export * from "./iStorageSpecification";
export * from "./iTraySpecification";
export * from "./iWindowParameters";
export * from "./storageState";
export * from "./wellKnownNamespaces";
4 changes: 4 additions & 0 deletions packages/desktop-types/src/configuration/storageState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum StorageState {
failed = "Failed",
provisioned = "Provisioned",
}

0 comments on commit aade7d3

Please sign in to comment.