Skip to content

Commit

Permalink
fix: simplify runner scripts (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
smrz2001 authored May 14, 2024
1 parent 5f95fae commit 21594f1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 82 deletions.
19 changes: 5 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ EXPOSE 8081

CMD npm run start


FROM base as runner

ENV CAS_PATH=/cas

# For running on AWS ECS
ENV AWS_REGION=${AWS_REGION}
ENV AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
Expand All @@ -38,18 +35,12 @@ ENV AWS_ECS_FAMILY=${AWS_ECS_FAMILY}
ENV DISCORD_WEBHOOK_URL=${DISCORD_WEBHOOK_URL}
ENV CLOUDWATCH_LOG_BASE_URL=${CLOUDWATCH_LOG_BASE_URL}

WORKDIR /runner

COPY runner/package*.json runner/*.js ./

RUN npm install

WORKDIR /

COPY entrypoint.sh .
RUN chmod +x ./entrypoint.sh
WORKDIR /cas

COPY runner.sh .
COPY runner ./runner
COPY runner.sh ./
COPY entrypoint.sh ./
RUN chmod +x ./runner.sh ./entrypoint.sh

ENTRYPOINT ["./entrypoint.sh"]
CMD [ "./runner.sh" ]
6 changes: 3 additions & 3 deletions runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ echo $LOGS

LOGS=($LOGS)
if [[ ${LOGS[0]} == "OK" ]]; then
cd $CAS_PATH && npm run start
npm run start
exit_code=$?
if [[ $exit_code != 0 ]]; then
cd $HOME && cd .. && node ./runner/report-failure.js
node ./runner/report-failure.js
echo "Service exited with non-zero exit code"
else
cd $HOME && cd .. && node ./runner/report-exit.js
node ./runner/report-exit.js
echo "Service exited cleanly"
fi
else
Expand Down
54 changes: 6 additions & 48 deletions runner/helpers.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import child_process from 'child_process'
import * as https from 'https'
import { ECSClient, ListTasksCommand } from '@aws-sdk/client-ecs'

const REPORTING_LEVEL = {
info: 'info',
error: 'error'
error: 'error',
}

const TASK_ID = process.env.INSTANCE_IDENTIFIER

/**
* Sends Discord message about ECS task
* @param {any} messageWithoutFields
*/
function reportTask(messageWithoutFields, reportingLevel = REPORTING_LEVEL.info) {
console.log('Reporting ECS task...')
const taskId = getThisTaskId()
const fields = generateDiscordCloudwatchFields([taskId])
const fields = generateDiscordCloudwatchFields([TASK_ID])
let message = messageWithoutFields
if (fields.length > 0) {
message = [{...messageWithoutFields[0], fields}]
message = [{ ...messageWithoutFields[0], fields }]
}
const data = { embeds: message, username: 'cas-runner' }
const retryDelayMs = 300000 // 300k ms = 5 mins
Expand All @@ -28,18 +27,6 @@ function reportTask(messageWithoutFields, reportingLevel = REPORTING_LEVEL.info)
sendDiscordNotification(webhookUrl, data, retryDelayMs)
}

/**
* Returns the ECS id for the running task
* @returns {string}
*/
function getThisTaskId() {
const taskId = child_process.execSync(
`curl -s "$ECS_CONTAINER_METADATA_URI_V4/task" | /runner/node_modules/node-jq/bin/jq -r ".TaskARN" | awk -F / '{print $NF}'`
).toString()
console.log('TASK ID:', taskId)
return taskId
}

/**
* Returns kv object for Discord fields
* @param {Array<string>} taskIds
Expand All @@ -53,35 +40,6 @@ function generateDiscordCloudwatchFields(taskIds) {
return fields
}

/**
* Returns list of running ECS anchor tasks
* @returns {Array<string>}
*/
async function listECSTasks() {
const client = new ECSClient({
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
})

const params = {
cluster: process.env.AWS_ECS_CLUSTER,
family: process.env.AWS_ECS_FAMILY,
}

const command = new ListTasksCommand(params)

const data = await client.send(command)

if (data.$metadata.httpStatusCode > 399) {
throw Error(data.$metadata.httpStatusCode)
} else {
return data.taskArns
}
}

/**
* Sends a POST to the discord webhookUrl
* @param {string} webhookUrl Discord webhook url
Expand Down Expand Up @@ -109,4 +67,4 @@ function sendDiscordNotification(webhookUrl, data, retryDelayMs = -1) {
req.end()
}

export { REPORTING_LEVEL, generateDiscordCloudwatchFields, getThisTaskId, listECSTasks, reportTask, sendDiscordNotification }
export { REPORTING_LEVEL, generateDiscordCloudwatchFields, reportTask, sendDiscordNotification }
17 changes: 0 additions & 17 deletions runner/package.json

This file was deleted.

0 comments on commit 21594f1

Please sign in to comment.