Skip to content

Commit

Permalink
do not immediatly fail if instance goes into an error state (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhudson authored Mar 13, 2024
1 parent c956d6e commit 7131024
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "database-branching-action",
"version": "1.0.0",
"version": "1.0.1",
"description": "Tembo database branching action",
"main": "lib/main.js",
"scripts": {
Expand Down
58 changes: 43 additions & 15 deletions src/getInstanceStatus.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import * as core from '@actions/core'
import axios from 'axios'
import {getInstanceStatus} from './getInstanceStatus'

jest.mock('axios')
Expand Down Expand Up @@ -47,21 +47,49 @@ it('successfully retrieves instance status when instance becomes Up', async () =
expect(mockedCore.info).toHaveBeenCalledWith('Instance is up and running.')
})

it('throws an error when instance encounters an "Error" state', async () => {
mockedAxios.get.mockResolvedValue({
data: {state: 'Error'}
})
it('retries when instance encounters an "Error" state and eventually becomes Up', async () => {
mockedAxios.get
.mockResolvedValueOnce({
data: {state: 'Error'}
})
.mockResolvedValueOnce({
data: {state: 'Configuring'}
})
.mockResolvedValueOnce({
data: {
state: 'Up',
instance_id: '123',
instance_name: 'testInstance',
connection_info: {
host: 'localhost',
port: 5432,
user: 'admin',
password: 'password'
}
}
})

await expect(
getInstanceStatus(
'https://api.tembo.io',
'org123',
'123',
'token789',
1000,
2
)
).rejects.toThrow('Instance encountered an error.')
const status = await getInstanceStatus(
'https://api.tembo.io',
'org123',
'123',
'token789',
1000,
3
)

expect(status).toEqual({
instance_id: '123',
instance_name: 'testInstance',
host: 'localhost',
port: '5432',
user: 'admin',
password: 'password'
})
expect(mockedCore.info).toHaveBeenCalledWith('Instance is up and running.')
expect(mockedCore.warning).toHaveBeenCalledWith(
'Instance encountered an error. Retrying...'
)
})

it('throws an error when max attempts are exceeded without reaching "Up" state', async () => {
Expand Down
10 changes: 4 additions & 6 deletions src/getInstanceStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// getInstanceStatus.ts
import axios from 'axios'
import * as core from '@actions/core'
import axios from 'axios'

interface GetInstanceStatusParams {
instance_id: string
Expand All @@ -25,7 +24,7 @@ export async function getInstanceStatus(
let attempts = 0

while (
(state === 'Submitted' || state === 'Configuring') &&
(state === 'Submitted' || state === 'Configuring' || state === 'Error') &&
attempts < maxAttempts
) {
try {
Expand All @@ -52,7 +51,7 @@ export async function getInstanceStatus(
password: connection_info?.password
}
} else if (state === 'Error') {
throw new Error('Instance encountered an error.')
core.warning('Instance encountered an error. Retrying...')
} else {
await new Promise(resolve => setTimeout(resolve, pollInterval))
}
Expand All @@ -63,8 +62,7 @@ export async function getInstanceStatus(
} else {
errorMessage = `Failed to check instance status: ${error instanceof Error ? error.message : 'An unknown error occurred.'}`
}
core.setFailed(errorMessage)
throw new Error(errorMessage)
core.warning(errorMessage)
}

attempts++
Expand Down

0 comments on commit 7131024

Please sign in to comment.