Skip to main content

Wait for Attribute Match

View as Markdown

WaitForAttributeMatch returns once an Attribute matches a comparison, such as a revision greater than the last one the caller saw. Use it when a caller needs to refresh after durable business state changes, while the Flow keeps running.

How it works

The application owns the Attribute and decides what it means. The wait checks only that Attribute's value.

  1. Keep a revision. A common choice is an integer revision Attribute that starts at zero.
  2. Advance it with every change. Every Step and RPC that changes the business state declares the same lock. While it holds the lock, it reads the revision, adds one, writes the business state, and then writes the new revision.
  3. Wait, then refresh. The caller waits until the revision is greater than the last value it saw. The wait returns the current revision. The caller then fetches the new state with its read RPC, or reads the Attributes with GetAttributes.

Advance revision under a lock

The Job Posting example serializes its update RPC with one Attribute lock. It advances UpdateVersion in the same invocation that writes the state.

job_post_flow.pyPython
@rpc(lock_attributes=(update_posting_lock.lock(),))
def update(self, context: Context, input: JobInfo) -> RPCResult[int]:
version = self.update_version.get(context) + 1
self.title.set(context, input.title or "")
self.job_description.set(context, input.description or "")
self.last_update_time_millis.set(context, int(time.time() * 1000))
if input.notes is not None:
self.notes.set(context, input.notes)
self.update_version.set(context, version)
update = PostingUpdate(
version,
f"{context.flow_id}:{version}",
input,
)
self.linkedin_posting_updates.publish(context, update)
self.indeed_posting_updates.publish(context, update)
return RPCResult(version)

Example: examples/python/dex_examples/products/job-post/job_post_flow.py

Wait, then refresh

The wait addresses the active run by Flow ID and takes the Attribute and its comparison. After it returns a revision greater than the caller's watermark, the controller invokes the read RPC.

controller.pyPython
revision = await app_state.client.wait_for_attribute_match(
flow_id,
app_state.job_post.update_version,
AttributeMatch.greater_than(required_int_query("lastRevision")),
WaitForAttributeOptions(),
)
job_info = await app_state.client.invoke_rpc(app_state.job_post.get, flow_id)

Example: examples/python/dex_examples/products/job-post/controller.py

Rules and pitfalls

  • Treat the revision as a watermark. A wait returns the current value, which may skip values in between. If state moves from 0 to 1 to 2 to 3 quickly, one wait for a value greater than 0 may return 3.
  • Set a request timeout behind a deadline. When the wait runs behind an HTTP endpoint or another entry point with its own deadline, set RequestTimeout. See Wait timeouts and limits.

Supported operators, rejected comparison types, and the active-Flow rule are in Wait for an Attribute match. The full example is the Job Posting use case.

Next