From 7e0a0ed6992ee69df589606cc3e952508ef8a79f Mon Sep 17 00:00:00 2001 From: Hao Luo Date: Thu, 22 Sep 2022 14:35:47 -0500 Subject: [PATCH] added long-running action and example template Signed-off-by: Hao Luo --- .../sample-templates/all-templates.yaml | 1 + .../longrunning-demo/wait-action.yaml | 43 ++++++++++ .../actions/builtin/createBuiltinActions.ts | 2 + .../actions/builtin/stress/index.ts | 1 + .../actions/builtin/stress/waitWorkflow.ts | 86 +++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 plugins/scaffolder-backend/sample-templates/longrunning-demo/wait-action.yaml create mode 100644 plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/index.ts create mode 100644 plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/waitWorkflow.ts diff --git a/plugins/scaffolder-backend/sample-templates/all-templates.yaml b/plugins/scaffolder-backend/sample-templates/all-templates.yaml index 6637c9479b..a3c81f8af0 100644 --- a/plugins/scaffolder-backend/sample-templates/all-templates.yaml +++ b/plugins/scaffolder-backend/sample-templates/all-templates.yaml @@ -6,6 +6,7 @@ metadata: spec: targets: - ./remote-templates.yaml + - ./longrunning-demo/wait-action.yaml # For local development of a template, you can reference your local templates here. # Examples: # diff --git a/plugins/scaffolder-backend/sample-templates/longrunning-demo/wait-action.yaml b/plugins/scaffolder-backend/sample-templates/longrunning-demo/wait-action.yaml new file mode 100644 index 0000000000..56032408b7 --- /dev/null +++ b/plugins/scaffolder-backend/sample-templates/longrunning-demo/wait-action.yaml @@ -0,0 +1,43 @@ +apiVersion: scaffolder.backstage.io/v1beta3 +# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-template +kind: Template +metadata: + name: wait-action + title: wait-action + description: template with a waiting task for a long running action +spec: + owner: user:guest + type: service + + # These parameters are used to generate the input form in the frontend, and are + # used to gather input data for the execution of the template. + parameters: + - title: Fill in some steps + required: + - name + properties: + name: + title: Name + type: string + description: Unique name of the component + default: some value + ui:autofocus: true + ui:options: + rows: 5 + + # These steps are executed in the scaffolder backend, using data that we gathered + # via the parameters above. + steps: + - id: someAsyncCall + name: Some Async Call + action: debug:log + input: + message: Some async call that return a tickedId (guid, for instance, that identifies a process) + + - id: waitWorkflow + name: Wait Creation + action: stress:waitWorkflow + input: + name: 'test' #${{ steps.someAsyncCall.output.ticketId }} + delayMilliseconds: 1000 + maxCalls: 30 diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts index fcddf127e2..68e8e4cef0 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/createBuiltinActions.ts @@ -55,6 +55,7 @@ import { createPublishGitlabAction, createPublishGitlabMergeRequestAction, } from './publish'; +import { waitWorkflow } from './stress'; /** * The options passed to {@link createBuiltinActions} @@ -181,6 +182,7 @@ export const createBuiltinActions = ( config, githubCredentialsProvider, }), + waitWorkflow(), ]; return actions as TemplateAction[]; diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/index.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/index.ts new file mode 100644 index 0000000000..d6b15d0f2a --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/index.ts @@ -0,0 +1 @@ +export { waitWorkflow } from './waitWorkflow'; diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/waitWorkflow.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/waitWorkflow.ts new file mode 100644 index 0000000000..317d4ee4aa --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/stress/waitWorkflow.ts @@ -0,0 +1,86 @@ +import { createTemplateAction } from '@backstage/plugin-scaffolder-backend'; + +export function waitWorkflow() { + type InputParameters = { + name: string; + delayMilliseconds: number; + maxCalls: number; + }; + + return createTemplateAction({ + id: 'stress:waitWorkflow', + schema: { + input: { + required: ['name'], + type: 'object', + properties: { + name: { + type: 'string', + title: 'Name', + description: + 'The Ticket Number used to check the status of the Workflow.', + }, + delayMilliseconds: { + type: 'number', + title: 'Delay', + description: + 'The amount of milliseconds to delay before the next checkStatus call.', + }, + maxCalls: { + type: 'number', + title: 'Max Calls', + description: + 'The amount of maximum calls allowed to check a status before stopping.', + }, + }, + }, + output: { + type: 'object', + properties: { + status: { + title: 'Status', + type: 'object', + properties: { + message: { + title: 'Status Message', + type: 'string', + }, + }, + }, + results: { + title: 'Results from Ticket execution', + type: 'object', + }, + }, + }, + }, + async handler(ctx) { + const { name, maxCalls, delayMilliseconds } = getParameters(ctx.input); + + ctx.logger.info(`Running the long running workflow: ${name}...`); + + let i = 1; + + for (;;) { + ctx.logger.info(`waiting... [${i}/${maxCalls}]`); + + if (i++ >= maxCalls) { + break; + } + + await new Promise(res => setTimeout(res, delayMilliseconds)); + } + + ctx.output('status.message', 'Workflow completed'); + }, + }); + + function getParameters(input: InputParameters) { + const { name, delayMilliseconds, maxCalls } = input; + return { + name, + maxCalls: maxCalls ?? 30, + delayMilliseconds: delayMilliseconds ?? 2000, + }; + } +}