Skip to content

Commit a338d11

Browse files
fqueirugatimja
andauthored
Adds polling to have live updates (#14)
Co-authored-by: Tim Jacomb <[email protected]>
1 parent d0c0744 commit a338d11

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

src/main/frontend/pipeline-graph/main/PipelineGraph.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22

33
import { nodeStrokeWidth, getGroupForResult } from './support/StatusIcons';
44
import { TruncatingLabel } from './support/TruncatingLabel';
5+
import startPollingPipelineStatus from './support/startPollingPipelineStatus';
56

67
import {
78
CompositeConnection,
@@ -63,16 +64,21 @@ export class PipelineGraph extends React.Component {
6364
}
6465

6566
componentDidMount() {
66-
fetch('graph')
67-
.then(res => res.json())
68-
.then((result) => {
69-
this.setState({
70-
stages: result.data.stages
71-
})
72-
this.stagesUpdated(result.data.stages); // TODO this doesn't seem right
73-
})
74-
.catch(console.log)
67+
const onPipelineDataReceived = (data: { stages: Array<StageInfo> }) => {
68+
const { stages } = data;
69+
this.setState({ stages });
70+
this.stagesUpdated(stages)
71+
};
72+
const onPollingError = (err: Error) => {
73+
console.log('There was an error when polling the pipeline status', err)
74+
};
75+
const onPipelineComplete = () => undefined
7576

77+
startPollingPipelineStatus(
78+
onPipelineDataReceived,
79+
onPollingError,
80+
onPipelineComplete
81+
)
7682
}
7783

7884
componentWillReceiveProps(nextProps: Props) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { StageInfo } from '../PipelineGraphModel'
2+
3+
interface ApiResult {
4+
complete: boolean;
5+
stages: Array<StageInfo>;
6+
}
7+
8+
/**
9+
* Starts polling the server to retrieve pipeline status.
10+
* Will only stop once the run is finished.
11+
*/
12+
export default function startPollingPipelineStatus(
13+
onFetchSuccess: (data: ApiResult) => void,
14+
onFetchError: (err: Error) => void,
15+
onPipelineComplete: () => void,
16+
interval = 3000
17+
) {
18+
let isComplete = false;
19+
20+
async function fetchPipelineData() {
21+
try {
22+
const res = await fetch('graph')
23+
const result = await res.json()
24+
onFetchSuccess(result.data);
25+
isComplete = result.data.complete;
26+
} catch (err) {
27+
// TODO: implement exponential backoff of the timeout interval
28+
onFetchError(err)
29+
}
30+
finally {
31+
if (isComplete) {
32+
onPipelineComplete()
33+
} else {
34+
setTimeout(() => fetchPipelineData(), interval)
35+
}
36+
}
37+
}
38+
fetchPipelineData()
39+
}

src/main/java/io/jenkins/plugins/pipelinegraphview/PipelineGraph.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
public class PipelineGraph {
66

77
private List<PipelineStage> stages;
8+
private boolean complete = false;
89

9-
public PipelineGraph(List<PipelineStage> stages) {
10+
public PipelineGraph(List<PipelineStage> stages, boolean complete) {
1011
this.stages = stages;
12+
this.complete = complete;
1113
}
1214

15+
public boolean isComplete() {
16+
return complete;
17+
}
1318
public List<PipelineStage> getStages() {
1419
return stages;
1520
}

src/main/java/io/jenkins/plugins/pipelinegraphview/PipelineGraphApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public PipelineGraph createGraph() {
105105
})
106106
.filter(stage -> !stagesThatAreChildrenOrNestedStages.contains(stage.getId())).collect(Collectors.toList());
107107

108-
return new PipelineGraph(stageResults);
108+
return new PipelineGraph(stageResults, run.getExecution().isComplete());
109109
}
110110

111111
private Function<Integer, PipelineStage> mapper(Map<Integer, PipelineStageInternal> stageMap, Map<Integer, List<Integer>> stageToChildrenMap) {

0 commit comments

Comments
 (0)