@@ -7,6 +7,23 @@ const Firebase = require('firebase');
77const rp = require ( 'request-promise' ) ;
88const { CfReceiverService } = require ( '@codefresh-io/cf-receiver' ) ;
99
10+ const { Http } = require ( '../../helpers/http' ) ;
11+
12+ const http = Http ( ) ;
13+
14+ const getStatus = async ( firebaseAuth , progressJobId ) => {
15+ const { url, accessToken : token } = firebaseAuth ;
16+ const userOptions = {
17+ url : `${ url } /${ progressJobId } /status.json?auth=${ token } ` ,
18+ method : 'GET' ,
19+ options : {
20+ timeout : 10000 ,
21+ } ,
22+ } ;
23+ const status = await http ( userOptions ) ;
24+ return status ;
25+ } ;
26+
1027const _connectToFirebase = async ( firebaseAuth ) => new Promise ( ( resolve , reject ) => {
1128 const jobIdRef = new Firebase ( firebaseAuth . url ) ;
1229 jobIdRef . authWithCustomToken ( firebaseAuth . accessToken , ( err ) => {
@@ -72,7 +89,8 @@ function _isBuildFinished(status) {
7289 return [ 'success' , 'error' , 'terminated' ] . includes ( status ) ;
7390}
7491
75- const _printFollowFirebaseLogs = async ( firebaseAuth , progressJobId , removeTimestamps ) => new Promise ( ( resolve , reject ) => {
92+ // eslint-disable-next-line max-len
93+ const _printFollowFirebaseLogs = async ( firebaseAuth , progressJobId , removeTimestamps , workflowId , sdk ) => new Promise ( ( resolve , reject ) => {
7694 const jobIdRef = new Firebase ( `${ _getFinalFirebaseUrl ( firebaseAuth ) } /${ progressJobId } ` ) ;
7795
7896 const errorCallback = ( err ) => {
@@ -82,12 +100,35 @@ const _printFollowFirebaseLogs = async (firebaseAuth, progressJobId, removeTimes
82100 } ) ) ;
83101 } ;
84102
103+ let interval ;
104+
105+ const statusHandler = ( status ) => {
106+ if ( _isBuildFinished ( status ) ) {
107+ clearInterval ( interval ) ;
108+ resolve ( ) ;
109+ }
110+ } ;
111+
112+ interval = setInterval ( async ( ) => {
113+ try {
114+ const status = await getStatus ( firebaseAuth , progressJobId ) ;
115+ statusHandler ( status ) ;
116+ } catch ( err ) {
117+ process . stdout . write ( `Failed to get status from firebase: ${ err } ` ) ;
118+ try {
119+ await _connectToFirebase ( firebaseAuth ) ;
120+ } catch ( error ) {
121+ const errMsg = `Failed to connect to firebase: ${ error } ` ;
122+ process . stdout . write ( errMsg ) ;
123+ }
124+ const workflow = await sdk . workflows . getBuild ( { buildId : workflowId , noAccount : false , firebaseFallback : 'true' } ) ;
125+ statusHandler ( workflow . status ) ;
126+ }
127+ } , 10000 ) ;
128+
85129 jobIdRef . child ( 'status' )
86130 . on ( 'value' , ( snapshot ) => {
87- const status = snapshot . val ( ) ;
88- if ( _isBuildFinished ( status ) ) {
89- resolve ( ) ;
90- }
131+ statusHandler ( snapshot . val ( ) ) ;
91132 } , errorCallback ) ;
92133
93134 jobIdRef . child ( 'steps' )
@@ -103,6 +144,7 @@ const _printFollowFirebaseLogs = async (firebaseAuth, progressJobId, removeTimes
103144 step . ref . child ( 'logs' )
104145 . on ( 'child_added' , ( snapshot ) => { // eslint-disable-line
105146 let log = snapshot . val ( ) ;
147+
106148 if ( removeTimestamps ) {
107149 log = _stripTimestamps ( log ) ;
108150 }
@@ -111,11 +153,11 @@ const _printFollowFirebaseLogs = async (firebaseAuth, progressJobId, removeTimes
111153 } , errorCallback ) ;
112154} ) ;
113155
114- const showWorkflowLogsFromFirebase = async ( progressJobId , follow , removeTimestamps , sdk ) => {
156+ const showWorkflowLogsFromFirebase = async ( progressJobId , follow , removeTimestamps , workflowId , sdk ) => {
115157 const firebaseAuth = await sdk . firebase . getToken ( { progressId : progressJobId } ) ;
116158 await _connectToFirebase ( firebaseAuth ) ;
117159 if ( follow ) {
118- await _printFollowFirebaseLogs ( firebaseAuth , progressJobId , removeTimestamps ) ;
160+ await _printFollowFirebaseLogs ( firebaseAuth , progressJobId , removeTimestamps , workflowId , sdk ) ;
119161 } else {
120162 await _printCurrentFirebaseLogs ( firebaseAuth , progressJobId , removeTimestamps ) ;
121163 }
0 commit comments