Stop and limit fetch process logs

This commit is contained in:
Anthony Laibe 2018-10-08 10:53:30 +01:00 committed by Pascal Precht
parent ca55a8091e
commit 0e4248cca8
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
5 changed files with 60 additions and 29 deletions

View File

@ -256,6 +256,7 @@ export const gasOracle = {
// Web Socket // Web Socket
export const WATCH_NEW_PROCESS_LOGS = 'WATCH_NEW_PROCESS_LOGS'; export const WATCH_NEW_PROCESS_LOGS = 'WATCH_NEW_PROCESS_LOGS';
export const STOP_NEW_PROCESS_LOGS = 'STOP_NEW_PROCESS_LOGS';
export const WATCH_NEW_CONTRACT_LOGS = 'WATCH_NEW_CONTRACT_LOGS'; export const WATCH_NEW_CONTRACT_LOGS = 'WATCH_NEW_CONTRACT_LOGS';
export const INIT_BLOCK_HEADER = 'INIT_BLOCK_HEADER'; export const INIT_BLOCK_HEADER = 'INIT_BLOCK_HEADER';
export const STOP_BLOCK_HEADER = 'STOP_BLOCK_HEADER'; export const STOP_BLOCK_HEADER = 'STOP_BLOCK_HEADER';
@ -269,6 +270,13 @@ export function listenToProcessLogs(processName) {
}; };
} }
export function stopProcessLogs(processName) {
return {
type: STOP_NEW_PROCESS_LOGS,
processName
};
}
export function listenToContractLogs() { export function listenToContractLogs() {
return { return {
type: WATCH_NEW_CONTRACT_LOGS type: WATCH_NEW_CONTRACT_LOGS

View File

@ -17,10 +17,7 @@ CommandResult.propTypes = {
class Console extends Component { class Console extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {value: ''};
value: '',
selectedProcess: 'Embark'
};
} }
handleSubmit(event) { handleSubmit(event) {
@ -43,7 +40,7 @@ class Console extends Component {
</Logs> </Logs>
</Tab>) </Tab>)
].concat(processes.map(process => ( ].concat(processes.map(process => (
<Tab title={process.name} key={process.name} onClick={(e, x) => this.clickTab(e, x)}> <Tab title={process.name} key={process.name}>
<Logs> <Logs>
{ {
processLogs.filter((item) => item.name === process.name) processLogs.filter((item) => item.name === process.name)
@ -57,7 +54,7 @@ class Console extends Component {
render() { render() {
const tabs = this.renderTabs(); const tabs = this.renderTabs();
const {selectedProcess, value} = this.state; const {value} = this.state;
return ( return (
<Grid.Row cards className="console"> <Grid.Row cards className="console">
@ -66,17 +63,17 @@ class Console extends Component {
<Card.Body className="console-container"> <Card.Body className="console-container">
<React.Fragment> <React.Fragment>
<TabbedHeader <TabbedHeader
selectedTitle={selectedProcess} selectedTitle={this.props.activeProcess}
stateCallback={newProcess => this.setState({selectedProcess: newProcess})} stateCallback={this.props.updateTab}
> >
{tabs} {tabs}
</TabbedHeader> </TabbedHeader>
<TabbedContainer selectedTitle={selectedProcess}> <TabbedContainer selectedTitle={this.props.activeProcess}>
{tabs} {tabs}
</TabbedContainer> </TabbedContainer>
</React.Fragment> </React.Fragment>
</Card.Body> </Card.Body>
{selectedProcess === 'Embark' && <Card.Footer> {this.props.isEmbark() && <Card.Footer>
<form onSubmit={(event) => this.handleSubmit(event)} autoComplete="off"> <form onSubmit={(event) => this.handleSubmit(event)} autoComplete="off">
<Form.Input value={value} <Form.Input value={value}
onChange={(event) => this.handleChange(event)} onChange={(event) => this.handleChange(event)}
@ -93,9 +90,11 @@ class Console extends Component {
Console.propTypes = { Console.propTypes = {
postCommand: PropTypes.func, postCommand: PropTypes.func,
isEmbark: PropTypes.func,
commands: PropTypes.arrayOf(PropTypes.object).isRequired, commands: PropTypes.arrayOf(PropTypes.object).isRequired,
processes: PropTypes.arrayOf(PropTypes.object).isRequired, processes: PropTypes.arrayOf(PropTypes.object).isRequired,
processLogs: PropTypes.arrayOf(PropTypes.object).isRequired processLogs: PropTypes.arrayOf(PropTypes.object).isRequired,
updateTab: PropTypes.func
}; };
export default Console; export default Console;

View File

@ -3,31 +3,39 @@ import React, {Component} from 'react';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {Page} from "tabler-react"; import {Page} from "tabler-react";
import {commands as commandsAction, listenToProcessLogs, processLogs as processLogsAction} from "../actions"; import {commands as commandsAction, listenToProcessLogs, processLogs as processLogsAction, stopProcessLogs} from "../actions";
import DataWrapper from "../components/DataWrapper"; import DataWrapper from "../components/DataWrapper";
import Processes from '../components/Processes'; import Processes from '../components/Processes';
import Console from '../components/Console'; import Console from '../components/Console';
import {getProcesses, getCommands, getProcessLogs} from "../reducers/selectors"; import {getProcesses, getCommands, getProcessLogs} from "../reducers/selectors";
import deepEqual from 'deep-equal';
const EMBARK_PROCESS_NAME = 'Embark';
class HomeContainer extends Component { class HomeContainer extends Component {
constructor(props) {
super(props);
this.state = { activeProcess: EMBARK_PROCESS_NAME };
}
componentDidMount() { componentDidMount() {
this.getProcesses(); this.updateTab();
} }
componentDidUpdate(prevProps) { isEmbark() {
if (!deepEqual(this.props.processes, prevProps.processes)) { return this.state.activeProcess === EMBARK_PROCESS_NAME
this.getProcesses(prevProps); }
updateTab(processName = EMBARK_PROCESS_NAME) {
if (!this.isEmbark()){
this.props.stopProcessLogs(this.state.activeProcess)
} }
}
getProcesses(prevProps) { if (processName !== EMBARK_PROCESS_NAME) {
this.props.processes.forEach(process => { this.props.fetchProcessLogs(processName);
this.props.fetchProcessLogs(process.name); this.props.listenToProcessLogs(processName);
if (!prevProps || !prevProps.processes.length) { }
this.props.listenToProcessLogs(process.name);
} this.setState({activeProcess: processName});
});
} }
render() { render() {
@ -39,7 +47,13 @@ class HomeContainer extends Component {
)} /> )} />
<DataWrapper shouldRender={this.props.processes.length > 0 } {...this.props} render={({processes, postCommand, processLogs}) => ( <DataWrapper shouldRender={this.props.processes.length > 0 } {...this.props} render={({processes, postCommand, processLogs}) => (
<Console postCommand={postCommand} commands={this.props.commands} processes={processes} processLogs={processLogs} /> <Console activeProcess={this.state.activeProcess}
postCommand={postCommand}
commands={this.props.commands}
processes={processes}
processLogs={processLogs}
isEmbark={() => this.isEmbark}
updateTab={processName => this.updateTab(processName)} />
)} /> )} />
</React.Fragment> </React.Fragment>
); );
@ -69,6 +83,7 @@ export default connect(
{ {
postCommand: commandsAction.post, postCommand: commandsAction.post,
fetchProcessLogs: processLogsAction.request, fetchProcessLogs: processLogsAction.request,
listenToProcessLogs listenToProcessLogs,
stopProcessLogs
} }
)(HomeContainer); )(HomeContainer);

View File

@ -231,7 +231,16 @@ export function *listenToProcessLogs(action) {
const socket = api.webSocketProcess(credentials, action.processName); const socket = api.webSocketProcess(credentials, action.processName);
const channel = yield call(createChannel, socket); const channel = yield call(createChannel, socket);
while (true) { while (true) {
const processLog = yield take(channel); const { cancel, processLog } = yield race({
processLog: take(channel),
cancel: take(actions.STOP_NEW_PROCESS_LOGS)
});
if (cancel && action.processName === cancel.processName) {
channel.close();
return;
}
yield put(actions.processLogs.success([processLog])); yield put(actions.processLogs.success([processLog]));
} }
} }

View File

@ -85,7 +85,7 @@ class ProcessLauncher {
'get', 'get',
apiRoute, apiRoute,
(req, res) => { (req, res) => {
res.send(JSON.stringify(self.logs)); res.send(JSON.stringify(self.logs.slice(-50)));
} }
); );
} }