Triggering from your app
Wait for a run
triggerAndWait starts a run and returns its output. runs.poll waits for a run you already started, until it finishes or a condition you choose is true.
On this page
Trigger and wait#
triggerAndWait starts a run, waits for it to finish, and returns its output:
import { tasks, RunFailedError, WaitTimeoutError } from '@bokjoy/sdk'; try { const output = await tasks.triggerAndWait( 'email.send', { email: 'grace@example.com', template: 'welcome' }, { idempotencyKey: 'welcome-grace', timeoutMs: 60_000 }, ); console.log('sent:', output.sent);} catch (error) { if (error instanceof RunFailedError) { console.error(`run ${error.run.status}: ${error.run.error}`); } else if (error instanceof WaitTimeoutError) { // The run keeps going. Read it again later. console.error('still running:', error.run?.id); } else { throw error; }}from bokjoy import RunFailedError, WaitTimeoutError, tasks try: output = tasks.trigger_and_wait( "email.send", {"email": "grace@example.com", "template": "welcome"}, idempotency_key="welcome-grace", timeout=60, ) print("sent:", output["sent"])except RunFailedError as error: print(f"run {error.run.status}: {error.run.error}")except WaitTimeoutError as error: # The run keeps going. Read it again later. print("still running:", error.run and error.run.id)| The run | triggerAndWait |
|---|---|
| Completes | Returns its output. |
| Fails or is cancelled | Throws RunFailedError. The run is on error.run, with its status and error. |
| Is still going at the timeout | Throws WaitTimeoutError. The run keeps going. |
It takes the trigger options and the wait options below. Unlike Trigger.dev's triggerAndWait, it works from any backend, not only inside a task.
Wait for a run you started#
runs.poll waits for a run and returns it once it has finished, whatever its status. It never throws for a failed run, so check the status yourself. Pass until to stop earlier:
import { runs, tasks } from '@bokjoy/sdk'; const handle = await tasks.trigger( 'user.onboarding', { email: 'ada@example.com' }, { idempotencyKey: 'signup-ada' },); // Stop as soon as the workflow waits, or once it finishes.const run = await runs.poll(handle, { until: (run) => run.isWaiting, timeoutMs: 120_000,}); console.log(run.status, run.attempts.length);from bokjoy import runs, tasks handle = tasks.trigger( "user.onboarding", {"email": "ada@example.com"}, idempotency_key="signup-ada",) # Stop as soon as the workflow waits, or once it finishes.run = runs.poll( handle, until=lambda run: run.is_waiting, timeout=120,) print(run.status, len(run.attempts))poll never changes the run. A wait that times out leaves it running.
Wait options#
| Node | Python | Default |
|---|---|---|
timeoutMs | timeout (seconds) | None. Past it, WaitTimeoutError. |
pollIntervalMs | poll_interval (seconds) | 1 second |
until | until | Stop early when it returns true. poll only. |
stream | stream | true: follow the run stream, as below. |
signal | An AbortSignal that ends the wait. |
The timeout is a deadline for the whole wait: every request, retry and pause stops at it. For triggerAndWait it starts before the trigger request, so it covers the trigger too.
Timeouts#
WaitTimeoutError carries the run's last known state on error.run, or null (Python: None) when no answer came in time.
If the trigger itself got no answer in time, the run may or may not have started. error.idempotencyKey (Python: idempotency_key) is the key the client sent. Trigger again with that key to reach the same run without starting a second one.
How a wait follows the run#
A wait opens the run stream and reads the run each time its status changes, so it returns as soon as the run finishes. It also reads the run every 10 seconds in case a change went unseen. If the stream is unavailable or drops, the wait reads the run every poll interval instead. Pass stream: false to always poll.
Read the result later#
A wait is optional. Save the run's ID when you trigger, and read the run whenever you need it with runs.retrieve. Results are kept: Bok Joy never deletes a run.