Skip to content

Commit 967b180

Browse files
authored
add: shutdown lock method (#50)
1 parent 767a567 commit 967b180

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

meerkat-dbm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devrev/meerkat-dbm",
3-
"version": "0.0.141",
3+
"version": "0.0.142",
44
"dependencies": {
55
"tslib": "^2.3.0",
66
"@duckdb/duckdb-wasm": "^1.28.0",

meerkat-dbm/src/dbm/dbm.spec.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('DBM', () => {
160160
let db: AsyncDuckDB;
161161
let fileManager: FileManagerType;
162162
let dbm: DBM;
163-
let instanceManager;
163+
let instanceManager: InstanceManager;
164164

165165
beforeAll(async () => {
166166
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -179,6 +179,9 @@ describe('DBM', () => {
179179
onEvent: (event) => {
180180
console.log(event);
181181
},
182+
options: {
183+
shutdownInactiveTime: 100,
184+
},
182185
};
183186
dbm = new DBM(options);
184187
});
@@ -389,5 +392,29 @@ describe('DBM', () => {
389392
*/
390393
expect(instanceManager.terminateDB).not.toBeCalled();
391394
});
395+
396+
it('should not shutdown the db if the shutdown lock is true', async () => {
397+
jest.spyOn(instanceManager, 'terminateDB');
398+
399+
/**
400+
* Set the shutdown lock to true
401+
*/
402+
dbm.setShutdownLock(true);
403+
404+
/**
405+
* Execute a query
406+
*/
407+
await dbm.queryWithTableNames('SELECT * FROM table1', ['table1']);
408+
409+
/**
410+
* wait for 200ms
411+
*/
412+
await new Promise((resolve) => setTimeout(resolve, 200));
413+
414+
/**
415+
* Expect instanceManager.terminateDB to not be called
416+
*/
417+
expect(instanceManager.terminateDB).not.toBeCalled();
418+
});
392419
});
393420
});

meerkat-dbm/src/dbm/dbm.ts

+15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class DBM {
1717
private options: DBMConstructorOptions['options'];
1818
private terminateDBTimeout: NodeJS.Timeout | null = null;
1919
private onDuckDBShutdown?: () => void;
20+
private shutdownLock = false;
2021

2122
constructor({
2223
fileManager,
@@ -35,6 +36,13 @@ export class DBM {
3536
}
3637

3738
private async _shutdown() {
39+
/**
40+
* If the shutdown lock is true, then don't shutdown the DB
41+
*/
42+
if (this.shutdownLock) {
43+
return;
44+
}
45+
3846
if (this.connection) {
3947
await this.connection.close();
4048
this.connection = null;
@@ -285,4 +293,11 @@ export class DBM {
285293
*/
286294
return connection.query(query);
287295
}
296+
297+
/**
298+
* Set the shutdown lock to prevent the DB from shutting down
299+
*/
300+
public async setShutdownLock(state: boolean) {
301+
this.shutdownLock = state;
302+
}
288303
}

0 commit comments

Comments
 (0)