Wait for Attribute Match
View as MarkdownWaitForAttributeMatch 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.
- Keep a revision. A common choice is an integer revision Attribute that starts at zero.
- 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.
- 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.
@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
func (flow *JobPostingFlow) GetRPCs() []dex.RPCDef {
return []dex.RPCDef{
dex.DefineRPC(flow.Get, nil),
dex.DefineRPC(flow.GetWithStrongConsistency, nil),
dex.DefineRPC(flow.Update, &dex.RPCOptions{
LockAttributes: []dex.AttributeLock{dex.LockAttribute(UpdatePostingLock)},
}),
}
}
func (*JobPostingFlow) Update(
ctx dex.Context,
input JobInfo,
) (*dex.RPCResult[int], error) {
version, err := UpdateVersion.Get(ctx)
if err != nil {
return nil, err
}
version++
if err := Title.Set(ctx, input.Title); err != nil {
return nil, err
}
if err := JobDescription.Set(ctx, input.Description); err != nil {
return nil, err
}
if err := LastUpdateTimeMillis.Set(ctx, time.Now().UnixMilli()); err != nil {
return nil, err
}
if input.Notes != "" {
if err := Notes.Set(ctx, input.Notes); err != nil {
return nil, err
}
}
if err := UpdateVersion.Set(ctx, version); err != nil {
return nil, err
}
update := PostingUpdate{
Version: version,
IdempotencyKey: fmt.Sprintf("%s:%d", ctx.FlowID(), version),
Posting: input,
}
if err := LinkedInPostingUpdates.Publish(ctx, update); err != nil {
return nil, err
}
if err := IndeedPostingUpdates.Publish(ctx, update); err != nil {
return nil, err
}
return &dex.RPCResult[int]{Output: version}, nil
}
@RPC(lockAttributes = {"UpdatePostingLock"})
public RPCResult<Integer> update(final Context context, final JobInfo input) {
final int version = updateVersion.get(context) + 1;
title.set(context, input.title);
jobDescription.set(context, input.description);
lastUpdateTimeMillis.set(context, System.currentTimeMillis());
if (input.notes != null) {
notes.set(context, input.notes);
}
updateVersion.set(context, version);
final PostingUpdate update = new PostingUpdate(
version,
context.getFlowId() + ":" + version,
input);
linkedInPostingUpdates.publish(context, update);
indeedPostingUpdates.publish(context, update);
return RPCResult.of(version);
}
Example: examples/java/src/main/java/io/superdurable/dex/products/jobpost/JobPostingFlow.java
@rpc({
inputCodec: jobInfoCodec,
outputCodec: doubleCodec,
lockAttributes: [updatePostingLock.lock()],
})
public update(context: Context, input: JobInfo): RPCResult<number> {
const version = this.updateVersion.get(context) + 1;
this.title.set(context, input.title);
this.jobDescription.set(context, input.description);
this.lastUpdateTimeMillis.set(context, BigInt(Date.now()));
if (input.notes !== undefined && input.notes.length > 0) {
this.notes.set(context, input.notes);
}
this.updateVersion.set(context, version);
const update = {
version,
idempotencyKey: `${context.flowId}:${version}`,
posting: input,
};
this.linkedInPostingUpdates.publish(context, update);
this.indeedPostingUpdates.publish(context, update);
return {
output: version,
};
}
Example: examples/typescript/src/products/job-post/job-post-flow.ts
RpcList::new()
.function_without_input(GET_JOB_POST, Self::get_job_post)
.function(
UPDATE_JOB_POST.lock(UPDATE_POSTING_LOCK.lock()),
Self::update_job_post,
)
let version = UPDATE_VERSION.get_required(context)? + 1;
POST.set(context, replacement.clone())?;
TITLE.set(context, replacement.title.clone())?;
DESCRIPTION.set(context, replacement.description.clone())?;
UPDATE_VERSION.set(context, version)?;
let update = PostingUpdate {
version,
idempotency_key: format!("{}:{version}", context.flow_id()),
posting: replacement,
};
LINKEDIN_POSTING_UPDATES.publish(context, update.clone())?;
INDEED_POSTING_UPDATES.publish(context, update)?;
Ok(RpcResult::new(version))
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.
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
var revision int
err = controller.client.WaitForAttributeMatch(
request.Request.Context(),
flowID,
UpdateVersion,
sdk.AttributeMatchGreaterThan(lastRevision),
&revision,
sdk.WaitForAttributeOptions{},
)
var jobInfo JobInfo
err = controller.client.InvokeRPC(
request.Request.Context(),
flowID,
controller.flow.Get,
nil,
&jobInfo,
)
final int revision = client.waitForAttributeMatch(
workflowId,
flow.updateVersion,
AttributeMatch.greaterThan(lastRevision),
WaitForAttributeOptions.newBuilder().build());
final JobPostingFlow stub = client.newRpcStub(JobPostingFlow.class, workflowId);
final Map<String, Object> response = new LinkedHashMap<String, Object>();
response.put("revision", revision);
response.put("jobInfo", client.invokeRPC(stub::get));
Example: examples/java/src/main/java/io/superdurable/dex/products/jobpost/JobPostingController.java
const revision = await client.waitForAttributeMatch(
workflowId,
jobPostingFlow.updateVersion,
AttributeMatch.greaterThan(lastRevision),
{},
);
const jobInfo = await client.invokeRPC(jobPostingFlow.get, workflowId);
Example: examples/typescript/src/products/job-post/controller.ts
let revision = client.wait_for_attribute_match(
&flow_id,
&UPDATE_VERSION,
AttributeMatch::greater_than(query.last_revision),
WaitForAttributeOptions::new(),
)?;
let job_info: JobPost = client.invoke_rpc_without_input(&flow_id, GET_JOB_POST)?;
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.