Skip to content

Commit 5d10ec0

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

15 files changed

+74
-42
lines changed

__tests__/auth.js

+3-2
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

+3-3
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

+7-4
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

+2-2
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

+3-2
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

+3-2
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

+1-1
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

+15-7
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

+10-9
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);

src/package.js

+2-2
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
}

src/packages.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ class Packages {
195195
/**
196196
* Destroys packages
197197
*/
198-
destroy() {
199-
this.packages.forEach(pkg => pkg.destroy());
198+
async destroy() {
199+
await Promise.all(this.packages.map(pkg => pkg.destroy()));
200+
200201
this.packages = [];
201202
}
202203

src/providers/core.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const bodyParser = require('body-parser');
3535
const proxy = require('express-http-proxy');
3636
const nocache = require('nocache');
3737
const {ServiceProvider} = require('@osjs/common');
38-
const {isAuthenticated} = require('../utils/core.js');
38+
const {isAuthenticated, closeWatches} = require('../utils/core.js');
3939

4040
/**
4141
* OS.js Core Service Provider
@@ -50,8 +50,8 @@ class CoreServiceProvider extends ServiceProvider {
5050
this.watches = [];
5151
}
5252

53-
destroy() {
54-
this.watches.forEach(w => w.close());
53+
async destroy() {
54+
await closeWatches(this.watches);
5555
super.destroy();
5656
}
5757

src/providers/packages.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const path = require('path');
3333
const chokidar = require('chokidar');
3434
const {ServiceProvider} = require('@osjs/common');
3535
const Packages = require('../packages');
36+
const {closeWatches} = require('../utils/core');
3637

3738
/**
3839
* OS.js Package Service Provider
@@ -74,9 +75,9 @@ class PackageServiceProvider extends ServiceProvider {
7475
}
7576
}
7677

77-
destroy() {
78-
this.watches.forEach(w => w.close());
79-
this.packages.destroy();
78+
async destroy() {
79+
await closeWatches(this.watches);
80+
await this.packages.destroy();
8081
super.destroy();
8182
}
8283

src/providers/vfs.js

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class VFSServiceProvider extends ServiceProvider {
4444
this.filesystem = new Filesystem(core, options);
4545
}
4646

47+
async destroy() {
48+
await this.filesystem.destroy();
49+
super.destroy();
50+
}
51+
4752
depends() {
4853
return [
4954
'osjs/express'

src/utils/core.js

+10
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,13 @@ module.exports.isAuthenticated = (groups = [], all = false) => (req, res, next)
109109
.status(403)
110110
.send('Access denied');
111111
};
112+
113+
/**
114+
* Closes an array of watches
115+
*/
116+
module.exports.closeWatches = (watches) => Promise.all(
117+
watches.map((w) => {
118+
return w.close()
119+
.catch(error => console.warn(error));
120+
})
121+
);

0 commit comments

Comments
 (0)