Skip to content

Commit 4133c69

Browse files
committed
add jobmonitoring example
Signed-off-by: Antonio Mendoza Pérez <[email protected]>
1 parent a072cc4 commit 4133c69

11 files changed

+362
-23
lines changed

spec/examples/applicantrequest.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {TransitionDataConditionBuilder} from "../../src";
2222
import {OperationStateBuilder} from "../../src";
2323
import {SubFlowStateBuilder} from "../../src";
2424
import {ActionBuilder} from "../../src";
25-
import {DefaultTransitionTypeBuilder} from "../../src";
25+
import {DefaultTransitionBuilder} from "../../src";
2626
import {FunctionRefBuilder} from "../../src";
2727

2828

@@ -53,7 +53,7 @@ describe("applicationrequest workflow example", () => {
5353
.withCondition("${ .applicants | .age < 18 }")
5454
.withTransition("RejectApplication")
5555
.build()])
56-
.withDefault(new DefaultTransitionTypeBuilder()
56+
.withDefault(new DefaultTransitionBuilder()
5757
.withTransition(
5858
"RejectApplication",
5959
).build())

spec/examples/jobmonitoring.json

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{
2+
"id": "jobmonitoring",
3+
"version": "1.0",
4+
"name": "Job Monitoring",
5+
"description": "Monitor finished execution of a submitted job",
6+
"start": "SubmitJob",
7+
"functions": [
8+
{
9+
"name": "submitJob",
10+
"operation": "http://myapis.org/monitorapi.json#doSubmit",
11+
"type": "rest"
12+
},
13+
{
14+
"name": "checkJobStatus",
15+
"operation": "http://myapis.org/monitorapi.json#checkStatus",
16+
"type": "rest"
17+
},
18+
{
19+
"name": "reportJobSuceeded",
20+
"operation": "http://myapis.org/monitorapi.json#reportSucceeded",
21+
"type": "rest"
22+
},
23+
{
24+
"name": "reportJobFailed",
25+
"operation": "http://myapis.org/monitorapi.json#reportFailure",
26+
"type": "rest"
27+
}
28+
],
29+
"states": [
30+
{
31+
"name": "SubmitJob",
32+
"type": "operation",
33+
"actionMode": "sequential",
34+
"actions": [
35+
{
36+
"functionRef": {
37+
"refName": "submitJob",
38+
"arguments": {
39+
"name": "${ .job.name }"
40+
}
41+
},
42+
"actionDataFilter": {
43+
"results": "${ .jobuid }"
44+
}
45+
}
46+
],
47+
"onErrors": [
48+
{
49+
"error": "*",
50+
"transition": "SubmitError"
51+
}
52+
],
53+
"stateDataFilter": {
54+
"output": "${ .jobuid }"
55+
},
56+
"transition": "WaitForCompletion"
57+
},
58+
{
59+
"name": "SubmitError",
60+
"type": "subflow",
61+
"workflowId": "handleJobSubmissionErrorWorkflow",
62+
"end": true
63+
},
64+
{
65+
"name": "WaitForCompletion",
66+
"type": "delay",
67+
"timeDelay": "PT5S",
68+
"transition": "GetJobStatus"
69+
},
70+
{
71+
"name": "GetJobStatus",
72+
"type": "operation",
73+
"actionMode": "sequential",
74+
"actions": [
75+
{
76+
"functionRef": {
77+
"refName": "checkJobStatus",
78+
"arguments": {
79+
"name": "${ .jobuid }"
80+
}
81+
},
82+
"actionDataFilter": {
83+
"results": "${ .jobstatus }"
84+
}
85+
}
86+
],
87+
"stateDataFilter": {
88+
"output": "${ .jobstatus }"
89+
},
90+
"transition": "DetermineCompletion"
91+
},
92+
{
93+
"name": "DetermineCompletion",
94+
"type": "switch",
95+
"dataConditions": [
96+
{
97+
"condition": "${ .jobStatus == \"SUCCEEDED\" }",
98+
"transition": "JobSucceeded"
99+
},
100+
{
101+
"condition": "${ .jobStatus == \"FAILED\" }",
102+
"transition": "JobFailed"
103+
}
104+
],
105+
"default": {
106+
"transition": "WaitForCompletion"
107+
}
108+
},
109+
{
110+
"name": "JobSucceeded",
111+
"type": "operation",
112+
"actionMode": "sequential",
113+
"actions": [
114+
{
115+
"functionRef": {
116+
"refName": "reportJobSuceeded",
117+
"arguments": {
118+
"name": "${ .jobuid }"
119+
}
120+
}
121+
}
122+
],
123+
"end": true
124+
},
125+
{
126+
"name": "JobFailed",
127+
"type": "operation",
128+
"actionMode": "sequential",
129+
"actions": [
130+
{
131+
"functionRef": {
132+
"refName": "reportJobFailed",
133+
"arguments": {
134+
"name": "${ .jobuid }"
135+
}
136+
}
137+
}
138+
],
139+
"end": true
140+
}
141+
]
142+
}

spec/examples/jobmonitoring.spec.ts

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import {
18+
ActionBuilder,
19+
ActionDataFilterBuilder,
20+
DatabasedSwitchBuilder,
21+
DefaultTransitionBuilder,
22+
DelayStateBuilder,
23+
FunctionDefBuilder,
24+
FunctionRefBuilder,
25+
OnErrorBuilder,
26+
OperationStateBuilder,
27+
StateDataFilterBuilder,
28+
SubFlowStateBuilder,
29+
TransitionDataConditionBuilder,
30+
WorkflowBuilder,
31+
} from "../../src";
32+
import * as fs from "fs";
33+
34+
35+
describe("jobmonitoring workflow example", () => {
36+
37+
38+
it('should generate Workflow object', function () {
39+
40+
const workflow = new WorkflowBuilder()
41+
.withId("jobmonitoring")
42+
.withVersion("1.0")
43+
.withName("Job Monitoring")
44+
.withDescription("Monitor finished execution of a submitted job")
45+
.withStart("SubmitJob")
46+
.withFunctions([
47+
new FunctionDefBuilder()
48+
.withName("submitJob")
49+
.withOperation("http://myapis.org/monitorapi.json#doSubmit")
50+
.build(),
51+
new FunctionDefBuilder()
52+
.withName("checkJobStatus")
53+
.withOperation("http://myapis.org/monitorapi.json#checkStatus")
54+
.build(),
55+
new FunctionDefBuilder()
56+
.withName("reportJobSuceeded")
57+
.withOperation("http://myapis.org/monitorapi.json#reportSucceeded")
58+
.build(),
59+
new FunctionDefBuilder()
60+
.withName("reportJobFailed")
61+
.withOperation("http://myapis.org/monitorapi.json#reportFailure")
62+
.build(),
63+
])
64+
.withStates([
65+
new OperationStateBuilder()
66+
.withName("SubmitJob")
67+
.withActionMode('sequential')
68+
.withActions([
69+
new ActionBuilder()
70+
.withFunctionRef(
71+
new FunctionRefBuilder()
72+
.withRefName("submitJob")
73+
.withArguments({
74+
"name": "${ .job.name }",
75+
})
76+
.build(),
77+
)
78+
.withActionDataFilter(
79+
new ActionDataFilterBuilder()
80+
.withResults("${ .jobuid }")
81+
.build(),
82+
)
83+
.build(),
84+
])
85+
.withOnErrors([
86+
new OnErrorBuilder()
87+
.withError("*")
88+
.withTransition("SubmitError")
89+
.build(),
90+
])
91+
.withStateDataFilter(
92+
new StateDataFilterBuilder()
93+
.withOutput("${ .jobuid }")
94+
.build(),
95+
)
96+
.withTransition("WaitForCompletion")
97+
.build(),
98+
new SubFlowStateBuilder()
99+
.withName("SubmitError")
100+
.withWorkflowId("handleJobSubmissionErrorWorkflow")
101+
.withEnd(true)
102+
.build(),
103+
new DelayStateBuilder()
104+
.withName("WaitForCompletion")
105+
.withTimeDelay("PT5S")
106+
.withTransition("GetJobStatus")
107+
.build(),
108+
new OperationStateBuilder()
109+
.withName("GetJobStatus")
110+
.withActionMode('sequential')
111+
.withActions([
112+
new ActionBuilder()
113+
.withFunctionRef(
114+
new FunctionRefBuilder()
115+
.withRefName("checkJobStatus")
116+
.withArguments({
117+
"name": "${ .jobuid }",
118+
})
119+
.build(),
120+
)
121+
.withActionDataFilter(
122+
new ActionDataFilterBuilder()
123+
.withResults("${ .jobstatus }")
124+
.build(),
125+
)
126+
.build(),
127+
])
128+
.withStateDataFilter(
129+
new StateDataFilterBuilder()
130+
.withOutput("${ .jobstatus }")
131+
.build(),
132+
)
133+
.withTransition("DetermineCompletion")
134+
.build(),
135+
new DatabasedSwitchBuilder()
136+
.withName("DetermineCompletion")
137+
.withDataConditions([
138+
new TransitionDataConditionBuilder()
139+
.withCondition("${ .jobStatus == \"SUCCEEDED\" }")
140+
.withTransition("JobSucceeded")
141+
.build(),
142+
new TransitionDataConditionBuilder()
143+
.withCondition("${ .jobStatus == \"FAILED\" }")
144+
.withTransition("JobFailed")
145+
.build(),
146+
])
147+
.withDefault(
148+
new DefaultTransitionBuilder()
149+
.withTransition("WaitForCompletion")
150+
.build())
151+
.build(),
152+
new OperationStateBuilder()
153+
.withName("JobSucceeded")
154+
.withActionMode('sequential')
155+
.withActions([
156+
new ActionBuilder()
157+
.withFunctionRef(
158+
new FunctionRefBuilder()
159+
.withRefName("reportJobSuceeded")
160+
.withArguments({
161+
"name": "${ .jobuid }",
162+
})
163+
.build(),
164+
)
165+
.build(),
166+
])
167+
.withEnd(true)
168+
.build(),
169+
new OperationStateBuilder()
170+
.withName("JobFailed")
171+
.withActionMode('sequential')
172+
.withActions([
173+
new ActionBuilder()
174+
.withFunctionRef(
175+
new FunctionRefBuilder()
176+
.withRefName("reportJobFailed")
177+
.withArguments({
178+
"name": "${ .jobuid }",
179+
})
180+
.build(),
181+
)
182+
.build(),
183+
])
184+
.withEnd(true)
185+
.build(),
186+
])
187+
.build();
188+
189+
190+
const expected = JSON.parse(fs.readFileSync("./spec/examples/jobmonitoring.json")
191+
.toLocaleString()) as any;
192+
expect(workflow).toEqual(expected);
193+
194+
});
195+
196+
197+
});

spec/model/action-builder.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717

18-
import {ActionBuilder, ActionDataFilterType, EventRef} from '../../src';
18+
import {ActionBuilder, ActionDataFilter, EventRef} from '../../src';
1919
import {ActionDataFilterBuilder} from '../../src/model/action-data-filter.builder';
2020

2121

@@ -63,7 +63,7 @@ describe("ActionBuilder", () => {
6363
});
6464

6565

66-
function validActionDataFilter(): ActionDataFilterType {
66+
function validActionDataFilter(): ActionDataFilter {
6767
return {};
6868
}
6969

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export {BranchBuilder} from "./model/branch.builder";
2323
export {CronDefBuilder} from "./model/cron-def.builder";
2424
export {DelayStateBuilder} from "./model/delay-state.builder";
2525
export {DatabasedSwitchBuilder} from "./model/databased-switch.builder";
26-
export {DefaultTransitionTypeBuilder} from "./model/default-transition-type.builder";
26+
export {DefaultTransitionBuilder} from "./model/default-transition.builder";
2727
export {EndBuilder} from "./model/end.builder";
2828
export {EventBuilder} from "./model/event.builder";
2929
export {EventBasedSwitchBuilder} from "./model/event-based-switch.builder";

0 commit comments

Comments
 (0)