-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for recursion and a decision ending in [*]
- Loading branch information
Showing
13 changed files
with
317 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/design/test/resources/recursion.generated.typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* This file is automatically generated. It gets overwritten on build */ | ||
import {CompleteWorkflow, WrongTimingError, WorkflowBase} from "@gamgee/run"; | ||
import {StateStore} from "@gamgee/interfaces/store"; | ||
import {WorkflowTask} from "@gamgee/interfaces/task"; | ||
|
||
import {CountDownPayload} from "./recursion"; | ||
|
||
export abstract class RecursionWorkflowBase extends WorkflowBase { | ||
protected constructor() { | ||
super('RecursionWorkflow'); | ||
|
||
super._registerStep({ name: 'countDown', run: this.countDown, attempts: 1, backoffMs: 1000 }); | ||
} | ||
|
||
async submit(payload: CountDownPayload, store: StateStore, uniqueInstanceId?: string): Promise<string> { | ||
const task = await super._enqueue('countDown', payload, store, uniqueInstanceId); | ||
return task.instanceId; | ||
} | ||
|
||
abstract countDown(payload: CountDownPayload): Promise<ReturnType<(typeof this.decision)['zero']>> | Promise<ReturnType<(typeof this.decision)['nonZero']>>; | ||
|
||
protected decision = { | ||
zero() { | ||
return CompleteWorkflow; | ||
}, | ||
nonZero(payload: CountDownPayload) { | ||
return { | ||
targetTaskName: 'countDown', | ||
payload, | ||
} | ||
}, | ||
} | ||
|
||
protected _registerStep() { | ||
throw new WrongTimingError(); | ||
} | ||
|
||
protected _enqueue(): Promise<WorkflowTask> { | ||
throw new WrongTimingError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: RecursionWorkflow | ||
--- | ||
|
||
stateDiagram-v2 | ||
state decision <<choice>> | ||
|
||
direction LR | ||
[*] --> countDown | ||
countDown --> decision | ||
decision --> [*]: zero | ||
decision --> countDown: nonZero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Examples | ||
|
||
1. Simple workflow with one step: [simple-workflow](./simple-workflow). | ||
2. A workflow with a decision between going with the left or right branches: [conditions](./conditions). | ||
2. A workflow with a decision between going with the left or right branches: [conditions](./conditions). | ||
3. A workflow counting down recursively: [recursion](./recursion). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
```mermaid | ||
--- | ||
title: RecursionWorkflow | ||
--- | ||
stateDiagram-v2 | ||
direction TB | ||
state decision <<choice>> | ||
[*] --> countDown | ||
countDown --> decision | ||
decision --> [*]: zero | ||
decision --> countDown: nonZero | ||
``` | ||
|
||
A workflow counting down recursively. | ||
|
||
[[Diagram Source](./recursion.mermaid)] [[Generated Scaffold](./recursion.generated.ts)] [[Implementation](./recursion.ts)] |
41 changes: 41 additions & 0 deletions
41
packages/run/docs/examples/recursion/recursion.generated.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* This file is automatically generated. It gets overwritten on build */ | ||
import {CompleteWorkflow, WrongTimingError, WorkflowBase} from "@gamgee/run"; | ||
import {StateStore} from "@gamgee/interfaces/store"; | ||
import {WorkflowTask} from "@gamgee/interfaces/task"; | ||
|
||
import {CountDownPayload} from "./recursion"; | ||
|
||
export abstract class RecursionWorkflowBase extends WorkflowBase { | ||
protected constructor() { | ||
super('RecursionWorkflow'); | ||
|
||
super._registerStep({ name: 'countDown', run: this.countDown, attempts: 1, backoffMs: 1000 }); | ||
} | ||
|
||
async submit(payload: CountDownPayload, store: StateStore, uniqueInstanceId?: string): Promise<string> { | ||
const task = await super._enqueue('countDown', payload, store, uniqueInstanceId); | ||
return task.instanceId; | ||
} | ||
|
||
abstract countDown(payload: CountDownPayload): Promise<ReturnType<(typeof this.decision)['zero']>> | Promise<ReturnType<(typeof this.decision)['nonZero']>>; | ||
|
||
protected decision = { | ||
zero() { | ||
return CompleteWorkflow; | ||
}, | ||
nonZero(payload: CountDownPayload) { | ||
return { | ||
targetTaskName: 'countDown', | ||
payload, | ||
} | ||
}, | ||
} | ||
|
||
protected _registerStep() { | ||
throw new WrongTimingError(); | ||
} | ||
|
||
protected _enqueue(): Promise<WorkflowTask> { | ||
throw new WrongTimingError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: RecursionWorkflow | ||
--- | ||
|
||
stateDiagram-v2 | ||
state decision <<choice>> | ||
|
||
direction LR | ||
[*] --> countDown | ||
countDown --> decision | ||
decision --> [*]: zero | ||
decision --> countDown: nonZero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { RecursionWorkflowBase } from './recursion.generated' | ||
|
||
export type CountDownPayload = number | ||
|
||
export class RecursionWorkflow extends RecursionWorkflowBase { | ||
constructor() { | ||
super() | ||
} | ||
|
||
countDown(count: CountDownPayload) { | ||
if (count === 0) { | ||
return Promise.resolve(this.decision.zero()) | ||
} | ||
|
||
return Promise.resolve(this.decision.nonZero(count - 1)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: RecursionWorkflow | ||
--- | ||
|
||
stateDiagram-v2 | ||
state decision <<choice>> | ||
|
||
direction LR | ||
[*] --> countDown | ||
countDown --> decision | ||
decision --> [*]: zero | ||
decision --> countDown: nonZero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { RecursionWorkflowBase } from './recursion.generated' | ||
import { trace } from '@opentelemetry/api' | ||
|
||
export type CountDownPayload = { testId: string; count: number; failuresRequested: number } | ||
|
||
const failures: { [testId: string]: number } = {} | ||
|
||
export class RecursionWorkflow extends RecursionWorkflowBase { | ||
constructor() { | ||
super() | ||
} | ||
|
||
countDown(payload: CountDownPayload) { | ||
trace.getActiveSpan()?.setAttributes(payload) | ||
|
||
if (payload.failuresRequested > (failures[payload.testId] ??= 0)) { | ||
failures[payload.testId]++ | ||
throw new Error('Task failed successfully :)') | ||
} | ||
|
||
if (payload.count === 0) { | ||
return Promise.resolve(this.decision.zero()) | ||
} | ||
|
||
return Promise.resolve(this.decision.nonZero(Object.assign({}, payload, { count: payload.count - 1 }))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import InMemoryStateStore from '@gamgee/test/stateStores/in-memory' | ||
import { WorkflowWorker } from '../../src/worker' | ||
import { FetchStrategy } from '@gamgee/interfaces/store' | ||
import { TestsTraceExporter } from '../tests-trace-exporter' | ||
import { NodeSDK } from '@opentelemetry/sdk-node' | ||
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base' | ||
import { Span, SpanStatusCode, trace } from '@opentelemetry/api' | ||
import { expect } from '@jest/globals' | ||
import { RecursionWorkflow } from './recursion' | ||
|
||
function randomString(): string { | ||
return Math.random().toString(36).slice(2) | ||
} | ||
|
||
describe('test recursion workflow', () => { | ||
const testsTraceExporter = new TestsTraceExporter() | ||
const sdk: NodeSDK = new NodeSDK({ | ||
spanProcessor: new SimpleSpanProcessor(testsTraceExporter), | ||
}) | ||
sdk.start() | ||
|
||
afterAll(async () => { | ||
await sdk.shutdown() | ||
}) | ||
|
||
it.concurrent('runs the workflow', async () => { | ||
const testId = randomString() | ||
const store = new InMemoryStateStore() | ||
|
||
const parentSpanContext = await trace | ||
.getTracer('test') | ||
.startActiveSpan(expect.getState().currentTestName!, { root: true }, async (span: Span) => { | ||
const workflow = new RecursionWorkflow() | ||
await workflow.submit( | ||
{ | ||
testId, | ||
count: 4, | ||
failuresRequested: 0, | ||
}, | ||
store, | ||
) | ||
|
||
const worker = new WorkflowWorker() | ||
const result = await worker.executeWaitingWorkflow( | ||
store, | ||
{ workflowType: workflow.workflowType }, | ||
FetchStrategy.Random, | ||
1000, | ||
) | ||
|
||
expect(result).toStrictEqual('Workflow Completed') | ||
|
||
return span.spanContext() | ||
}) | ||
|
||
expect(store.getStats()).toStrictEqual({ | ||
taskUpdatesSeen: 6, // Create, ran countDown(4), ran countDown(3), ran countDown(2), ran countDown(1), and done | ||
tasksRemaining: 0, | ||
unrecoverableTasks: 0, | ||
}) | ||
|
||
const spansByTraceId = testsTraceExporter.getSpansByTraceId(parentSpanContext.traceId) | ||
expect(spansByTraceId).toMatchObject([ | ||
{ | ||
name: 'RecursionWorkflow.countDown', | ||
parentSpanId: parentSpanContext.spanId, | ||
status: { code: SpanStatusCode.OK, message: 'Continuing to countDown' }, | ||
attributes: { | ||
testId, | ||
count: 4, | ||
failuresRequested: 0, | ||
}, | ||
}, | ||
{ | ||
name: 'RecursionWorkflow.countDown', | ||
parentSpanId: spansByTraceId[0].spanId, | ||
status: { code: SpanStatusCode.OK, message: 'Continuing to countDown' }, | ||
attributes: { | ||
testId, | ||
count: 3, | ||
failuresRequested: 0, | ||
}, | ||
}, | ||
{ | ||
name: 'RecursionWorkflow.countDown', | ||
parentSpanId: spansByTraceId[1].spanId, | ||
status: { code: SpanStatusCode.OK, message: 'Continuing to countDown' }, | ||
attributes: { | ||
testId, | ||
count: 2, | ||
failuresRequested: 0, | ||
}, | ||
}, | ||
{ | ||
name: 'RecursionWorkflow.countDown', | ||
parentSpanId: spansByTraceId[2].spanId, | ||
status: { code: SpanStatusCode.OK, message: 'Continuing to countDown' }, | ||
attributes: { | ||
testId, | ||
count: 1, | ||
failuresRequested: 0, | ||
}, | ||
}, | ||
{ | ||
name: 'RecursionWorkflow.countDown', | ||
parentSpanId: spansByTraceId[3].spanId, | ||
status: { code: SpanStatusCode.OK, message: 'Workflow Completed' }, | ||
attributes: { | ||
testId, | ||
count: 0, | ||
failuresRequested: 0, | ||
}, | ||
}, | ||
]) | ||
}) | ||
}) |