fix: reconnect to mothership's subcription endpoint on key change

This commit is contained in:
Alexis Tyler
2021-06-01 17:21:34 +09:30
parent 2048d61a27
commit d537c6ee8b
2 changed files with 36 additions and 6 deletions

View File

@@ -8,6 +8,9 @@ import { coreLogger } from '../log';
import { paths } from '../paths';
import * as Sentry from '@sentry/node';
import { loadState } from '../utils';
import { mothership } from '../../mothership/subscribe-to-servers';
import { apiManager } from '../api-manager';
import { MessageTypes } from 'subscriptions-transport-ws';
export const myservers = () => {
const watchers: chokidar.FSWatcher[] = [];
@@ -37,10 +40,37 @@ export const myservers = () => {
// missing it's likely we shipped without Sentry
// initialised. This would be done for a reason!
if (sentryClient) {
sentryClient.getOptions().enabled = isEnabled;
// Check if the value changed
if (sentryClient.getOptions().enabled !== isEnabled) {
sentryClient.getOptions().enabled = isEnabled;
// Log for debugging
coreLogger.debug('%s crash reporting!', isEnabled ? 'Enabled' : 'Disabled');
// Log for debugging
coreLogger.debug('%s crash reporting!', isEnabled ? 'Enabled' : 'Disabled');
}
}
// If we have no my_servers key disconnect from mothership's subscription endpoint
if (apiManager.getValidKeys().filter(key => key.name === 'my_servers').length === 0) {
// Disconnect forcefully from mothership so we ensure it doesn't reconnect automatically
mothership.close(true, true);
}
// If we have a my_servers key reconnect to mothership
if (apiManager.getValidKeys().filter(key => key.name === 'my_servers').length === 1) {
// Reconnect to mothership
mothership.connect();
// Reregister all subscriptions
// @ts-expect-error
Object.keys(mothership.operations).forEach(id => {
mothership.sendMessage(
id,
// @ts-expect-error
MessageTypes.GQL_START,
// @ts-expect-error
mothership.operations[id].options
);
});
}
});

View File

@@ -4,8 +4,8 @@ import { MOTHERSHIP_GRAPHQL_LINK, ONE_SECOND } from '../consts';
import { userCache, CachedServers } from '../cache';
const log = logger.createChild({ prefix: 'subscribe-to-servers' });
const client = new SubscriptionClient(MOTHERSHIP_GRAPHQL_LINK, {
reconnect: true,
export const mothership = new SubscriptionClient(MOTHERSHIP_GRAPHQL_LINK, {
reconnect: false,
lazy: true,
minTimeout: ONE_SECOND * 30,
connectionCallback: errors => {
@@ -23,7 +23,7 @@ const client = new SubscriptionClient(MOTHERSHIP_GRAPHQL_LINK, {
export const subscribeToServers = async (apiKey: string) => {
log.silly('Subscribing to servers with %s', apiKey);
const query = client.request({
const query = mothership.request({
query: `subscription servers ($apiKey: String!) {
servers @auth(apiKey: $apiKey)
}`,