This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding storage service to keep references to storage clients for use.
- Loading branch information
1 parent
b39296e
commit aade7d3
Showing
14 changed files
with
191 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
packages/desktop-core/src/main/instances/storageInstanceService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/desktop-core/src/main/launcher/storageLauncherService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/desktop-core/src/main/storage/defaultStorageService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum StorageState { | ||
failed = "Failed", | ||
provisioned = "Provisioned", | ||
} |