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

feat: Adapt verifyServerUrl for new asynchronous Parse Server start-up states #8366

Merged
merged 7 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 9 additions & 16 deletions spec/ParseServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,20 @@ describe('Server Url Checks', () => {
server.close(done);
});

it('validate good server url', done => {
it('validate good server url', async () => {
Parse.serverURL = 'http://localhost:13376';
ParseServer.verifyServerUrl(async result => {
if (!result) {
done.fail('Did not pass valid url');
}
await reconfigureServer();
done();
});
const response = await ParseServer.verifyServerUrl();
expect(response).toBeTrue();
});

it('mark bad server url', done => {
it('mark bad server url', async () => {
spyOn(console, 'warn').and.callFake(() => {});
Parse.serverURL = 'notavalidurl';
ParseServer.verifyServerUrl(async result => {
if (result) {
done.fail('Did not mark invalid url');
}
await reconfigureServer();
done();
});
const response = await ParseServer.verifyServerUrl();
expect(response).not.toBeTrue();
expect(console.warn).toHaveBeenCalledWith(
`\nWARNING, Unable to connect to 'notavalidurl' as the URL is invalid. Cloud code and push notifications may be unavailable!\n`
);
});

xit('handleShutdown, close connection', done => {
Expand Down
8 changes: 8 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ describe('server', () => {
const health = await request({
url: 'http://localhost:12701/parse/health',
}).catch(e => e);
spyOn(console, 'warn').and.callFake(() => {});
const verify = await ParseServer.default.verifyServerUrl();
expect(verify).not.toBeTrue();
expect(console.warn).toHaveBeenCalledWith(
`\nWARNING, Unable to connect to 'http://localhost:12701/parse'. Cloud code and push notifications may be unavailable!\n`
);
expect(health.data.status).toBe('initialized');
expect(health.status).toBe(503);
await new Promise(resolve => server.close(resolve));
Expand Down Expand Up @@ -573,6 +579,8 @@ describe('server', () => {
expect(health.data.status).toBe('starting');
expect(health.status).toBe(503);
expect(health.headers['retry-after']).toBe('1');
const response = await ParseServer.default.verifyServerUrl();
expect(response).toBeTrue();
await startingPromise;
await new Promise(resolve => server.close(resolve));
});
Expand Down
57 changes: 36 additions & 21 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,30 +392,45 @@ class ParseServer {
return server;
}

static verifyServerUrl(callback) {
static async verifyServerUrl() {
// perform a health check on the serverURL value
if (Parse.serverURL) {
const isValidHttpUrl = string => {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === 'http:' || url.protocol === 'https:';
};
const url = `${Parse.serverURL.replace(/\/$/, '')}/health`;
if (!isValidHttpUrl(url)) {
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}' as the URL is invalid.` +
` Cloud code and push notifications may be unavailable!\n`
);
return;
}
const request = require('./request');
request({ url: Parse.serverURL.replace(/\/$/, '') + '/health' })
.catch(response => response)
.then(response => {
const json = response.data || null;
if (response.status !== 200 || !json || (json && json.status !== 'ok')) {
/* eslint-disable no-console */
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`
);
/* eslint-enable no-console */
if (callback) {
callback(false);
}
} else {
if (callback) {
callback(true);
}
}
});
const response = await request({ url }).catch(response => response);
const json = response.data || null;
console.log(response.status, { json });
const retry = response.headers['retry-after'];
if (retry) {
await new Promise(resolve => setTimeout(resolve, retry * 1000));
return this.verifyServerUrl();
}
if (response.status !== 200 || json?.status !== 'ok') {
/* eslint-disable no-console */
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`
);
/* eslint-enable no-console */
return;
}
return true;
}
}
}
Expand Down