SuperAPI AI 工具集

sync-job-expert

数据与分析

查看项目

更新于 10-15 23:44

技能介绍

用途:为 Frappe Tweaks 的 Sync Job 框架提供专家指导,该框架是基于队列的 DocTypes 数据同步系统。

核心概念:Sync Job Type 是定义同步配置的模板(源/目标 doctype、队列、超时、重试设置);Sync Job 是跟踪执行、状态、错误和差异的单个任务实例;Controller 是实现同步逻辑的 Python 模块(Bypass 或 Standard 模式)。

快速开始:创建时进入 Sync Job Type > New,设置名称和 doctypes,保存后生成 controller;入队参数与示例见 references/enqueueing.md;实现步骤见 references/implementation.md。

实现模式:Single Target(Standard)一对一同步,实现 get_target_document() 与 update_target_doc();Multiple Targets(Standard)一对多同步,实现 get_multiple_target_documents() 与 update_target_doc();Bypass 完全控制,实现 execute()。

状态流转:Pending → Queued → Started → Finished/Failed/Skipped/Relayed/No Target;Canceled 为手动取消;Failed 在 retry_count < max_retries 时自动重试。

关键方法:sync_job.get_context()、get_source_document()、get_trigger_document()、get_target_document();标志位 insert_enabled/update_enabled/delete_enabled、update_without_changes_enabled、dry_run。

配置:Sync Job Type 默认值可被单个作业覆盖,包括 Queue(default、short、long)、Timeout(秒)、Retry Delay(分钟)、Max Retries、Verbose Logging、Context、Trigger Tracking。

Dry Run 模式:使用 enqueue_sync_job(sync_job_type="SAP Customer Sync", source_document_name="CUST-00001", dry_run=True) 可在不保存的情况下填充 diff_summary。

排错:作业停留在 Queued 需检查 RQ worker(bench worker)与队列名;反复失败需检查 error_message、文档存在性与模块路径;无变更检测可设置 update_without_changes_enabled=True;模块未找到需确认 controller 存在、命名正确并运行 bench migrate。

最佳实践:简单映射用 Standard 模式,复杂操作用 Bypass;同步前验证源/目标;优雅处理缺失目标;用 context 传运行时参数;为复杂操作设置合适超时;重操作使用特定队列;用 frappe.log_error() 记录重要决策;测试重试逻辑。

源码参考:tweaks/utils/sync_job.py、tweaks/tweaks/doctype/sync_job_type/、tweaks/tweaks/doctype/sync_job/、tweaks/tweaks/doctype/sync_job_type/boilerplate/controller._py。