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 (#243)

* feat(compute resources): suggest recommended limits to CPU and Memory for a Function

* Adds documentation
  • Loading branch information
iainsproat authored Nov 6, 2023
1 parent 2cf7d93 commit 2f4e3cf
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 19 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ Providing a Speckle Function ID allows you to change one of those values, and up

Your Speckle Token must have write permissions for the Speckle Function with this ID, otherwise the publish will fail.

#### `speckle_function_recommended_cpu_m`

*Optional.* The recommended maximum CPU in millicores for the function. If the Function exceeds this limit, it will be throttled to run within the limit.

1000 millicores = 1 CPU core. Defaults to 1000 millicores (1 CPU core).

#### `speckle_function_recommended_memory_mi`

*Optional.* The recommended maximum memory in mebibytes for the function. If the Function exceeds this limit, it will be **terminated**.

1024 mebibytes = 1 gibibyte. Defaults to 100 mebibytes.

### Outputs

#### `version_id`
Expand Down
6 changes: 6 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ inputs:
speckle_function_release_tag:
description: 'User defined tag for the function release'
required: true
speckle_function_recommended_cpu_m:
description: 'The recommended maximum CPU in millicores for the function. 1000 millicores = 1 CPU core. Defaults to 1000 millicores (1 CPU core). If the Function exceeds this limit, it will be throttled to run within the limit.'
required: false
speckle_function_recommended_memory_mi:
description: 'The recommended maximum memory in mebibytes for the function. 1024 mebibytes = 1 gibibyte. Defaults to 100 mebibytes. If the Function exceeds this limit, it will be terminated.'
required: false
outputs:
speckle_automate_function_release_id:
description: 'The unique identifier of the function release.'
Expand Down
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 2f4e3cf

Please sign in to comment.