This example shows how custom Search Attributes can be used in your Workflow. This sample uses the pizzaWorkflow
, and creates a custom Search Attribute called isOrderFailed
which is a boolean. The user can then use this Search Attribute to query for Workflows where the pizza order has failed.
You'll need two terminal windows for this sample.
- In all terminals, change to the
/samples/custom-search-attributes/src
directory. - In one terminal, run
npm install
to install dependencies.
- First, you will create your custom Search Attribute,
isOrderFailed
, to a boolean. You can do this in one of your terminals with the following command:temporal operator search-attribute create --namespace default --name isOrderFailed --type bool
. - Make sure you can see all the Search Attributes you have with the command:
temporal operator search-attribute list
. You should now seeisOrderFailed
in this list. It may take up to ten seconds before the attribute appears.
Within the pizzaWorkflow
code, we will now dynamically update Search Attributes using upsertSearchAttributes
.
-
In
pizzaWorkflow.ts
, locateupsertSearchAttributes({ isOrderFailed: [false] })
which is used to indicate that the order has not failed. It is marked not failed, because it is in the part of the logic when the Workflow has received the Signal that the order has been fulfilled. -
In
pizzaWorkflow.ts
, locateupsertSearchAttributes({ isOrderFailed: [true] })
which is used to indicate that the order has failed. It is marked failed in the part of the Workflow code when the Workflow has received the Signal that the order has not been fulfilled successfully.
Once you have Workflows tagged with Custom Search Attributes, you can query them based on these attributes.
In client.ts
, we have:
const iterator = await client.workflow.list({
query: `isOrderFailed = true`,
});
for await (const workflow of iterator) {
console.log('failed order', workflow.workflowId);
}
This lists all the Workflows that fulfill this query.
To run the Workflows:
- In one terminal, start the Worker by running
npm start
. - In another terminal, start the Workflow by running
npm run workflow
.
In your console, you should see the Workflows that succeeded.
You can also send a query with your CLI: temporal workflow list -q 'isOrderFailed=false'
.