Skip to content

Commit aaef734

Browse files
committed
Made Core destroy procedure async
1 parent 2ead7b3 commit aaef734

File tree

15 files changed

+76
-42
lines changed

15 files changed

+76
-42
lines changed

__tests__/auth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ describe('Authentication', () => {
116116
expect(response.json).toBeCalledWith({username: 'jest'});
117117
});
118118

119-
test('#destroy', () => {
120-
auth = auth.destroy();
119+
test('#destroy', async () => {
120+
await auth.destroy();
121+
auth = undefined;
121122
});
122123
});

__tests__/core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ describe('Core', () => {
9090
expect(core.wss.clients[2].send).not.toBeCalled();
9191
});
9292

93-
test('#destroy', () => {
93+
test('#destroy', async () => {
9494
const cb = jest.fn();
9595
core.on('osjs/core:destroy', cb);
96-
core.destroy();
97-
core.destroy();
96+
await core.destroy();
97+
await core.destroy();
9898
expect(cb).toHaveBeenCalledTimes(1);
9999
});
100100
});

__tests__/filesystem.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ describe('Filesystem', () => {
100100
});
101101

102102
test('#unmount', () => {
103-
expect(filesystem.unmount(mountpoint))
103+
return expect(filesystem.unmount(mountpoint))
104+
.resolves
104105
.toBe(true);
105106
});
106107

107108
test('#unmount - test fail', () => {
108-
expect(filesystem.unmount({}))
109+
return expect(filesystem.unmount({}))
110+
.resolves
109111
.toBe(false);
110112
});
111113

@@ -132,7 +134,8 @@ describe('Filesystem', () => {
132134
}));
133135
});
134136

135-
test('#destroy', () => {
136-
filesystem = filesystem.destroy();
137+
test('#destroy', async () => {
138+
await filesystem.destroy();
139+
filesystem = undefined;
137140
});
138141
});

__tests__/package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('Package', () => {
9696
.toBe(path.resolve(core.configuration.public, 'apps/JestTest'));
9797
});
9898

99-
test('#destroy', () => {
100-
pkg.destroy();
99+
test('#destroy', async () => {
100+
await pkg.destroy();
101101
});
102102
});

__tests__/packages.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ describe('Packages', () => {
4747
}));
4848
});
4949

50-
test('#destroy', () => {
51-
packages = packages.destroy();
50+
test('#destroy', async () => {
51+
await packages.destroy();
52+
packages = undefined;
5253
});
5354
});

__tests__/settings.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ describe('Settings', () => {
5656
expect(response.json).toBeCalledWith({});
5757
});
5858

59-
test('#destroy', () => {
60-
settings = settings.destroy();
59+
test('#destroy', async () => {
60+
await settings.destroy();
61+
settings = undefined;
6162
});
6263
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
"homepage": "https://github.com/os-js/osjs-server#readme",
3636
"dependencies": {
37-
"@osjs/common": "^3.0.6",
37+
"@osjs/common": "^3.0.8",
3838
"body-parser": "^1.19.0",
3939
"chokidar": "^3.3.1",
4040
"connect-loki": "^1.1.0",

src/core.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Core extends CoreBase {
9494
*/
9595
destroy(done = () => {}) {
9696
if (this.destroyed) {
97-
return;
97+
return Promise.resolve();
9898
}
9999

100100
this.emit('osjs/core:destroy');
@@ -105,13 +105,21 @@ class Core extends CoreBase {
105105
this.wss.close();
106106
}
107107

108-
super.destroy();
108+
const finish = (error) => {
109+
if (error) {
110+
logger.error(error);
111+
}
109112

110-
if (this.httpServer) {
111-
this.httpServer.close(done);
112-
} else {
113-
done();
114-
}
113+
if (this.httpServer) {
114+
this.httpServer.close(done);
115+
} else {
116+
done();
117+
}
118+
};
119+
120+
return Promise.resolve(super.destroy())
121+
.then(() => finish())
122+
.catch(finish);
115123
}
116124

117125
/**

src/filesystem.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const uuid = require('uuid/v1');
3434
const mime = require('mime');
3535
const path = require('path');
3636
const vfs = require('./vfs');
37+
const {closeWatches} = require('./utils/core.js');
3738
const consola = require('consola');
3839
const logger = consola.withTag('Filesystem');
3940

@@ -62,12 +63,12 @@ class Filesystem {
6263
/**
6364
* Destroys instance
6465
*/
65-
destroy() {
66-
this.watches.forEach(({watch}) => {
67-
if (watch && typeof watch.close === 'function') {
68-
watch.close();
69-
}
70-
});
66+
async destroy() {
67+
const watches = this.watches.filter(({watch}) => {
68+
return watch && typeof watch.close === 'function';
69+
}).map(({watch}) => watch);
70+
71+
await closeWatches(watches);
7172

7273
this.watches = [];
7374
}
@@ -204,13 +205,13 @@ class Filesystem {
204205
/**
205206
* Unmounts given mountpoint
206207
* @param {object} mount Mountpoint
207-
* @return {boolean}
208+
* @return {Promise<boolean>}
208209
*/
209-
unmount(mountpoint) {
210+
async unmount(mountpoint) {
210211
const found = this.watches.find(w => w.id === mountpoint.id);
211212

212213
if (found && found.watch) {
213-
found.watch.close();
214+
await found.watch.close();
214215
}
215216

216217
const index = this.mountpoints.indexOf(mountpoint);
@@ -271,6 +272,8 @@ class Filesystem {
271272
}, args], filter);
272273
});
273274

275+
watch.on('error', error => logger.warn('Mountpoint watch error', error));
276+
274277
this.watches.push({
275278
id: mountpoint.id,
276279
watch

src/package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class Package {
5757
/**
5858
* Destroys instance
5959
*/
60-
destroy() {
60+
async destroy() {
6161
this.action('destroy');
6262

6363
if (this.watcher) {
64-
this.watcher.close();
64+
await this.watcher.close();
6565
this.watcher = null;
6666
}
6767
}

0 commit comments

Comments
 (0)