Skip to content

Commit 481f758

Browse files
authored
feat: add delay to client.trigger (#100)
1 parent 038bad4 commit 481f758

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/client/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ describe("workflow client", () => {
209209
headers: { "user-header": "user-header-value" },
210210
workflowRunId: myWorkflowRunId,
211211
retries: 15,
212+
delay: 1,
212213
});
213214
},
214215
responseFields: {
@@ -228,6 +229,7 @@ describe("workflow client", () => {
228229
"upstash-workflow-init": "true",
229230
"upstash-workflow-runid": `wfr_${myWorkflowRunId}`,
230231
"upstash-workflow-url": "https://requestcatcher.com/api",
232+
"upstash-delay": "1s",
231233
},
232234
},
233235
});

src/client/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NotifyResponse, Waiter } from "../types";
2-
import { FlowControl, Client as QStashClient } from "@upstash/qstash";
2+
import { FlowControl, PublishRequest, Client as QStashClient } from "@upstash/qstash";
33
import { makeGetWaitersRequest, makeNotifyRequest } from "./utils";
44
import { getWorkflowRunId } from "../utils";
55
import { triggerFirstInvocation } from "../workflow-requests";
@@ -190,6 +190,11 @@ export class Client {
190190
* with `wfr_`.
191191
* @param retries retry to use in the initial request. in the rest of
192192
* the workflow, `retries` option of the `serve` will be used.
193+
* @param flowControl Settings for controlling the number of active requests
194+
* and number of requests per second with the same key.
195+
* @param delay Delay for the workflow run. This is used to delay the
196+
* execution of the workflow run. The delay is in seconds or can be passed
197+
* as a string with a time unit (e.g. "1h", "30m", "15s").
193198
* @returns workflow run id
194199
*/
195200
public async trigger({
@@ -199,13 +204,15 @@ export class Client {
199204
workflowRunId,
200205
retries,
201206
flowControl,
207+
delay,
202208
}: {
203209
url: string;
204210
body?: unknown;
205211
headers?: Record<string, string>;
206212
workflowRunId?: string;
207213
retries?: number;
208214
flowControl?: FlowControl;
215+
delay?: PublishRequest["delay"];
209216
}): Promise<{ workflowRunId: string }> {
210217
const finalWorkflowRunId = getWorkflowRunId(workflowRunId);
211218
const context = new WorkflowContext({
@@ -223,6 +230,7 @@ export class Client {
223230
const result = await triggerFirstInvocation({
224231
workflowContext: context,
225232
telemetry: undefined, // can't know workflow telemetry here
233+
delay,
226234
});
227235
if (result.isOk()) {
228236
return { workflowRunId: finalWorkflowRunId };

src/workflow-requests.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type {
2020
} from "./types";
2121
import { StepTypes } from "./types";
2222
import type { WorkflowLogger } from "./logger";
23-
import { FlowControl, QstashError } from "@upstash/qstash";
23+
import { FlowControl, PublishRequest, QstashError } from "@upstash/qstash";
2424
import { getSteps } from "./client/utils";
2525
import { getHeaders } from "./qstash/headers";
2626

@@ -30,12 +30,14 @@ export const triggerFirstInvocation = async <TInitialPayload>({
3030
telemetry,
3131
debug,
3232
invokeCount,
33+
delay,
3334
}: {
3435
workflowContext: WorkflowContext<TInitialPayload>;
3536
useJSONContent?: boolean;
3637
telemetry?: Telemetry;
3738
debug?: WorkflowLogger;
3839
invokeCount?: number;
40+
delay?: PublishRequest["delay"];
3941
}): Promise<Ok<"success" | "workflow-run-already-exists", never> | Err<never, Error>> => {
4042
const { headers } = getHeaders({
4143
initHeaderValue: "true",
@@ -72,6 +74,7 @@ export const triggerFirstInvocation = async <TInitialPayload>({
7274
method: "POST",
7375
body,
7476
url: workflowContext.url,
77+
delay,
7578
});
7679

7780
if (result.deduplicated) {

0 commit comments

Comments
 (0)