Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: LiveQuery server is not shut down properly when handleShutdown is called #8491

Merged
merged 40 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9ef1968
ci: fix node engine check (#7891)
mtrezza Mar 25, 2022
f63fb2b
fix: return correct response when revert is used in beforeSave (#7839)
dblythy Mar 26, 2022
b3199d7
chore(release): 5.2.1-alpha.1 [skip ci]
semantic-release-bot Mar 26, 2022
48bd512
perf: reduce database operations when using the constant parameter in…
dblythy Mar 26, 2022
e0cca58
chore(release): 5.2.1-alpha.2 [skip ci]
semantic-release-bot Mar 26, 2022
90155cf
feat: add MongoDB 5.1 compatibility (#7682)
github-actions[bot] Mar 27, 2022
499cead
chore(release): 5.3.0-alpha.1 [skip ci]
semantic-release-bot Mar 27, 2022
ef56e98
fix: security upgrade parse push adapter from 4.1.0 to 4.1.2 (#7893)
mtrezza Mar 27, 2022
119dbe0
chore(release): 5.3.0-alpha.2 [skip ci]
semantic-release-bot Mar 27, 2022
6b4b358
feat: add MongoDB 5.2 support (#7894)
mtrezza Mar 27, 2022
75eca2d
chore(release): 5.3.0-alpha.3 [skip ci]
semantic-release-bot Mar 27, 2022
61ef23f
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy May 3, 2022
606ed96
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy May 5, 2022
1e89817
Merge branch 'alpha' of https://github.com/dblythy/parse-server into …
dblythy Jun 7, 2022
9720e8e
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Jun 7, 2022
7bc5c36
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Jun 17, 2022
7b2ab38
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Jun 18, 2022
3c8f5cc
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Sep 18, 2022
5814437
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Sep 29, 2022
986c6c2
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Nov 3, 2022
6a40dc4
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Nov 10, 2022
a9fb355
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Dec 20, 2022
a563b21
fix: shutdown liveQuery server properly when `handleShutdown` is called
dblythy Mar 30, 2023
f9263c1
Update ParseLiveQuery.spec.js
dblythy Mar 30, 2023
a133db4
tests
dblythy Mar 30, 2023
31c1b48
Update ParseLiveQuery.spec.js
dblythy Mar 30, 2023
a8b2ed2
Update ParseLiveQuery.spec.js
dblythy Mar 30, 2023
8c987bc
Update ParseLiveQuery.spec.js
dblythy Mar 30, 2023
9a234b1
handle shutdown
dblythy Mar 30, 2023
cdf1c32
Update ParseLiveQuery.spec.js
dblythy Mar 30, 2023
f18e0c2
Update ParseLiveQuery.spec.js
dblythy Mar 30, 2023
da12ad9
add shutdown
dblythy Mar 31, 2023
54ed9d3
Update ParseLiveQuery.spec.js
dblythy Mar 31, 2023
61f879d
rename
dblythy Apr 10, 2023
0040e35
Merge branch 'alpha' into shutdown-lq
mtrezza Apr 11, 2023
120f964
Merge branch 'alpha' into shutdown-lq
mtrezza Apr 11, 2023
de8b8ea
Merge remote-tracking branch 'upstream/alpha' into alpha
dblythy Apr 12, 2023
0397538
Merge branch 'alpha' into shutdown-lq
dblythy Apr 12, 2023
e6aa2ca
Merge branch 'alpha' into shutdown-lq
mtrezza Jun 8, 2023
91c0bdb
Merge branch 'alpha' into shutdown-lq
mtrezza Jun 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const Auth = require('../lib/Auth');
const UserController = require('../lib/Controllers/UserController').UserController;
const Config = require('../lib/Config');
const ParseServer = require('../lib/index').ParseServer;
const triggers = require('../lib/triggers');
const validatorFail = () => {
throw 'you are not authorized';
Expand Down Expand Up @@ -1214,6 +1215,40 @@ describe('ParseLiveQuery', function () {
await object.save();
});

it('does shutdown liveQuery server', async () => {
await reconfigureServer({ appId: 'test_app_id' });
const config = {
appId: 'hello_test',
masterKey: 'world',
port: 1345,
mountPath: '/1',
serverURL: 'http://localhost:1345/1',
liveQuery: {
classNames: ['Yolo'],
},
startLiveQueryServer: true,
};
if (process.env.PARSE_SERVER_TEST_DB === 'postgres') {
config.databaseAdapter = new databaseAdapter.constructor({
uri: databaseURI,
collectionPrefix: 'test_',
});
config.filesAdapter = defaultConfiguration.filesAdapter;
}
const server = await ParseServer.startApp(config);
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
client.serverURL = 'ws://localhost:1345/1';
const query = await new Parse.Query('Yolo').subscribe();
await Promise.all([
server.handleShutdown(),
new Promise(resolve => query.on('close', resolve)),
]);
await new Promise(resolve => setTimeout(resolve, 100));
expect(server.liveQueryServer.server.address()).toBeNull();
expect(server.liveQueryServer.subscriber.isOpen).toBeFalse();
await new Promise(resolve => server.server.close(resolve));
});

it('prevent afterSave trigger if not exists', async () => {
await reconfigureServer({
liveQuery: {
Expand Down
7 changes: 4 additions & 3 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,12 @@ export class MongoStorageAdapter implements StorageAdapter {
throw error;
}

handleShutdown() {
async handleShutdown() {
if (!this.client) {
return Promise.resolve();
return;
}
return this.client.close(false);
await this.client.close(false);
delete this.connectionPromise;
}

_adaptiveCollection(name: string) {
Expand Down
4 changes: 3 additions & 1 deletion src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
const now = new Date().getTime();
const helpers = this._pgp.helpers;
debug('deleteAllClasses');

if (this._client?.$pool.ended) {
return;
}
await this._client
.task('delete-all-classes', async t => {
try {
Expand Down
15 changes: 15 additions & 0 deletions src/LiveQuery/ParseLiveQueryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ class ParseLiveQueryServer {
}
this._createSubscribers();
}

async shutdown() {
if (this.subscriber.isOpen) {
await Promise.all([
...[...this.clients.values()].map(client => client.parseWebSocket.ws.close()),
this.parseWebSocketServer.close(),
...Array.from(this.subscriber.subscriptions.keys()).map(key =>
this.subscriber.unsubscribe(key)
),
this.subscriber.close?.(),
]);
}
this.subscriber.isOpen = false;
}

_createSubscribers() {
const messageRecieved = (channel, messageStr) => {
logger.verbose('Subscribe message %j', messageStr);
Expand Down
6 changes: 6 additions & 0 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ class ParseServer {
if (cacheAdapter && typeof cacheAdapter.handleShutdown === 'function') {
promises.push(cacheAdapter.handleShutdown());
}
if (this.liveQueryServer?.server?.close) {
promises.push(new Promise(resolve => this.liveQueryServer.server.close(resolve)));
}
if (this.liveQueryServer) {
promises.push(this.liveQueryServer.shutdown());
}
return (promises.length > 0 ? Promise.all(promises) : Promise.resolve()).then(() => {
if (this.config.serverCloseComplete) {
this.config.serverCloseComplete();
Expand Down