Skip to content

Commit 93e1234

Browse files
author
sii27858
committed
fix(auth): tail logs refresh token #702
1 parent 96fed0e commit 93e1234

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

src/commands/cloudmanager/environment/tail-log.js renamed to src/commands/cloudmanager/environment/tail-log/tail-log.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010
governing permissions and limitations under the License.
1111
*/
1212

13-
const BaseCommand = require('../../../base-command')
14-
const { initSdk, getProgramId, sanitizeEnvironmentId } = require('../../../cloudmanager-helpers')
15-
const commonFlags = require('../../../common-flags')
16-
const commonArgs = require('../../../common-args')
13+
const BaseCommand = require('../../../../base-command')
14+
const { initSdk, getProgramId, sanitizeEnvironmentId } = require('../../../../cloudmanager-helpers')
15+
const commonFlags = require('../../../../common-flags')
16+
const commonArgs = require('../../../../common-args')
17+
const { executeWithRetries } = require('./utils')
1718

1819
class TailLog extends BaseCommand {
1920
async run () {
@@ -31,8 +32,10 @@ class TailLog extends BaseCommand {
3132
}
3233

3334
async tailLog (programId, environmentId, service, logName, imsContextName = null) {
34-
const sdk = await initSdk(imsContextName)
35-
return sdk.tailLog(programId, environmentId, service, logName, process.stdout)
35+
return executeWithRetries(async () => {
36+
const sdk = await initSdk(imsContextName)
37+
return sdk.tailLog(programId, environmentId, service, logName, process.stdout)
38+
})
3639
}
3740
}
3841

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
async function executeWithRetries (fn, maxRetries = 5) {
2+
let retries = 0
3+
let startTime = Date.now()
4+
while (retries < maxRetries) {
5+
try {
6+
return await fn()
7+
} catch (error) {
8+
if (error.sdkDetails.status === 403) {
9+
console.log('403 Forbidden error. Attempting to reauthorize...')
10+
} else {
11+
throw error
12+
}
13+
retries++
14+
if (shouldResetRetires(startTime)) {
15+
retries = 0
16+
startTime = Date.now()
17+
}
18+
}
19+
}
20+
}
21+
22+
function shouldResetRetires (startTime, resetInterval = 3600000) {
23+
const elapsedTime = Date.now() - startTime
24+
if (elapsedTime > resetInterval) {
25+
console.log(`Resetting retries after ${resetInterval / 1000} seconds.`)
26+
return true
27+
}
28+
return false
29+
}
30+
31+
module.exports = { executeWithRetries }

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const GetExecutionStepDetails = require('./commands/cloudmanager/execution/get-s
2323
const GetExecutionStepLog = require('./commands/cloudmanager/execution/get-step-log')
2424
const ListAvailableLogOptions = require('./commands/cloudmanager/environment/list-available-log-options')
2525
const DownloadLogs = require('./commands/cloudmanager/environment/download-logs')
26-
const TailLog = require('./commands/cloudmanager/environment/tail-log')
26+
const TailLog = require('./commands/cloudmanager/environment/tail-log/tail-log')
2727
const DeletePipeline = require('./commands/cloudmanager/pipeline/delete')
2828
const UpdatePipeline = require('./commands/cloudmanager/pipeline/update')
2929
const OpenDeveloperConsole = require('./commands/cloudmanager/environment/open-developer-console')

test/commands/environment/tail-log.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ governing permissions and limitations under the License.
1212

1313
const { resetCurrentOrgId, setCurrentOrgId } = require('@adobe/aio-lib-ims')
1414
const { init, mockSdk } = require('@adobe/aio-lib-cloudmanager')
15-
const TailLog = require('../../../src/commands/cloudmanager/environment/tail-log')
15+
const TailLog = require('../../../src/commands/cloudmanager/environment/tail-log/tail-log')
1616

1717
beforeEach(() => {
1818
resetCurrentOrgId()

0 commit comments

Comments
 (0)