Skip to content

Commit

Permalink
WIP: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
soonum committed Mar 11, 2024
1 parent 3799ba0 commit ca173ff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 62 deletions.
54 changes: 23 additions & 31 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const config = require('./config')
const core = require('@actions/core')
const { waitForRunnerRegistered } = require('./gh')

function setOutput(label, instanceId, region) {
function setOutput(label) {
core.setOutput('label', label)
// core.setOutput('instance-id', instanceId)
}

async function start() {
Expand All @@ -15,11 +14,11 @@ async function start() {
'start'
)

// TODO LOG the instance ID instead of providing it as output.
setOutput(
start_instance_response.runner_name
// wait_instance_response.instance_id
)
const provider = config.input.backend
const instance_id = wait_instance_response.start.instance_id
core.info(`${provider} instance started with ID: ${instance_id}`)

setOutput(start_instance_response.runner_name)

await waitForRunnerRegistered(start_instance_response.runner_name)
}
Expand All @@ -29,6 +28,8 @@ async function stop() {
config.input.label
)
await slab.waitForInstance(stop_instance_response.task_id, 'stop')

core.info('Instance sucessfully terminated')
}

async function run() {
Expand Down
39 changes: 15 additions & 24 deletions src/slab.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ function getSignature(content) {

async function startInstanceRequest() {
const url = config.input.slabUrl
const provider = config.input.backend

const details = {
backend: {
provider: config.input.backend,
provider,
profile: config.input.profile
}
}
Expand All @@ -28,8 +29,6 @@ async function startInstanceRequest() {
const body = JSON.stringify(payload)
const signature = getSignature(body)

const provider = config.input.provider

try {
core.info(`Request ${provider} instance start`)

Expand Down Expand Up @@ -59,8 +58,6 @@ async function startInstanceRequest() {
}

async function waitForInstance(taskId, taskName) {
const provider = config.input.provider

for (let i = 0; i < 30; i++) {
await utils.sleep(15)

Expand All @@ -75,17 +72,17 @@ async function waitForInstance(taskId, taskName) {
}
} else {
core.error(
`Failed to wait for ${provider} instance (HTTP status code: ${response.status})`
`Failed to wait for instance (HTTP status code: ${response.status})`
)
}
} catch (error) {
core.error('Failed to fetch or remove ${provider} instance task')
core.error('Failed to fetch or remove instance task')
throw error
}
}

core.setFailed(
'Timeout while waiting for ${provider} instance to be running after 15 mins.'
'Timeout while waiting for instance to be running after 15 mins.'
)
}

Expand All @@ -102,10 +99,8 @@ async function terminateInstanceRequest(runnerName) {
const body = JSON.stringify(payload)
const signature = getSignature(body)

const provider = config.input.provider

try {
core.info(`Request ${provider} instance termination`)
core.info(`Request instance termination (runner: ${runnerName})`)

const response = await fetch(url.concat('/job'), {
method: 'POST',
Expand All @@ -119,44 +114,40 @@ async function terminateInstanceRequest(runnerName) {
})

if (response.ok) {
core.info(`${provider} instance termination successfully requested`)
core.info('Instance termination successfully requested')
return response.json()
} else {
core.setFailed(
`${provider} instance termination request has failed (HTTP status code: ${response.status})`
`Instance termination request has failed (HTTP status code: ${response.status})`
)
}
} catch (error) {
core.error(`${provider} instance termination request has failed`)
core.error('Instance termination request has failed')
throw error
}
}

async function getTask(taskId) {
const provider = config.input.provider

try {
const url = config.input.slabUrl
const route = `task_status/${config.githubContext.repo}/${taskId}`

const response = await fetch(url.concat(route))
if (response.ok) {
core.debug(`${provider} instance task successfully fetched`)
core.debug('Instance task successfully fetched')
return response
} else {
core.setFailed(
`${provider} instance task request has failed (HTTP status code: ${response.status})`
`Instance task status request has failed (ID: ${taskId}, HTTP status code: ${response.status})`
)
}
} catch (error) {
core.error(`Failed to fetch ${provider} task status with ID: ${taskId}`)
core.error(`Failed to fetch task status with ID: ${taskId}`)
throw error
}
}

async function removeTask(taskId) {
const provider = config.input.provider

try {
const url = config.input.slabUrl
const route = `task_delete/${config.githubContext.repo}/${taskId}`
Expand All @@ -165,15 +156,15 @@ async function removeTask(taskId) {
method: 'DELETE'
})
if (response.ok) {
core.debug(`${provider} instance task successfully removed`)
core.debug('Instance task successfully removed')
return response
} else {
core.setFailed(
`${provider} instance task removal has failed (HTTP status code: ${response.status})`
`Instance task status removal has failed (ID: ${taskId}, HTTP status code: ${response.status})`
)
}
} catch (error) {
core.error(`Failed to remove ${provider} task status with ID: ${taskId}`)
core.error(`Failed to remove task status with ID: ${taskId}`)
throw error
}
}
Expand Down

0 comments on commit ca173ff

Please sign in to comment.