@redflow/client 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +17 -0
- package/src/client.ts +1111 -0
- package/src/default.ts +13 -0
- package/src/index.ts +17 -0
- package/src/internal/errors.ts +150 -0
- package/src/internal/json.ts +32 -0
- package/src/internal/keys.ts +38 -0
- package/src/internal/sleep.ts +4 -0
- package/src/internal/time.ts +3 -0
- package/src/registry.ts +55 -0
- package/src/types.ts +181 -0
- package/src/worker.ts +1042 -0
- package/src/workflow.ts +42 -0
- package/tests/bugfixes.test.ts +761 -0
- package/tests/fixtures/worker-crash.ts +42 -0
- package/tests/fixtures/worker-recover.ts +63 -0
- package/tests/helpers/getFreePort.ts +18 -0
- package/tests/helpers/redisTestServer.ts +77 -0
- package/tests/helpers/waitFor.ts +16 -0
- package/tests/redflow.e2e.test.ts +3154 -0
- package/tsconfig.json +3 -0
package/src/workflow.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ZodTypeAny } from "zod";
|
|
2
|
+
import { getDefaultClient } from "./default";
|
|
3
|
+
import { validateInputWithSchema } from "./client";
|
|
4
|
+
import { getDefaultRegistry, type WorkflowHandler, type WorkflowHandlerContext } from "./registry";
|
|
5
|
+
import type { DefineWorkflowOptions, RunHandle, RunOptions } from "./types";
|
|
6
|
+
|
|
7
|
+
export type InferInput<TSchema extends ZodTypeAny | undefined> = TSchema extends ZodTypeAny ? import("zod").infer<TSchema> : unknown;
|
|
8
|
+
|
|
9
|
+
export type Workflow<TInput, TOutput> = {
|
|
10
|
+
name: string;
|
|
11
|
+
run(input: TInput, options?: RunOptions): Promise<RunHandle<TOutput>>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function defineWorkflow<TSchema extends ZodTypeAny | undefined, TOutput>(
|
|
15
|
+
options: DefineWorkflowOptions<TSchema>,
|
|
16
|
+
handler: WorkflowHandler<InferInput<TSchema>, TOutput>,
|
|
17
|
+
): Workflow<InferInput<TSchema>, TOutput> {
|
|
18
|
+
getDefaultRegistry().register({ options, handler });
|
|
19
|
+
|
|
20
|
+
const name = options.name;
|
|
21
|
+
const queue = options.queue ?? "default";
|
|
22
|
+
const configuredMaxAttempts = options.retries?.maxAttempts;
|
|
23
|
+
const maxAttemptsOverride =
|
|
24
|
+
typeof configuredMaxAttempts === "number" && Number.isFinite(configuredMaxAttempts) && configuredMaxAttempts > 0
|
|
25
|
+
? Math.floor(configuredMaxAttempts)
|
|
26
|
+
: undefined;
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
name,
|
|
30
|
+
async run(input, runOptions) {
|
|
31
|
+
const parsedInput = options.schema ? (validateInputWithSchema(options.schema, input) as InferInput<TSchema>) : input;
|
|
32
|
+
const client = getDefaultClient();
|
|
33
|
+
return await client.runByName<TOutput>(name, parsedInput, {
|
|
34
|
+
...runOptions,
|
|
35
|
+
queueOverride: runOptions?.queueOverride ?? queue,
|
|
36
|
+
__maxAttemptsOverride: maxAttemptsOverride,
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type { WorkflowHandler, WorkflowHandlerContext };
|