跳到主要内容

求职者沟通

查看 Markdown

在求职者决定是否接受机会期间,让雇主和求职者保持同步。

SDK: Python · Go · TypeScript · Java · Rust

展示:静态并行 Step、提醒、带截止时间的决定、等待 Attribute 匹配。

上图和下面的说明适用于 Python、Go、Java 和 TypeScript 示例。Rust 示例运行一个更短的 Flow,见 Rust 示例。

产品需求

流程接收雇主 ID、求职者 ID 和备注。它必须:

  1. 状态为 Initiated 时,每五秒提醒一次求职者。生产环境使用更长的间隔。
  2. 允许求职者退订提醒。机会仍然开放。
  3. 状态为 Initiated 时允许求职者拒绝。拒绝后仍可接受。
  4. 状态为 Initiated 或 Declined 时允许求职者接受。接受后流程以 done 完成。
  5. 60 天内没有收到接受时,以 timeout 完成。
  6. 在流程开始、每次决定之后和流程结束时通知招聘系统。
  7. 显示当前状态和备注,并能按状态查找流程。

Flow 设计

Initialize 保存双方信息、状态 Initiated 和备注,然后启动三个并行分支:

  • ProcessTimeout 用 AnyOf 等待 60 天 Timer 或 CompleteProcess Channel 上的消息。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。

engagement_flow.pyPython
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,
)
engagement_flow.pyPython
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)
engagement_flow.pyPython
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

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 示例。

Dex Web Timeline 展示从退订提醒到接受机会的求职者沟通运行

Dex Web Graph 展示从退订提醒到接受机会的求职者沟通运行

下一步