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

BREAKING CHANGE: The method `ParseServer.verifyServerUrl` now returns a promise instead of a callback.
This commit is contained in:
Daniel
2023-01-09 04:23:01 +11:00
committed by GitHub
parent 76c7a6fbd0
commit ffa4974158
3 changed files with 53 additions and 37 deletions

View File

@@ -29,27 +29,20 @@ describe('Server Url Checks', () => {
server.close(done); server.close(done);
}); });
it('validate good server url', done => { it('validate good server url', async () => {
Parse.serverURL = 'http://localhost:13376'; Parse.serverURL = 'http://localhost:13376';
ParseServer.verifyServerUrl(async result => { const response = await ParseServer.verifyServerUrl();
if (!result) { expect(response).toBeTrue();
done.fail('Did not pass valid url');
}
await reconfigureServer();
done();
});
}); });
it('mark bad server url', done => { it('mark bad server url', async () => {
spyOn(console, 'warn').and.callFake(() => {}); spyOn(console, 'warn').and.callFake(() => {});
Parse.serverURL = 'notavalidurl'; Parse.serverURL = 'notavalidurl';
ParseServer.verifyServerUrl(async result => { const response = await ParseServer.verifyServerUrl();
if (result) { expect(response).not.toBeTrue();
done.fail('Did not mark invalid url'); expect(console.warn).toHaveBeenCalledWith(
} `\nWARNING, Unable to connect to 'notavalidurl' as the URL is invalid. Cloud code and push notifications may be unavailable!\n`
await reconfigureServer(); );
done();
});
}); });
xit('handleShutdown, close connection', done => { xit('handleShutdown, close connection', done => {

View File

@@ -546,6 +546,12 @@ describe('server', () => {
const health = await request({ const health = await request({
url: 'http://localhost:12701/parse/health', url: 'http://localhost:12701/parse/health',
}).catch(e => e); }).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.data.status).toBe('initialized');
expect(health.status).toBe(503); expect(health.status).toBe(503);
await new Promise(resolve => server.close(resolve)); await new Promise(resolve => server.close(resolve));
@@ -573,6 +579,8 @@ describe('server', () => {
expect(health.data.status).toBe('starting'); expect(health.data.status).toBe('starting');
expect(health.status).toBe(503); expect(health.status).toBe(503);
expect(health.headers['retry-after']).toBe('1'); expect(health.headers['retry-after']).toBe('1');
const response = await ParseServer.default.verifyServerUrl();
expect(response).toBeTrue();
await startingPromise; await startingPromise;
await new Promise(resolve => server.close(resolve)); await new Promise(resolve => server.close(resolve));
}); });

View File

@@ -392,30 +392,45 @@ class ParseServer {
return server; return server;
} }
static verifyServerUrl(callback) { static async verifyServerUrl() {
// perform a health check on the serverURL value // perform a health check on the serverURL value
if (Parse.serverURL) { 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'); const request = require('./request');
request({ url: Parse.serverURL.replace(/\/$/, '') + '/health' }) const response = await request({ url }).catch(response => response);
.catch(response => response) const json = response.data || null;
.then(response => { console.log(response.status, { json });
const json = response.data || null; const retry = response.headers['retry-after'];
if (response.status !== 200 || !json || (json && json.status !== 'ok')) { if (retry) {
/* eslint-disable no-console */ await new Promise(resolve => setTimeout(resolve, retry * 1000));
console.warn( return this.verifyServerUrl();
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` + }
` Cloud code and push notifications may be unavailable!\n` if (response.status !== 200 || json?.status !== 'ok') {
); /* eslint-disable no-console */
/* eslint-enable no-console */ console.warn(
if (callback) { `\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
callback(false); ` Cloud code and push notifications may be unavailable!\n`
} );
} else { /* eslint-enable no-console */
if (callback) { return;
callback(true); }
} return true;
}
});
} }
} }
} }