Skip to content

Commit 10f1cc2

Browse files
Merge pull request #107 from codefresh-io/moshe_levi
Handle firebase disconnect
2 parents af11cf7 + 72eedf9 commit 10f1cc2

3 files changed

Lines changed: 51 additions & 9 deletions

File tree

lib/logic/logs.helper.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ const Firebase = require('firebase');
77
const rp = require('request-promise');
88
const { 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+
1027
const _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
}

lib/logic/logs.logic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Logs extends Resource {
88
const workflow = await this.sdk.workflows.getBuild({ buildId: workflowId, noAccount: false });
99
const progressJob = await this.sdk.progress.get({ id: workflow.progress });
1010
if (progressJob.location.type === 'firebase') {
11-
await showWorkflowLogsFromFirebase(progressJob._id, follow, removeTimetamps, this.sdk);
11+
await showWorkflowLogsFromFirebase(progressJob._id, follow, removeTimetamps, workflowId, this.sdk);
1212
} else if (progressJob.location.type === 'composition') {
1313
await showWorkflowLogsByWebsocket(progressJob._id, progressJob.account, follow, this.sdk);
1414
} else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh-sdk",
3-
"version": "1.11.2",
3+
"version": "1.11.3",
44
"description": "Codefresh_api_swagger_3_0_specification",
55
"main": "index.js",
66
"author": {

0 commit comments

Comments
 (0)