This repository was archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
109 lines (97 loc) · 3.07 KB
/
context.ts
File metadata and controls
109 lines (97 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
makeSeededGenerators,
RandomGenerators,
} from "https://raw.githubusercontent.com/alextes/vegas/main/mod.ts";
import { PromiseOrValue } from "./promise.ts";
import {
LocalActivityCommand,
ScheduleActivityCommand,
SleepCommand,
WaitForSignalCommand,
} from "./runtime/core/commands.ts";
import { Arg } from "./types.ts";
export type ActivityResult<T> = PromiseOrValue<T>;
/**
* Returns if the given activity result is a generator or not.
* @param value the activity result
* @returns a typeguard for activity result.
*/
export const isValue = <T>(value: ActivityResult<T>): value is T => {
return (
(value as Generator).next === undefined &&
(value as Promise<T>).then === undefined
);
};
/**
* Activity is the signature of any activity.
*/
export type Activity<TArgs extends Arg, TResult> = (
...args: [...TArgs]
) => ActivityResult<TResult>;
/**
* Activity executor receives an activity and executes it.
*/
export type ActivityExecutor<TArgs extends Arg, TResult> = (
activity: Activity<TArgs, TResult>,
...args: [...TArgs]
) => ActivityResult<TResult>;
/**
* WorkflowContext is used for providing api access to the workflow engine.
*/
export class WorkflowContext {
private rand: RandomGenerators;
constructor(public executionId: string) {
this.rand = makeSeededGenerators(executionId);
}
/**
* waitForSignal wait for the given signal to be occurred.
* @param signal the signal name
*/
public waitForSignal(signal: string): WaitForSignalCommand {
return { name: "wait_signal", signal };
}
/**
* Executes the activity for the given context and args.
* @param activity the activity that should be executed
* @param input the activity args (optionally)
*/
public callActivity<TArgs extends Arg = Arg, TResult = unknown>(
activity: Activity<TArgs, TResult>,
...input: [...TArgs]
): ScheduleActivityCommand<TArgs, TResult> {
return { name: "schedule_activity", activity, input };
}
/**
* Executes the activity for the given context and args.
* @param activity the activity that should be executed
*/
public callLocalActivity<TResult = unknown>(
activity: () => TResult,
): LocalActivityCommand<TResult> {
return { name: "local_activity", result: activity() };
}
/**
* stop the current workflow execution and sleep the given miliseconds time.
* @param sleepMs the time in miliseconds
*/
public sleep(sleepMs: number): SleepCommand {
// get the current date & time (as milliseconds since Epoch)
const currentTimeAsMs = Date.now();
const adjustedTimeAsMs = currentTimeAsMs + sleepMs;
return this.sleepUntil(new Date(adjustedTimeAsMs));
}
/**
* stops the current workflow execution and sleep until the given date.
* @param until the date that should sleep.
*/
public sleepUntil(until: Date): SleepCommand {
return { name: "sleep", until };
}
/**
* Returns a random consistent with the given workflow execution
* @returns a random float value.
*/
public random(): number {
return this.rand.randomInt(0, Number.MAX_SAFE_INTEGER);
}
}