diff --git a/app/mothership/index.ts b/app/mothership/index.ts index 367c50b32..3804dedf6 100644 --- a/app/mothership/index.ts +++ b/app/mothership/index.ts @@ -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); diff --git a/app/mothership/subscribe-to-servers.ts b/app/mothership/subscribe-to-servers.ts index a9719953b..83ea131e4 100644 --- a/app/mothership/subscribe-to-servers.ts +++ b/app/mothership/subscribe-to-servers.ts @@ -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; }; diff --git a/app/run.ts b/app/run.ts index 71282fa41..0749cb1d5 100644 --- a/app/run.ts +++ b/app/run.ts @@ -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);