Skip to content

Commit 60789c0

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

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ 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')
17-
const { executeWithRetries } = require('./utils')
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')
1817

1918
class TailLog extends BaseCommand {
2019
async run () {
@@ -39,6 +38,36 @@ class TailLog extends BaseCommand {
3938
}
4039
}
4140

41+
async function executeWithRetries (fn, maxRetries = 5) {
42+
let retries = 0
43+
let startTime = Date.now()
44+
while (retries < maxRetries) {
45+
try {
46+
return await fn()
47+
} catch (error) {
48+
if (error.sdkDetails && error.sdkDetails.status === 403) {
49+
console.log('403 Forbidden error. Attempting to reauthorize...')
50+
} else {
51+
throw error
52+
}
53+
retries++
54+
if (shouldResetRetires(startTime)) {
55+
retries = 0
56+
startTime = Date.now()
57+
}
58+
}
59+
}
60+
}
61+
62+
function shouldResetRetires (startTime, resetInterval = 3600000) {
63+
const elapsedTime = Date.now() - startTime
64+
if (elapsedTime > resetInterval) {
65+
console.log(`Resetting retries after ${resetInterval / 1000} seconds.`)
66+
return true
67+
}
68+
return false
69+
}
70+
4271
TailLog.description = 'outputs a stream of log data for the specified environment, service and log name'
4372

4473
TailLog.args = [

src/commands/cloudmanager/environment/tail-log/utils.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

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/tail-log')
26+
const TailLog = require('./commands/cloudmanager/environment/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: 12 additions & 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/tail-log')
15+
const TailLog = require('../../../src/commands/cloudmanager/environment/tail-log')
1616

1717
beforeEach(() => {
1818
resetCurrentOrgId()
@@ -47,3 +47,14 @@ test('tail-log - config', async () => {
4747
await expect(mockSdk.tailLog.mock.calls.length).toEqual(1)
4848
await expect(mockSdk.tailLog).toHaveBeenCalledWith('5', '17', 'author', 'aemerror', process.stdout)
4949
})
50+
51+
test('tail-log - throw error after 5 attempts for 403 code status', async () => {
52+
setCurrentOrgId('good')
53+
mockSdk.tailLog.mockRejectedValue({ sdkDetails: { status: 403 } })
54+
55+
expect.assertions(1)
56+
57+
const runResult = TailLog.run(['17', 'author', 'aemerror', '--programId', '5'])
58+
await runResult
59+
await expect(mockSdk.tailLog.mock.calls.length).toEqual(5)
60+
})

0 commit comments

Comments
 (0)