fix: cleanup mothership subscription

This commit is contained in:
Alexis Tyler
2020-12-15 00:19:54 +10:30
parent 20a4735959
commit b8ba6654dd
3 changed files with 29 additions and 15 deletions

View File

@@ -183,18 +183,20 @@ class MothershipService {
// Relay socket is closed, close internal one
if (error.message.includes('WebSocket is not open')) {
this.localGraphqlApi?.close();
return;
}
}
});
// Sub to /servers on mothership
this.mothershipServersEndpoint = subscribeToServers(apiKey);
this.mothershipServersEndpoint = await subscribeToServers(apiKey);
});
// Relay is closed
this.relay.on('close', async function (this: WebSocketWithHeartBeat, code, _message) {
try {
mothershipLogger.debug('Connection closed with code %s.', code);
const message = JSON.parse(_message);
mothershipLogger.debug('Connection closed with code=%s reason="%s"', code, message.message);
// Stop ws heartbeat
if (this.pingTimeout) {
@@ -222,7 +224,6 @@ class MothershipService {
if (code === 4429) {
try {
let interval: NodeJS.Timeout | undefined;
const message = JSON.parse(_message);
const retryAfter = parseInt(message['Retry-After'], 10) || 30;
mothershipLogger.debug('Rate limited, retrying after %ss', retryAfter);

View File

@@ -1,35 +1,46 @@
import * as Sentry from '@sentry/node';
import { pubsub } from '../core';
import { SubscriptionClient } from 'graphql-subscriptions-client';
import { MOTHERSHIP_GRAPHQL_LINK } from '../consts';
import { userCache, CachedServers } from '../cache';
import { log } from '../core';
const client = new SubscriptionClient(MOTHERSHIP_GRAPHQL_LINK, {
reconnect: true,
reconnect: false,
lazy: true, // only connect when there is a query
connectionCallback: (error) => {
if (error) {
Sentry.captureException(error);
connectionCallback: (errors) => {
if (errors) {
// Log all errors
errors.forEach((error: Error) => {
log.error(error);
});
}
}
});
export const subscribeToServers = (apiKey: string) => {
return client.request({
client.on('error', (error) => {
log.error('url="%s" message="%s"', MOTHERSHIP_GRAPHQL_LINK, error.message);
}, null);
export const subscribeToServers = async (apiKey: string) => {
const query = client.request({
query: `subscription servers ($apiKey: String!) {
servers @auth(apiKey: $apiKey)
}`,
variables: {
apiKey
}
})
.subscribe({
});
// Subscribe
const subscription = query.subscribe({
next: ({ data, errors }) => {
if (errors) {
// Send all errors to Sentry
// Log all errors
errors.forEach((error: Error) => {
Sentry.captureException(error);
log.error(error);
});
return;
}
// Update internal cache
@@ -43,4 +54,6 @@ export const subscribeToServers = (apiKey: string) => {
});
}
});
return subscription;
};

View File

@@ -53,7 +53,7 @@ export const run = async (channel: string, mutation: string, options: RunOptions
});
// Log result
coreLogger.silly(`run:${moduleToRun.name}`, JSON.stringify(result.json, null, 2));
coreLogger.silly(`run:${moduleToRun.name}`, JSON.stringify(result.json));
// Save result
publish(channel, mutation, result.json);