Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
events: handle message bus connection failures
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns committed Mar 23, 2022
1 parent ad17580 commit 73c3355
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/dashboard-message-bus/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import axios from "axios";

any.shim();

export class MessageBusConnectionError extends Error {}

/**
* Convert any JS object or value to a base64 representation of it
*/
Expand Down Expand Up @@ -152,7 +154,7 @@ export const getMessageBusPorts = async (
}
}

throw new Error(
throw new MessageBusConnectionError(
`Could not connect to dashboard at http://${dashboardHost}:${dashboardPort}/ports`
);
};
46 changes: 31 additions & 15 deletions packages/events/defaultSubscribers/dashboard/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@ const {
connectToMessageBusWithRetries,
createMessage,
getMessageBusPorts,
sendAndAwait
sendAndAwait,
MessageBusConnectionError
} = require("@truffle/dashboard-message-bus");

module.exports = class DashboardMessageBusClient {
constructor(config) {
this.ready = (async () => {
const dashboard = config.dashboard || {
host: "localhost",
port: 24012
};
this.config = config.dashboard || {
host: "localhost",
port: 24012
};
}

async _getSocket() {
if (this._socket) {
return this._socket;
}

const { publishPort } = await getMessageBusPorts(
this.config.port,
this.config.host
);

const { publishPort } = await getMessageBusPorts(
dashboard.port,
dashboard.host
);
this._socket = await connectToMessageBusWithRetries(
publishPort,
this.config.host
);

return await connectToMessageBusWithRetries(publishPort, dashboard.host);
})();
return this._socket;
}

async sendAndAwait({ type, payload }) {
const socket = await this.ready;
const message = createMessage(type, payload);
try {
const socket = await this._getSocket();
const message = createMessage(type, payload);

return await sendAndAwait(socket, message);
return await sendAndAwait(socket, message);
} catch (err) {
if (!(err instanceof MessageBusConnectionError)) {
throw err;
} else return;
}
}
};

0 comments on commit 73c3355

Please sign in to comment.