Skip to content

Commit

Permalink
tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
kswanson33 committed Dec 16, 2024
1 parent a62dd28 commit 1b7e5af
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
19 changes: 2 additions & 17 deletions src/lib/run-studies.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'
import { runStudies } from './run-studies'
// import { managementAppGetRunnableStudiesRequest } from './utils'
import * as api from './api'
import * as aws from './aws'
import {
GetResourcesCommand,
GetResourcesCommandInput,
GetResourcesCommandOutput,
ResourceGroupsTaggingAPIClient,
} from '@aws-sdk/client-resource-groups-tagging-api'
import { AwsStub, mockClient } from 'aws-sdk-client-mock'
import {
DeleteTaskDefinitionsCommand,
DescribeTaskDefinitionCommand,
ECSClient,
RegisterTaskDefinitionCommand,
RunTaskCommand,
} from '@aws-sdk/client-ecs'
import { GetResourcesCommandOutput } from '@aws-sdk/client-resource-groups-tagging-api'
import { RUN_ID_TAG_KEY } from './aws'
import { ManagementAppGetRunnableStudiesResponse, TOAGetRunsResponse } from './types'

Expand Down Expand Up @@ -44,7 +30,6 @@ describe('runStudies()', () => {
const runId2 = 'to-be-run-2'
const runId_toGarbageCollect = 'run-finished'

// process.env.BASE_TASK_DEFINITION_FAMILY = "BASE_TASK_DEF_FAMILY"
process.env.ECS_CLUSTER = 'MOCK_ECS_CLUSTER'
process.env.BASE_TASK_DEFINITION_FAMILY = 'MOCK_BASE_TASK_DEF_FAMILY'
process.env.VPC_SUBNETS = 'MOCK_VPC_SUBNETS,SUBNET'
Expand Down Expand Up @@ -95,7 +80,7 @@ describe('runStudies()', () => {
ResourceARN: runId_toGarbageCollect,
Tags: [{ Key: RUN_ID_TAG_KEY, Value: runId_toGarbageCollect }],
},
], // Todo: runInAWS and run to be gc'd
],
})

// Mock getECSTaskDefinition
Expand Down
11 changes: 7 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { ManagementAppGetRunnableStudiesResponse, TOAGetRunsResponse } from './t
import { ResourceTagMapping } from '@aws-sdk/client-resource-groups-tagging-api'
import { RUN_ID_TAG_KEY } from './aws'

const getRunIdFromResourceTagMapping = (resource: ResourceTagMapping): string | undefined => {
return resource.Tags?.find((tag) => tag.Key === RUN_ID_TAG_KEY)?.Value
}

export const filterManagementAppRuns = (
managementAppResponse: ManagementAppGetRunnableStudiesResponse,
toaResponse: TOAGetRunsResponse,
existingAwsRuns: string[],
): ManagementAppGetRunnableStudiesResponse => {
// TODO: also filter out in-progress runs
const runIdArray: string[] = toaResponse.runs.map((run) => run.runId) // flatten runsInTOA
const toaRunIdArray: string[] = toaResponse.runs.map((run) => run.runId) // flatten toaResponse

return {
runs: managementAppResponse.runs.filter((run) => {
return !runIdArray.includes(run.runId) && !existingAwsRuns.includes(run.runId)
return !toaRunIdArray.includes(run.runId) && !existingAwsRuns.includes(run.runId)
}),
}
}
Expand All @@ -27,7 +30,7 @@ export const filterOrphanTaskDefinitions = (
// If there are task definitions with run IDs that were not returned by
// the BMA, they must be for orphan tasks that were already run.
taskDefinitionResources.forEach((item) => {
const runId = item.Tags?.find((tag) => tag.Key === RUN_ID_TAG_KEY)?.Value
const runId = getRunIdFromResourceTagMapping(item)
if (runId !== undefined && !managementAppRunIds.includes(runId) && item.ResourceARN !== undefined) {
orphanTaskDefinitions.push(item.ResourceARN)
}
Expand Down

0 comments on commit 1b7e5af

Please sign in to comment.