Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3: update trigger API design #1045

Merged
merged 1 commit into from
Apr 19, 2024
Merged

v3: update trigger API design #1045

merged 1 commit into from
Apr 19, 2024

Conversation

ericallam
Copy link
Member

Updates the trigger, batchTrigger and their *AndWait variants to use the first parameter for the payload/items, and the second parameter for options.

Before:

await yourTask.trigger({ payload: { foo: "bar" }, options: { idempotencyKey: "key_1234" } });
await yourTask.triggerAndWait({ payload: { foo: "bar" }, options: { idempotencyKey: "key_1234" } });

await yourTask.batchTrigger({ items: [{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }] });
await yourTask.batchTriggerAndWait({ items: [{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }] });

After:

await yourTask.trigger({ foo: "bar" }, { idempotencyKey: "key_1234" });
await yourTask.triggerAndWait({ foo: "bar" }, { idempotencyKey: "key_1234" });

await yourTask.batchTrigger([{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }]);
await yourTask.batchTriggerAndWait([{ payload: { foo: "bar" } }, { payload: { foo: "baz" } }]);

We've also changed the API of the triggerAndWait result. Before, if the subtask that was triggered finished with an error, we would automatically "rethrow" the error in the parent task.

Now instead we're returning a TaskRunResult object that allows you to discriminate between successful and failed runs in the subtask:

Before:

try {
  const result = await yourTask.triggerAndWait({ foo: "bar" });

  // result is the output of your task
  console.log("result", result);

} catch (error) {
  // handle subtask errors here
}

After:

const result = await yourTask.triggerAndWait({ foo: "bar" });

if (result.ok) {
  console.log(`Run ${result.id} succeeded with output`, result.output);
} else {
  console.log(`Run ${result.id} failed with error`, result.error);
}

…o use the first parameter for the payload/items, and the second parameter for options

Also always returns a `TaskRunResult` object from `triggerAndWait` instead of rethrowing subtask errors in the parent
Copy link

changeset-bot bot commented Apr 19, 2024

🦋 Changeset detected

Latest commit: f936e04

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ericallam ericallam merged commit 374edef into main Apr 19, 2024
4 checks passed
@ericallam ericallam deleted the v3/trigger-api-redesign branch April 19, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant