求职者沟通
查看 Markdown在求职者决定是否接受机会期间,让雇主和求职者保持同步。
SDK: Python · Go · TypeScript · Java · Rust
展示:静态并行 Step、提醒、带截止时间的决定、等待 Attribute 匹配。
上图和下面的说明适用于 Python、Go、Java 和 TypeScript 示例。Rust 示例运行一个更短的 Flow,见 Rust 示例。
产品需求
流程接收雇主 ID、求职者 ID 和备注。它必须:
- 状态为 Initiated 时,每五秒提醒一次求职者。生产环境使用更长的间隔。
- 允许求职者退订提醒。机会仍然开放。
- 状态为 Initiated 时允许求职者拒绝。拒绝后仍可接受。
- 状态为 Initiated 或 Declined 时允许求职者接受。接受后流程以 done 完成。
- 60 天内没有收到接受时,以 timeout 完成。
- 在流程开始、每次决定之后和流程结束时通知招聘系统。
- 显示当前状态和备注,并能按状态查找流程。
Flow 设计
Initialize 保存双方信息、状态 Initiated 和备注,然后启动三个并行分支:
ProcessTimeout用AnyOf等待 60 天 Timer 或CompleteProcessChannel 上的消息。Accept RPC 发布这条消息。随后ProcessTimeout以 done 或 timeout 完成 Flow。- Reminder 等待五秒 Timer 或
OptOutReminder上的消息。状态不再是 Initiated 时,它结束。否则,收到退订时它移到RecordReminderOptOut;Timer 触发时它发送一封邮件,再移回自身。 NotifyExternalSystem报告流程开始。Accept 和 Decline 会带着新状态再次启动它。
两个等待使用不同的 Channel,所以退订不会接受、拒绝或结束这个机会。
状态、备注和更新时间都是 Attribute。Accept、Decline 和 RecordReminderOptOut 共享 EngagementStatus 上的锁,所以每次状态判断和备注更新都能看到上一次的结果。退订不改变状态,只追加一条备注。RPC 发现当前状态不允许变更时会失败,也不会启动通知。Reminder 发送邮件时不持有锁。
ProcessTimeout 用 GracefulComplete 完成,它会等其他分支结束。接受之后,仍在运行的 Reminder 在下一次 Timer 触发时结束。
启动端点在 Attribute 匹配等待看到雇主 ID Attribute 之后才返回,所以启动后立刻读取就能看到保存的双方信息。
真实的邮件或招聘系统调用需要幂等键,因为被中断的 Step 可能再次运行。示例没有传:它的示例服务只打印调用。
核心实现
每个标签页声明 Flow 和它的 Step,再展示两个 AnyOf 等待。Python 标签页还展示提醒循环和加锁的退订 Step。
class EngagementFlow(Flow[EngagementInput]):
employer_id = Attribute("EmployerId", str)
job_seeker_id = Attribute("JobSeekerId", str)
engagement_status = Attribute(
"EngagementStatus",
Status,
AttributeIndex(IndexType.KEYWORD, STATUS_SEARCH_KEY),
)
last_update_timestamp = Attribute("LastUpdateTimeMillis", int)
notes = Attribute("notes", str)
opt_out_reminder = Channel[None]("OptOutReminder", type(None))
complete_process = Channel[None]("CompleteProcess", type(None))
def __init__(self, service: MyDependencyService) -> None:
self.service = service
self.initialize = Initialize(self)
self.process_timeout = ProcessTimeout(self)
self.reminder = Reminder(self)
self.record_reminder_opt_out = RecordReminderOptOut(self)
self.notify_external_system = NotifyExternalSystem(self)
def get_steps(self) -> StepList[EngagementInput]:
return StepList.start_step(self.initialize).other_steps(
self.process_timeout,
self.reminder,
self.record_reminder_opt_out,
self.notify_external_system,
)
def get_persistence_schema(self) -> PersistenceSchema:
return PersistenceSchema.of(
self.employer_id,
self.job_seeker_id,
self.engagement_status,
self.last_update_timestamp,
self.notes,
self.opt_out_reminder,
self.complete_process,
)
class ProcessTimeout(Step[None]):
def __init__(self, flow: "EngagementFlow") -> None:
self.flow = flow
def wait_for(self, context: Context, input: None) -> Wait:
return Wait.any_of(
Timer.by_duration(timedelta(days=60)),
self.flow.complete_process.for_one(),
)
def execute(self, context: Context, input: None) -> StepDecision:
description = self.flow.describe_engagement(context)
result = "timeout"
if description.current_status == Status.ACCEPTED:
result = "done"
self.flow.service.update_external_system(
f"engagement from employer {description.employer_id} "
f"to job seeker {description.job_seeker_id} "
f"finished with status {description.current_status}"
)
return graceful_complete(result)
class Reminder(Step[None]):
def __init__(self, flow: "EngagementFlow") -> None:
self.flow = flow
def wait_for(self, context: Context, input: None) -> Wait:
return Wait.any_of(
Timer.by_duration(timedelta(seconds=5)),
self.flow.opt_out_reminder.for_one(),
)
def execute(self, context: Context, input: None) -> StepDecision:
status = self.flow.engagement_status.get(context)
if status is not Status.INITIATED:
return dead_end()
if self.flow.opt_out_reminder.results(context):
return go_to(RecordReminderOptOut, None)
self.flow.service.send_email(
self.flow.job_seeker_id.get(context),
"Reminder: please respond",
"Please respond to the engagement.",
)
return go_to(Reminder, None)
class RecordReminderOptOut(Step[None]):
def __init__(self, flow: "EngagementFlow") -> None:
self.flow = flow
def get_step_options(self) -> StepOptions:
return StepOptions(execute_lock_attributes=(self.flow.engagement_status.lock(),))
def execute(self, context: Context, input: None) -> StepDecision:
status = self.flow.engagement_status.get(context)
if status is not Status.INITIATED:
return dead_end()
self.flow.update_status(context, status, "user opted out of reminders")
return dead_end()
例子: examples/python/dex_examples/products/engagement/engagement_flow.py
type EngagementFlow struct {
dex.FlowDefaults
service service.MyService
}
func NewEngagementFlow(applicationService service.MyService) *EngagementFlow {
return &EngagementFlow{service: applicationService}
}
func (flow *EngagementFlow) GetSteps() []dex.StepDef {
return []dex.StepDef{
dex.DefineStartStep(initializeStep{}),
dex.DefineStep(processTimeout{service: flow.service}),
dex.DefineStep(reminderStep{service: flow.service}),
dex.DefineStep(recordReminderOptOut{}),
dex.DefineStep(notifyExternalSystem{service: flow.service}),
}
}
func (*EngagementFlow) GetPersistenceSchema() dex.PersistenceSchema {
return dex.PersistenceSchema{
Attributes: []dex.AttributeDef{
EmployerID,
JobSeekerID,
EngagementStatus,
LastUpdateTimestamp,
Notes,
},
Channels: []dex.ChannelDef{OptOutReminder, CompleteProcess},
}
}
func (processTimeout) WaitFor(
dex.Context,
dex.None,
) (*dex.Wait, error) {
return dex.AnyOf(
dex.Timer(60*24*time.Hour),
CompleteProcess.ForOne(),
), nil
}
func (reminderStep) WaitFor(
dex.Context,
dex.None,
) (*dex.Wait, error) {
return dex.AnyOf(
dex.Timer(5*time.Second),
OptOutReminder.ForOne(),
), nil
}
@Component
public class EngagementFlow implements Flow<EngagementInput> {
public static final String STATUS_SEARCH_KEY = "keyword1";
public final Attribute<String> employerId = Attribute.define("EmployerId", String.class);
public final Attribute<String> jobSeekerId = Attribute.define("JobSeekerId", String.class);
public final Attribute<Status> engagementStatus = Attribute.define(
"EngagementStatus",
Status.class,
new AttributeIndex(AttributeIndex.Type.KEYWORD, STATUS_SEARCH_KEY));
public final Attribute<Long> lastUpdateTimestamp =
Attribute.define("LastUpdateTimeMillis", Long.class);
public final Attribute<String> notes = Attribute.define("notes", String.class);
public final Channel<Void> optOutReminder = Channel.define("OptOutReminder", Void.class);
public final Channel<Void> completeProcess = Channel.define("CompleteProcess", Void.class);
private final MyDependencyService service;
private final Initialize initialize = new Initialize();
private final ProcessTimeout processTimeout = new ProcessTimeout();
private final Reminder reminder = new Reminder();
private final RecordReminderOptOut recordReminderOptOut = new RecordReminderOptOut();
private final NotifyExternalSystem notifyExternalSystem = new NotifyExternalSystem();
public EngagementFlow(final MyDependencyService service) {
this.service = service;
}
@Override
public StepList<EngagementInput> getSteps() {
return StepList.startStep(initialize)
.otherSteps(processTimeout, reminder, recordReminderOptOut, notifyExternalSystem);
}
@Override
public PersistenceSchema getPersistenceSchema() {
return PersistenceSchema.of(
employerId,
jobSeekerId,
engagementStatus,
lastUpdateTimestamp,
notes,
optOutReminder,
completeProcess);
}
final class ProcessTimeout implements Step<Void> {
@Override
public Wait waitFor(final Context context, final Void input) {
return Wait.anyOf(
Timer.byDuration(Duration.ofDays(60)),
completeProcess.forOne());
}
final class Reminder implements Step<Void> {
@Override
public Wait waitFor(final Context context, final Void input) {
return Wait.anyOf(
Timer.byDuration(Duration.ofSeconds(5)),
optOutReminder.forOne());
}
例子: examples/java/src/main/java/io/superdurable/dex/products/engagement/EngagementFlow.java
const engagementStatus = new Attribute("EngagementStatus", statusCodec, {
type: IndexType.KEYWORD,
indexKey: STATUS_SEARCH_KEY,
});
export class EngagementFlow implements Flow<EngagementInput> {
public readonly employerId = new Attribute("EmployerId", stringCodec);
public readonly jobSeekerId = new Attribute("JobSeekerId", stringCodec);
public readonly engagementStatus = engagementStatus;
public readonly lastUpdateTimestamp = new Attribute("LastUpdateTimeMillis", int64Codec);
public readonly notes = new Attribute("notes", optionalCodec(stringCodec));
public readonly initialize = new Initialize(this);
public readonly processTimeout = new ProcessTimeout(this);
public readonly reminder = new Reminder(this);
public readonly recordReminderOptOut = new RecordReminderOptOut(this);
public readonly notifyExternalSystem = new NotifyExternalSystem(this);
public constructor(public readonly service: MyDependencyService = myDependencyService) {}
public getFlowType(): string {
return "EngagementFlow";
}
public getSteps() {
return StepList.startStep(this.initialize).otherSteps(
this.processTimeout,
this.reminder,
this.recordReminderOptOut,
this.notifyExternalSystem,
);
}
public getPersistenceSchema(): PersistenceSchema {
return {
attributes: [
this.employerId,
this.jobSeekerId,
this.engagementStatus,
this.lastUpdateTimestamp,
this.notes,
],
channels: [optOutReminder, completeProcess],
};
}
class ProcessTimeout implements Step<void> {
public waitFor(_context: Context, _input: void): Wait {
return Wait.anyOf(
Timer.byDuration(PROCESS_TIMEOUT_MS),
completeProcess.forOne(),
);
}
class Reminder implements Step<void> {
public waitFor(_context: Context, _input: void): Wait {
return Wait.anyOf(Timer.byDuration(REMINDER_MS), optOutReminder.forOne());
}
例子: examples/typescript/src/products/engagement/engagement-flow.ts
#[derive(Default)]
pub struct EngagementFlow {
start: Start,
wait_for_decision: WaitForDecision,
notify_external_system: NotifyExternalSystem,
}
impl Flow for EngagementFlow {
type StartInput = EngagementRequest;
fn steps(&self) -> StepList<'_, Self::StartInput> {
StepList::start(&self.start)
.and(&self.wait_for_decision)
.and(&self.notify_external_system)
}
fn persistence(&self) -> PersistenceSchema {
PersistenceSchema::new()
.attribute(&STATUS)
.attribute(&EMPLOYER_ID)
.channel(&DECISION)
}
fn rpcs(&self) -> RpcList<Self> {
RpcList::new()
.function_without_input(DESCRIBE_ENGAGEMENT, Self::describe)
.procedure(ACCEPT_ENGAGEMENT, Self::accept)
.procedure(DECLINE_ENGAGEMENT, Self::decline)
.procedure_without_input(OPT_OUT_ENGAGEMENT, Self::opt_out)
}
}
impl Step for WaitForDecision {
type Input = EngagementRequest;
fn wait_for(&self, _context: &mut Context, _input: Self::Input) -> HandlerResult<Wait> {
Ok(Wait::any_of([
DECISION.for_one(),
Timer::by_duration(Duration::from_secs(86_400)),
]))
}
fn execute(&self, context: &mut Context, _input: Self::Input) -> HandlerResult<StepDecision> {
let resolved = DECISION
.condition_results(context)?
.into_iter()
.next()
.unwrap_or(EngagementStatus {
status: "reminder-due".into(),
notes: String::new(),
});
STATUS.set(context, resolved.clone())?;
Ok(StepDecision::go_to(&NotifyExternalSystem, resolved.status))
}
}
Rust 示例
Rust 示例提供同样的六个 playground 调用,但运行一个更短的 Flow,以便集成测试更快结束。一个 Step 等待 24 小时 Timer 或决策 Channel 上的一条消息。Accept、Decline 和 Opt out of reminder 各自向这个 Channel 发布一条消息。第一条消息就做出决定:这个 Step 把它写为状态,NotifyExternalSystem 记录一个事件,然后 Flow 完成。如果 Timer 先触发,状态写为 reminder-due。只有这个 Step 写状态,所以不需要锁。Rust Flow 不发送提醒,也没有 done 或 timeout 结果。
运行时,调用 Start,再调用一次 Accept。Accept 只发布决定,所以请在 Dex Web 中等待运行关闭,或反复调用 Describe,直到它显示状态 accepted。之后不要再调用任何决定,因为 Flow 已经关闭。
运行
按在 playground 里运行一个示例启动一种语言的示例。在示例 playground 中打开 Job seeker engagement。对于 Python、Go、Java 或 TypeScript,调用 Start,再用返回的 Flow ID 依次调用 Opt out of reminder、Decline 和 Accept。对于 Rust,按 Rust 示例操作。Describe 显示状态和备注。List / search 执行一个搜索查询:keyword1 = 'Accepted' 查找已接受的运行,默认的 ExecutionStatus="Running" 查找正在运行的运行。
Accept 之后,这条消息唤醒 ProcessTimeout。Reminder 在下一次 Timer 触发时结束后,Flow 以 done 完成。在 Dex Web 中打开 Events。Timeline 显示接受消息,Graph 显示各个 Step 的执行。截图来自 Go 示例。

