Skip to content

Commit

Permalink
Add stateId/processType as required fields, and add comment, and refo…
Browse files Browse the repository at this point in the history
…rmat
  • Loading branch information
longquanzheng committed Nov 10, 2023
1 parent 4d80b98 commit fc8f7ee
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 39 deletions.
35 changes: 22 additions & 13 deletions src/xdb/async-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@ import {Communication} from "./communication";
import {StateDecision} from "./state-decision";
import {CommandRequest} from "./command-request";

export interface AsyncState<Input>{
stateOptions?:AsyncStateOptions
waitUntil?:WaitUntilMethod<Input>
execute(ctx:Context, input:Input, persistence:Persistence, communication :Communication):StateDecision
export interface AsyncState<Input> {
// stateId is a required field to identify the state definition
// It must be unique in all the states in the same process definition.
// It's recommended to just use the Class name of the state as the stateId.
stateId: string
// asyncStateOptions is the options for the async state
stateOptions?: AsyncStateOptions
// waitUntil is the optional method to implement the waitUntil API call.
// waitUntil API will return a commandRequest for server to wait for.
// After the commandRequest is completed, the execute API will be invoked.
// if waitUntil is not implemented, the execute API will be invoked immediately.
waitUntil?: WaitUntilMethod<Input>

// execute is the required method to implement the execute API call.
execute(ctx: Context, input: Input, persistence: Persistence, communication: Communication): StateDecision
}

export interface WaitUntilMethod<Input>{
waitUntil(ctx:Context, input:Input, communication: Communication):CommandRequest
export interface WaitUntilMethod<Input> {
waitUntil(ctx: Context, input: Input, communication: Communication): CommandRequest
}
export interface AsyncStateOptions{

export interface AsyncStateOptions {
// waitUntilTimeoutSeconds is the timeout for the waitUntil API call.
// Default: 10 seconds(configurable in server) when set as 0
// It will be capped to 60 seconds by server (configurable in server)
Expand All @@ -26,17 +38,14 @@ export interface AsyncStateOptions{
// waitUntilRetryPolicy is the retry policy for the waitUntil API call.
// Default: infinite retry with 1 second initial interval, 120 seconds max interval, and 2 backoff factor,
// when set as nil
waitUntilRetryPolicy?:RetryPolicy
waitUntilRetryPolicy?: RetryPolicy
// executeRetryPolicy is the retry policy for the execute API call.
// Default: infinite retry with 1 second initial interval, 120 seconds max interval, and 2 backoff factor,
// when set as nil
executeRetryPolicy?:RetryPolicy
executeRetryPolicy?: RetryPolicy
// failureRecoveryState is the state to recover after current state execution fails
// Default: no recovery when set as nil
failureRecoveryState?: AsyncState<any>
// persistencePolicyName is the name of loading policy for persistence if not using default policy
persistencePolicyName?:string
// stateId is the unique identifier of the state.
// It is being used for WorkerService to choose the right AsyncState to execute Start/Execute APIs
stateId?: string
persistencePolicyName?: string
}
2 changes: 1 addition & 1 deletion src/xdb/command-request.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface CommandRequest{
export interface CommandRequest {
// TODO
}
3 changes: 1 addition & 2 deletions src/xdb/communication.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export interface Communication{
export interface Communication {
// TODO
}
3 changes: 1 addition & 2 deletions src/xdb/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export interface Context{
export interface Context {
// TODO
}
2 changes: 1 addition & 1 deletion src/xdb/persistence-schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface PersistenceSchema{
export interface PersistenceSchema {
// TODO
}
2 changes: 1 addition & 1 deletion src/xdb/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface Persistence{
export interface Persistence {
// TODO
}
24 changes: 14 additions & 10 deletions src/xdb/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ import {StateSchema} from "./state-schema";
import {PersistenceSchema} from "./persistence-schema";
import {ProcessIdReusePolicy} from "xdb-ts-api";

export interface Process{
stateSchema?:StateSchema
persistenceSchema?:PersistenceSchema
processOptions?:ProcessOptions
export interface Process {
// processType is a required field to identify the process type.
// It must be unique in all the processes registered in the same registry.
// It's recommended to just use the Class name of the process as the processType.
processType: string
// stateSchema is to define the async states of the process.
stateSchema?: StateSchema
// persistenceSchema is to define the persistence schema of the process.
persistenceSchema?: PersistenceSchema
// processOptions is the options for the process
processOptions?: ProcessOptions
}

export interface ProcessOptions{
export interface ProcessOptions {
// TimeoutSeconds is the timeout for the process execution.
// Default: 0, mean which means infinite timeout
timeoutSeconds?:number
timeoutSeconds?: number
// IdReusePolicy is the policy for reusing process id.
// Default: xdbapi.ALLOW_IF_NO_RUNNING when set as nil
idReusePolicy?:ProcessIdReusePolicy
// processType defines the processType of this process definition.
// GetFinalProcessType set the default value when return empty string
processType?:string
idReusePolicy?: ProcessIdReusePolicy
}
2 changes: 1 addition & 1 deletion src/xdb/state-decision.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface StateDecision{
export interface StateDecision {
// TODO
}
17 changes: 9 additions & 8 deletions src/xdb/state-schema.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import {AsyncState} from "./async-state";

export interface StateSchema{
export interface StateSchema {
startingState: AsyncState<any> | null | undefined
nonStartingStates: AsyncState<any>[]
}

export function newStateSchema(
startingState:AsyncState<any>,
...nonStartingState:AsyncState<any>[]
):StateSchema{
startingState: AsyncState<any>,
...nonStartingState: AsyncState<any>[]
): StateSchema {
return new stateSchema(startingState, ...nonStartingState);
}

export function newStateSchemaNoStartingState(
...nonStartingState:AsyncState<any>[]
):StateSchema{
...nonStartingState: AsyncState<any>[]
): StateSchema {
return new stateSchema(null, ...nonStartingState);
}

class stateSchema implements StateSchema{
class stateSchema implements StateSchema {
nonStartingStates: AsyncState<any>[];
startingState: AsyncState<any> | null | undefined;
constructor(startingState:AsyncState<any>|null, ...nonStartingState:AsyncState<any>[] ) {

constructor(startingState: AsyncState<any> | null, ...nonStartingState: AsyncState<any>[]) {
this.nonStartingStates = nonStartingState;
this.startingState = startingState;
}
Expand Down

0 comments on commit fc8f7ee

Please sign in to comment.