Skip to content

Commit

Permalink
feat(compute resources): suggest recommended limits to CPU and Memory…
Browse files Browse the repository at this point in the history
… for a Function
  • Loading branch information
iainsproat committed Nov 6, 2023
1 parent 2cf7d93 commit b637bae
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
36 changes: 27 additions & 9 deletions dist/action/index.js

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

2 changes: 1 addition & 1 deletion dist/action/index.js.map

Large diffs are not rendered by default.

46 changes: 37 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@ import { readFileSync } from 'node:fs'
import { join } from 'node:path'

const InputVariablesSchema = z.object({
speckleAutomateUrl: z.string().url().nonempty(),
speckleToken: z.string().nonempty(),
speckleFunctionId: z.string().nonempty(),
speckleAutomateUrl: z.string().url().min(1),
speckleToken: z.string().min(1),
speckleFunctionId: z.string().min(1),
speckleFunctionInputSchema: z.record(z.string().nonempty(), z.unknown()).nullable(),
speckleFunctionCommand: z.string().nonempty().array(),
speckleFunctionReleaseTag: z.string().max(10).nonempty()
speckleFunctionCommand: z.string().min(1).array(),
speckleFunctionReleaseTag: z.string().max(10).min(1),
speckleFunctionRecommendedCPUm: z
.number()
.int()
.finite()
.gte(100)
.lte(16000)
.optional(),
speckleFunctionRecommendedMemoryMi: z
.number()
.int()
.finite()
.gte(1)
.lte(8000)
.optional()
})

type InputVariables = z.infer<typeof InputVariablesSchema>
Expand Down Expand Up @@ -40,7 +54,15 @@ const parseInputs = (): InputVariables => {
.split(' '),
speckleFunctionReleaseTag: core.getInput('speckle_function_release_tag', {
required: true
})
}),
speckleFunctionRecommendedCPUm: parseInt(
core.getInput('speckle_function_recommended_cpu_m', {
required: false
})
),
speckleFunctionRecommendedMemoryMi: parseInt(
core.getInput('speckle_function_recommended_memory_mi', { required: false })
)
}
const inputParseResult = InputVariablesSchema.safeParse(rawInputs)
if (inputParseResult.success) return inputParseResult.data
Expand All @@ -66,6 +88,8 @@ type FunctionVersionRequestBody = {
versionTag: string
command: string[]
inputSchema: Record<string, unknown> | null
recommendedCPUm?: number
recommendedMemoryMi?: number
}

const FunctionVersionResponseBodySchema = z.object({
Expand All @@ -81,7 +105,9 @@ const registerNewVersionForTheSpeckleAutomateFunction = async (
speckleFunctionId,
speckleFunctionInputSchema,
speckleToken,
speckleFunctionReleaseTag
speckleFunctionReleaseTag,
speckleFunctionRecommendedCPUm,
speckleFunctionRecommendedMemoryMi
}: InputVariables,
commitId: string
// { gitCommitSha, gitRefName, gitRefType }: RequiredEnvVars
Expand All @@ -91,7 +117,9 @@ const registerNewVersionForTheSpeckleAutomateFunction = async (
commitId,
versionTag: speckleFunctionReleaseTag,
command: speckleFunctionCommand,
inputSchema: speckleFunctionInputSchema
inputSchema: speckleFunctionInputSchema,
recommendedCPUm: speckleFunctionRecommendedCPUm,
recommendedMemoryMi: speckleFunctionRecommendedMemoryMi
}
const versionRegisterUrl = new URL(
`/api/v1/functions/${speckleFunctionId}/versions`,
Expand Down Expand Up @@ -197,7 +225,7 @@ export async function run(): Promise<void> {
return failAndReject(e, 'Failed to register the new function version')
}
core.info(
`Registered function version tagged as ${inputVariables.speckleFunctionReleaseTag} with new id: ${versionId}`
`Registered function version tagged as ${inputVariables.speckleFunctionReleaseTag} with new id: ${versionId}. Recommended CPU: ${inputVariables.speckleFunctionRecommendedCPUm}m, recommended memory: ${inputVariables.speckleFunctionRecommendedMemoryMi}Mi.`
)
core.setOutput('speckle_automate_function_release_id', versionId)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ describe('Register new version', () => {
vi.stubEnv('HOME', tmpDir) // the input schema file path is assumed to be relative to the home directory
vi.stubEnv('INPUT_SPECKLE_FUNCTION_INPUT_SCHEMA_FILE_PATH', './schema.json')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_RELEASE_TAG', 'v1.0.0')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_RECOMMENDED_CPU_M', '1000')
vi.stubEnv('INPUT_SPECKLE_FUNCTION_RECOMMENDED_MEMORY_MI', '500')
vi.stubEnv('INPUT_SPECKLE_AUTOMATE_URL', 'http://myfakeautomate.speckle.internal')
vi.stubEnv('GITHUB_SHA', 'commitSha')
vi.stubEnv('GITHUB_REF_TYPE', 'commit')
Expand Down

0 comments on commit b637bae

Please sign in to comment.