@@ -67,56 +67,52 @@ export const TemplateInvocation = ({
6767 showCommand,
6868 setShowCommand,
6969} ) => {
70- const intervalRef = useRef ( null ) ;
7170 const templateURL = showTemplateInvocationUrl ( hostID , jobID ) ;
7271 const hostDetailsPageUrl = useForemanHostDetailsPageUrl ( ) ;
7372
7473 const status = useSelector ( selectTemplateInvocationStatus ( hostID ) ) ;
7574 const response = useSelector ( selectTemplateInvocation ( hostID ) ) ;
7675 const dispatch = useDispatch ( ) ;
7776
78- const responseRef = useRef ( response ) ;
79- useEffect ( ( ) => {
80- responseRef . current = response ;
81- } , [ response ] ) ;
77+ const timeoutRef = useRef ( null ) ;
8278
8379 useEffect ( ( ) => {
80+ const scheduleNextPoll = ( ) => {
81+ timeoutRef . current = setTimeout ( ( ) => dispatchFetch ( ) , 5000 ) ;
82+ } ;
83+
8484 const dispatchFetch = ( ) => {
8585 dispatch (
8686 APIActions . get ( {
8787 url : templateURL ,
8888 key : `${ GET_TEMPLATE_INVOCATION } _${ hostID } ` ,
89+ handleSuccess : ( { data } ) => {
90+ const finished = data ?. finished ?? true ;
91+ // eslint-disable-next-line camelcase
92+ const autoRefresh = data ?. auto_refresh || false ;
93+ if ( ! finished && autoRefresh ) {
94+ scheduleNextPoll ( ) ;
95+ }
96+ } ,
97+ handleError : ( ) => {
98+ timeoutRef . current = null ;
99+ } ,
89100 } )
90101 ) ;
91102 } ;
92103
93- if ( intervalRef . current ) {
94- clearInterval ( intervalRef . current ) ;
95- intervalRef . current = null ;
104+ if ( timeoutRef . current ) {
105+ clearTimeout ( timeoutRef . current ) ;
106+ timeoutRef . current = null ;
96107 }
97108
98109 if ( isExpanded ) {
99- if ( isEmpty ( responseRef . current ) ) {
100- dispatchFetch ( ) ;
101- }
102-
103- intervalRef . current = setInterval ( ( ) => {
104- const latestResponse = responseRef . current ;
105- const finished = latestResponse ?. finished ?? true ;
106- // eslint-disable-next-line camelcase
107- const autoRefresh = latestResponse ?. auto_refresh || false ;
108-
109- if ( ! finished && autoRefresh ) {
110- dispatchFetch ( ) ;
111- } else if ( intervalRef . current ) {
112- clearInterval ( intervalRef . current ) ;
113- }
114- } , 5000 ) ;
110+ dispatchFetch ( ) ;
115111 }
116112
117113 return ( ) => {
118- if ( intervalRef . current ) {
119- clearInterval ( intervalRef . current ) ;
114+ if ( timeoutRef . current ) {
115+ clearTimeout ( timeoutRef . current ) ;
120116 }
121117 } ;
122118 } , [ isExpanded , dispatch , templateURL , hostID ] ) ;
0 commit comments