Send signal in batch using typescript sdk

We are trying to send signal to 100’s of running workflows using temporal client in typescript sdk. Instead of sending separate signals for all workflows is there a way we can do this in a single request?

You can use the gRPC API to start a batch signal:

import { randomUUID } from 'node:crypto';
import * as proto from '@temporalio/proto';

const jobId = randomUUID();
await client.workflowService.startBatchOperation({
  namespace: 'my-namespace',
  jobId,
  signalOperation: {
    signal: 'some-signal',
    input: {
      payloads: ['arg1', 'arg2'].map((arg) => client.options.loadedDataConverter.payloadConverter.toPayload(arg)),
    },
  },
});
while (true) {
  const response = await client.workflowService.describeBatchOperation({ jobId });
  if (response.state === proto.temporal.api.enums.v1.BatchOperationState.BATCH_OPERATION_STATE_FAILED) {
    throw new Error('batch failed');
  }
  if (response.state === proto.temporal.api.enums.v1.BatchOperationState.BATCH_OPERATION_STATE_RUNNING) {
    await new Promise((resolve) => setTimeout(resolve, 1000));
  }
  break;
}
1 Like

Do you know if there is a limit to how many workflows we can send a signal to in one go?

There’s no limit, the system manages this batching for you.
Oh, I forgot to add a query or set of executions for the batch job in my example.

Again thanks for this. While constructing the payload, can each arg be a key value pair like following:
[{abc:123}, {xyz:456}].map((…

@bergundy I get the following when using the above code to pass args as payload. We are currently using the default data convertor. Any ideas what might be missing?

/app/workflow-bundle-5feb3d484fc78c2a5e62.js:4067
            throw new errors_1.ValueError(`Unknown encoding: ${encoding}`);
            ^

ValueError: Unknown encoding: binary/protobuf
    at DefaultPayloadConverter.fromPayload (/app/node_modules/@temporalio/common/src/converter/payload-converter.ts:163:12)
    at /app/node_modules/@temporalio/common/src/converter/payload-converter.ts:84:54
    at Array.map (<anonymous>)
    at arrayFromPayloads (/app/node_modules/@temporalio/common/src/converter/payload-converter.ts:84:18)
    at Activator.signalWorkflow (/app/node_modules/@temporalio/workflow/src/internals.ts:545:30)
    at /app/node_modules/@temporalio/workflow/src/worker-interface.ts:215:29
    at Object.activate (/app/node_modules/@temporalio/workflow/src/worker-interface.ts:221:2)
    at evalmachine.<anonymous>:1:18

Looks like a server issue, fixed here: [UNTESTED] Fix handling of batch signal input payloads by mjameswh · Pull Request #4374 · temporalio/temporal · GitHub