📯 tRaNsFeR lOgS 📯

This commit is contained in:
Matthew Penner 2020-12-16 16:55:44 -07:00
parent e6c4a68e4a
commit 5c5e2e24f1
12 changed files with 149 additions and 65 deletions

View file

@ -84,8 +84,6 @@ export default () => {
// Sent by the source node whenever the server was archived successfully.
case 'archive':
terminal.writeln(TERMINAL_PRELUDE + 'Server has been archived successfully, attempting connection to target node..\u001b[0m');
// TODO: Get WebSocket information for the target node.
return;
}
};
@ -167,7 +165,7 @@ export default () => {
useEffect(() => {
if (connected && instance) {
terminal.clear();
// terminal.clear();
instance.addListener('status', handlePowerChangeEvent);
instance.addListener('console output', handleConsoleOutput);

View file

@ -0,0 +1,28 @@
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
import { ServerContext } from '@/state/server';
const TransferListener = () => {
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const getServer = ServerContext.useStoreActions(actions => actions.server.getServer);
const setServerFromState = ServerContext.useStoreActions(actions => actions.server.setServerFromState);
// Listen for the installation completion event and then fire off a request to fetch the updated
// server information. This allows the server to automatically become available to the user if they
// just sit on the page.
useWebsocketEvent('transfer status', (status: string) => {
if (status === 'starting') {
setServerFromState(s => ({ ...s, isTransferring: true }));
return;
}
if (status !== 'success') {
return;
}
getServer(uuid).catch(error => console.error(error));
});
return null;
};
export default TransferListener;

View file

@ -15,39 +15,14 @@ const reconnectErrors = [
export default () => {
let updatingToken = false;
const [ error, setError ] = useState<'connecting' | string>('');
const [ transfer, setTransfer ] = useState<boolean>(false);
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
const uuid = ServerContext.useStoreState(state => state.server.data?.uuid);
const setServerStatus = ServerContext.useStoreActions(actions => actions.status.setServerStatus);
const { setInstance, setConnectionState } = ServerContext.useStoreActions(actions => actions.socket);
const updateToken = (uuid: string, socket: Websocket) => {
if (updatingToken) return;
updatingToken = true;
getWebsocketToken(uuid)
.then(data => socket.setToken(data.token, true))
.catch(error => console.error(error))
.then(() => {
updatingToken = false;
});
};
useEffect(() => {
connected && setError('');
}, [ connected ]);
useEffect(() => {
return () => {
instance && instance.close();
};
}, [ instance ]);
useEffect(() => {
// If there is already an instance or there is no server, just exit out of this process
// since we don't need to make a new connection.
if (instance || !uuid) {
return;
}
const connect = (uuid: string, transfer = false) => {
setTransfer(transfer);
const socket = new Websocket();
@ -76,7 +51,34 @@ export default () => {
}
});
getWebsocketToken(uuid)
socket.on('transfer status', (status: string) => {
if (status === 'success') {
setTransfer(false);
return;
}
if (status === 'starting') {
return;
}
// This doesn't use the `setTransfer` hook as it doesn't want to work properly in this context,
// and causes all kinds of fuckery with the websocket.
let transfer = false;
if (status === 'archived') {
transfer = true;
}
// Close the current websocket connection.
socket.close();
setError('connecting');
setConnectionState(false);
setInstance(null);
connect(uuid, transfer);
});
getWebsocketToken(uuid, transfer)
.then(data => {
// Connect and then set the authentication token.
socket.setToken(data.token).connect(data.socket);
@ -85,6 +87,38 @@ export default () => {
setInstance(socket);
})
.catch(error => console.error(error));
};
const updateToken = (uuid: string, socket: Websocket) => {
if (updatingToken) return;
updatingToken = true;
getWebsocketToken(uuid, transfer)
.then(data => socket.setToken(data.token, true))
.catch(error => console.error(error))
.then(() => {
updatingToken = false;
});
};
useEffect(() => {
connected && setError('');
}, [ connected ]);
useEffect(() => {
return () => {
instance && instance.close();
};
}, [ instance ]);
useEffect(() => {
// If there is already an instance or there is no server, just exit out of this process
// since we don't need to make a new connection.
if (instance || !uuid) {
return;
}
connect(uuid);
}, [ uuid ]);
return (

View file

@ -42,7 +42,6 @@ export default () => {
setVisible(false);
})
.catch(error => {
console.log(error);
addError({ key: 'database:create', message: httpErrorToHuman(error) });
setSubmitting(false);
});