---
title: "求职者沟通"
description: "在求职者决定是否接受机会期间，让雇主和求职者保持同步。"
canonical_url: "https://docs.superdurable.io/zh-Hans/examples/job-seeker-engagement/"
source: "docs/i18n/zh-Hans/docusaurus-plugin-content-docs/current/use-cases/job-seeker-engagement.mdx"
locale: "zh-Hans"
language: "python"
---

> 供 AI Agent 使用的全部页面索引：https://docs.superdurable.io/zh-Hans/llms.txt

# 求职者沟通

本页代码为 Python。其他语言：[Go](https://docs.superdurable.io/zh-Hans/examples/job-seeker-engagement.go.md) · [Java](https://docs.superdurable.io/zh-Hans/examples/job-seeker-engagement.java.md) · [TypeScript](https://docs.superdurable.io/zh-Hans/examples/job-seeker-engagement.typescript.md) · [Rust](https://docs.superdurable.io/zh-Hans/examples/job-seeker-engagement.rust.md)

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

**SDK：** Python · Go · TypeScript · Java · Rust

**展示：**[静态并行 Step](https://docs.superdurable.io/zh-Hans/design-patterns/parallel/static-parallel-steps.md)、[提醒](https://docs.superdurable.io/zh-Hans/design-patterns/durable-timer/reminder.md)、[带截止时间的决定](https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/decision-with-deadline.md)、[等待 Attribute 匹配](https://docs.superdurable.io/zh-Hans/design-patterns/responsive-update/wait-for-attribute-match.md)。

**Flow 图：EngagementFlow**

**Step 列表**

1. `Initialize`（起始 Step）：Store the engagement and start the timeout, reminder, and notification paths. 下一步：`NotifyExternalSystem`、`ProcessTimeout`，或 `Reminder`。 写入 `EmployerId`、`EngagementStatus`、`JobSeekerId`、`LastUpdateTimeMillis`、`notes`。
2. `NotifyExternalSystem`：Tell the external system about the engagement status. 下一步：这个分支结束。 读取 `EmployerId`、`JobSeekerId`。
3. `ProcessTimeout`：Wait for completion or the 60-day timeout, then report the final status. 下一步：Flow 完成。
4. `RecordReminderOptOut`：Record the reminder opt-out in the notes under the status lock. 下一步：这个分支结束。 读取 `EngagementStatus`；锁定 `EngagementStatus`。
5. `Reminder`：Remind the job seeker until they respond or opt out. 下一步：这个分支结束、`RecordReminderOptOut`，或 `Reminder`（再次）。 读取 `EngagementStatus`、`JobSeekerId`、`OptOutReminder`。

**RPC 列表**

- `accept`：读取 `EngagementStatus`；锁定 `EngagementStatus`；发布到 `CompleteProcess`。
- `decline`：读取 `EngagementStatus`；锁定 `EngagementStatus`。
- `describe`
- `opt_out`：发布到 `OptOutReminder`。

例子: [examples/python/dex_examples/products/engagement/engagement_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/products/engagement/engagement_flow.py)

上图和下面的说明适用于 Python、Go、Java 和 TypeScript 示例。Rust 示例运行一个更短的 Flow，见 [Rust 示例](https://docs.superdurable.io/zh-Hans/examples/job-seeker-engagement.md#rust-example)。

<a id="product-requirements"></a>

## 产品需求

<a id="产品需求"></a>

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

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

<a id="flow-design"></a>

## Flow 设计

<a id="flow-设计"></a>

**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 之后才返回，所以启动后立刻读取就能看到保存的双方信息。

真实的邮件或招聘系统调用需要[幂等键](https://docs.superdurable.io/zh-Hans/concepts/step.md#idempotency-keys)，因为被中断的 Step 可能再次运行。示例没有传：它的示例服务只打印调用。

<a id="core-implementation"></a>

## 核心实现

<a id="核心实现"></a>

每个标签页声明 Flow 和它的 Step，再展示两个 `AnyOf` 等待。Python 标签页还展示提醒循环和加锁的退订 Step。

**Python**

```python
class EngagementFlow(Flow[EngagementInput]):
    employer_id = Attribute("EmployerId", str)
    job_seeker_id = Attribute("JobSeekerId", str)
    # dex:indexed-attribute attribute-key:EngagementStatus index-key:keyword1 index-type:keyword value-type:string description:"Current engagement status"
    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,
        )
```

```python
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)
```

```python
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)


# dex:group group-id:reminders group-label:"Reminders"
# dex:explanation text:"Record the reminder opt-out in the notes under the status lock."
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](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/products/engagement/engagement_flow.py)

<a id="rust-example"></a>

## 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 已经关闭。

<a id="run-it"></a>

## 运行

<a id="demo"></a>

<a id="演示"></a>

按[在 playground 里运行一个示例](https://docs.superdurable.io/zh-Hans/install.md#run-an-example-from-the-playground)启动一种语言的示例。在[示例 playground](https://github.com/superdurable/dex/tree/main/examples/playground) 中打开 **Job seeker engagement**。对于 Python、Go、Java 或 TypeScript，调用 **Start**，再用返回的 Flow ID 依次调用 **Opt out of reminder**、**Decline** 和 **Accept**。对于 Rust，按 [Rust 示例](https://docs.superdurable.io/zh-Hans/examples/job-seeker-engagement.md#rust-example)操作。**Describe** 显示状态和备注。**List / search** 执行一个搜索查询：`keyword1 = 'Accepted'` 查找已接受的运行，默认的 `ExecutionStatus="Running"` 查找正在运行的运行。

**Accept** 之后，这条消息唤醒 `ProcessTimeout`。**Reminder** 在下一次 Timer 触发时结束后，Flow 以 **done** 完成。在 Dex Web 中打开 **Events**。**Timeline** 显示接受消息，**Graph** 显示各个 Step 的执行。截图来自 Go 示例。

[![Dex Web Timeline 展示从退订提醒到接受机会的求职者沟通运行](https://docs.superdurable.io/img/use-cases/job-seeker-engagement-timeline.png)](https://docs.superdurable.io/zh-Hans/img/use-cases/job-seeker-engagement-timeline.png)

[![Dex Web Graph 展示从退订提醒到接受机会的求职者沟通运行](https://docs.superdurable.io/img/use-cases/job-seeker-engagement-graph.png)](https://docs.superdurable.io/zh-Hans/img/use-cases/job-seeker-engagement-graph.png)

<a id="next"></a>

## 下一步

<a id="下一步"></a>

- [Deal DSL](https://docs.superdurable.io/zh-Hans/examples/deal-dsl.md)
- [提醒](https://docs.superdurable.io/zh-Hans/design-patterns/durable-timer/reminder.md)
- [带截止时间的决定](https://docs.superdurable.io/zh-Hans/design-patterns/external-interaction/decision-with-deadline.md)
- [响应式更新](https://docs.superdurable.io/zh-Hans/design-patterns.md#responsive-update)
- [等待 Attribute 匹配](https://docs.superdurable.io/zh-Hans/design-patterns/responsive-update/wait-for-attribute-match.md)
