🩹 Add stop to platform and wait for it in CLI (#438)
This commit is contained in:
committed by
ericlinagora
parent
7c31550339
commit
4869c70bbc
@@ -131,7 +131,9 @@ const command: yargs.CommandModule<unknown, unknown> = {
|
|||||||
repository,
|
repository,
|
||||||
spinner,
|
spinner,
|
||||||
});
|
});
|
||||||
|
spinner.start("Shutting down platform...");
|
||||||
|
await platform.stop();
|
||||||
|
spinner.succeed("Platform shutdown");
|
||||||
spinner.stop();
|
spinner.stop();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -36,6 +36,14 @@ export default class Database extends TdriveService<DatabaseServiceAPI> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async doStop(): Promise<this> {
|
||||||
|
if (this.service) {
|
||||||
|
await this.service.disconnect();
|
||||||
|
this.service = null;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
api(): DatabaseServiceAPI {
|
api(): DatabaseServiceAPI {
|
||||||
return this.service;
|
return this.service;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,13 @@ export default class DatabaseService implements DatabaseServiceAPI {
|
|||||||
return this.connector;
|
return this.connector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async disconnect() {
|
||||||
|
if (this.connector) {
|
||||||
|
await this.connector.disconnect();
|
||||||
|
this.connector = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getManager(): Manager<unknown> {
|
getManager(): Manager<unknown> {
|
||||||
return new Manager<unknown>(this.connector);
|
return new Manager<unknown>(this.connector);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
@@ -9,6 +9,7 @@ export abstract class AbstractConnector<T extends ConnectionOptions> implements
|
|||||||
constructor(protected type: DatabaseType, protected options: T, protected secret: string) {}
|
constructor(protected type: DatabaseType, protected options: T, protected secret: string) {}
|
||||||
|
|
||||||
abstract connect(): Promise<this>;
|
abstract connect(): Promise<this>;
|
||||||
|
abstract disconnect(): Promise<this>;
|
||||||
|
|
||||||
abstract drop(): Promise<this>;
|
abstract drop(): Promise<this>;
|
||||||
|
|
||||||
|
|||||||
+6
@@ -178,6 +178,12 @@ export class CassandraConnector extends AbstractConnector<CassandraConnectionOpt
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async disconnect(): Promise<this> {
|
||||||
|
if (this.client) await this.client.shutdown();
|
||||||
|
this.client = null;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
async getTableDefinition(name: string): Promise<string[]> {
|
async getTableDefinition(name: string): Promise<string[]> {
|
||||||
let result: string[];
|
let result: string[];
|
||||||
|
|
||||||
|
|||||||
+5
@@ -22,6 +22,11 @@ export interface Connector extends Initializable {
|
|||||||
*/
|
*/
|
||||||
connect(): Promise<this>;
|
connect(): Promise<this>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnect from the database
|
||||||
|
*/
|
||||||
|
disconnect(): Promise<this>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the type of connector
|
* Get the type of connector
|
||||||
*/
|
*/
|
||||||
|
|||||||
+6
@@ -36,6 +36,12 @@ export class MongoConnector extends AbstractConnector<MongoConnectionOptions> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async disconnect(): Promise<this> {
|
||||||
|
if (this.client) await this.client.close();
|
||||||
|
this.client = null;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
getClient(): mongo.MongoClient {
|
getClient(): mongo.MongoClient {
|
||||||
return this.client;
|
return this.client;
|
||||||
}
|
}
|
||||||
|
|||||||
+6
@@ -50,6 +50,12 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async disconnect(): Promise<this> {
|
||||||
|
if (this.client) await this.client.end();
|
||||||
|
this.client = null;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
async init(): Promise<this> {
|
async init(): Promise<this> {
|
||||||
if (!this.client) {
|
if (!this.client) {
|
||||||
await this.connect();
|
await this.connect();
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ export class SearchAdapter implements SearchAdapterInterface {
|
|||||||
connect(): Promise<void> {
|
connect(): Promise<void> {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
disconnect(): Promise<void> {
|
||||||
|
throw new Error("Method not implemented.");
|
||||||
|
}
|
||||||
upsert(entities: any[]): Promise<void> {
|
upsert(entities: any[]): Promise<void> {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-2
@@ -29,6 +29,7 @@ export default class ESAndOpenSearch extends SearchAdapter implements SearchAdap
|
|||||||
private bulkReaders = 0;
|
private bulkReaders = 0;
|
||||||
private buffer: Operation[] = [];
|
private buffer: Operation[] = [];
|
||||||
private client: OpenClient | ESClient;
|
private client: OpenClient | ESClient;
|
||||||
|
private pendingTimeout: NodeJS.Timeout = null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
readonly database: DatabaseServiceAPI,
|
readonly database: DatabaseServiceAPI,
|
||||||
@@ -75,6 +76,18 @@ export default class ESAndOpenSearch extends SearchAdapter implements SearchAdap
|
|||||||
this.startBulkReader();
|
this.startBulkReader();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async disconnect() {
|
||||||
|
if (this.client) {
|
||||||
|
const client = this.client;
|
||||||
|
this.client = null;
|
||||||
|
if (this.pendingTimeout) {
|
||||||
|
clearTimeout(this.pendingTimeout);
|
||||||
|
this.pendingTimeout = null;
|
||||||
|
}
|
||||||
|
await client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async createIndex(
|
private async createIndex(
|
||||||
entity: EntityDefinition,
|
entity: EntityDefinition,
|
||||||
_columns: { [name: string]: ColumnDefinition },
|
_columns: { [name: string]: ColumnDefinition },
|
||||||
@@ -214,10 +227,17 @@ export default class ESAndOpenSearch extends SearchAdapter implements SearchAdap
|
|||||||
|
|
||||||
let buffer;
|
let buffer;
|
||||||
do {
|
do {
|
||||||
await new Promise(r =>
|
await new Promise<void>(
|
||||||
setTimeout(r, parseInt(`${this.configuration.flushInterval}`) || 3000),
|
r =>
|
||||||
|
(this.pendingTimeout = setTimeout(() => {
|
||||||
|
this.pendingTimeout = null;
|
||||||
|
r();
|
||||||
|
}, parseInt(`${this.configuration.flushInterval}`) || 3000)),
|
||||||
);
|
);
|
||||||
buffer = this.buffer;
|
buffer = this.buffer;
|
||||||
|
if (!this.client)
|
||||||
|
// disconnect was called; break this tail loop
|
||||||
|
return;
|
||||||
} while (buffer.length === 0);
|
} while (buffer.length === 0);
|
||||||
this.buffer = [];
|
this.buffer = [];
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,14 @@ export default class MongoSearch extends SearchAdapter implements SearchAdapterI
|
|||||||
this.mongodb = await service.getDatabase();
|
this.mongodb = await service.getDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async disconnect() {
|
||||||
|
if (this.mongodb) {
|
||||||
|
const service = this.database.getConnector() as MongoConnector;
|
||||||
|
await service.disconnect();
|
||||||
|
this.mongodb = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async createIndex(
|
private async createIndex(
|
||||||
entityDefinition: EntityDefinition,
|
entityDefinition: EntityDefinition,
|
||||||
columns: { [name: string]: ColumnDefinition },
|
columns: { [name: string]: ColumnDefinition },
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ export type IndexedEntity = {
|
|||||||
|
|
||||||
export interface SearchAdapterInterface {
|
export interface SearchAdapterInterface {
|
||||||
connect(): Promise<void>;
|
connect(): Promise<void>;
|
||||||
|
disconnect(): Promise<void>;
|
||||||
upsert(entities: any[]): Promise<void>;
|
upsert(entities: any[]): Promise<void>;
|
||||||
remove(entities: any[]): Promise<void>;
|
remove(entities: any[]): Promise<void>;
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,14 @@ export default class Search extends TdriveService<SearchServiceAPI> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async doStop(): Promise<this> {
|
||||||
|
if (this.service) {
|
||||||
|
await this.service.disconnect();
|
||||||
|
this.service = null;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public async upsert(entities: never[]) {
|
public async upsert(entities: never[]) {
|
||||||
return this.service.upsert(entities);
|
return this.service.upsert(entities);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user