Skip to content

Commit

Permalink
Improve shutdown: better error handling & move to the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed May 2, 2024
1 parent 42717d3 commit 1787d67
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/stop-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ function softShutdown(token: string) {

// To handle all this, we send a HTTP request to the GraphQL API instead, which triggers the same thing.
return new Promise<void>((resolve, reject) => {
const req = http.request("http://127.0.0.1:45457", {
const req = http.request("http://127.0.0.1:45457/shutdown", {
method: 'POST',
headers: {
'content-type': 'application/json',
'origin': 'https://app.httptoolkit.tech',
'authorization': `Bearer ${token}`
}
});
req.on('error', reject);

req.end(JSON.stringify({
operationName: 'Shutdown',
query: 'mutation Shutdown { shutdown }',
variables: {}
}));
req.on('error', (e) => {
console.warn(`Error requesting server shutdown: ${e.message}`);
// This often happens - not totally clear why, but seems likely that in the race to
// shut down, the server doesn't successfully send a response first. If the server
// is not reachable though, it's probably shut down already so we're all good.
resolve();
});
req.end();

req.on('response', (res) => {
if (res.statusCode !== 200) {
Expand All @@ -71,20 +71,14 @@ function softShutdown(token: string) {
res.on('end', () => {
const rawResponseBody = Buffer.concat(responseChunks);
try {
const responseBody = JSON.parse(rawResponseBody.toString('utf8'));
const errors = responseBody.errors as Array<{ message: string, path: string[] }> | undefined;
if (errors?.length) {
console.error(errors);
const errorCount = errors.length > 1 ? `s (${errors.length})` : '';
const responseBodyString = rawResponseBody.toString('utf8');
const responseBody = JSON.parse(responseBodyString);

throw new Error(
`Server error${errorCount} during shutdown: ${errors.map(e =>
`${e.message} at ${e.path.join('.')}`
).join(', ')}`
);
if (responseBody.success) {
resolve();
} else {
throw new Error(`Server shotdown failed: ${responseBodyString}`);
}

resolve();
} catch (e) {
reject(e);
}
Expand Down

0 comments on commit 1787d67

Please sign in to comment.