( params: TaskOptions<TInput, TOutput, TInitOutput> )
| 330 | }; |
| 331 | |
| 332 | export function createTask<TInput = void, TOutput = unknown, TInitOutput extends InitOutput = any>( |
| 333 | params: TaskOptions<TInput, TOutput, TInitOutput> |
| 334 | ): Task<TInput, TOutput> { |
| 335 | const task: Task<TInput, TOutput> = { |
| 336 | id: params.id, |
| 337 | trigger: async (payload, options) => { |
| 338 | const apiClient = apiClientManager.client; |
| 339 | |
| 340 | if (!apiClient) { |
| 341 | throw apiClientMissingError(); |
| 342 | } |
| 343 | |
| 344 | const taskMetadata = taskCatalog.getTaskMetadata(params.id); |
| 345 | |
| 346 | const payloadPacket = await stringifyIO(payload); |
| 347 | |
| 348 | const handle = await tracer.startActiveSpan( |
| 349 | taskMetadata ? "Trigger" : `${params.id} trigger()`, |
| 350 | async (span) => { |
| 351 | const response = await apiClient.triggerTask( |
| 352 | params.id, |
| 353 | { |
| 354 | payload: payloadPacket.data, |
| 355 | options: { |
| 356 | queue: options?.queue ?? params.queue, |
| 357 | concurrencyKey: options?.concurrencyKey, |
| 358 | test: taskContext.ctx?.run.isTest, |
| 359 | payloadType: payloadPacket.dataType, |
| 360 | idempotencyKey: options?.idempotencyKey, |
| 361 | }, |
| 362 | }, |
| 363 | { spanParentAsLink: true } |
| 364 | ); |
| 365 | |
| 366 | span.setAttribute("messaging.message.id", response.id); |
| 367 | |
| 368 | return response; |
| 369 | }, |
| 370 | { |
| 371 | kind: SpanKind.PRODUCER, |
| 372 | attributes: { |
| 373 | [SEMATTRS_MESSAGING_OPERATION]: "publish", |
| 374 | [SemanticInternalAttributes.STYLE_ICON]: "trigger", |
| 375 | ["messaging.client_id"]: taskContext.worker?.id, |
| 376 | [SEMATTRS_MESSAGING_DESTINATION]: params.queue?.name ?? params.id, |
| 377 | [SEMATTRS_MESSAGING_SYSTEM]: "trigger.dev", |
| 378 | ...(taskMetadata |
| 379 | ? accessoryAttributes({ |
| 380 | items: [ |
| 381 | { |
| 382 | text: `${taskMetadata.exportName}.trigger()`, |
| 383 | variant: "normal", |
| 384 | }, |
| 385 | ], |
| 386 | style: "codepath", |
| 387 | }) |
| 388 | : {}), |
| 389 | }, |
no test coverage detected
searching dependent graphs…