feat(periodic): 新增主备定时任务 SDK 与调用示例 - #41
Conversation
Co-authored-by: zhangxiangyu52 <zhangxiangyu52@huawei.com>
|
|
|
head_sha: 变更摘要本 PR 新增进程内主备(SingleLeader)周期任务 SDK( 主要改动
|
|
head_sha: 代码审查审查总结本次审查覆盖了全部 11 个变更文件,结论如下: 发现的问题按优先级统计:
总体风险判断: 该 PR 的核心主备选主 + 锁续期机制方向正确、Lua 脚本参数化无注入风险、导入路径与现有 逐文件审查确认:
💬 仅评论 |
|
|
||
| async def _run_forever(self) -> None: | ||
| if self._run_on_start and not self._stopped.is_set(): | ||
| await self._safe_tick(planned_fire=self._now()) |
There was a problem hiding this comment.
head_sha: b772e455905ce072f7bc00174719a0d951bf07ab
🟡 Medium Priority
建议:将 run_on_start 的首轮 tick 包进 try/except Exception,记录日志后继续进入主循环,保证与主循环一致的容错。
| await self._safe_tick(planned_fire=self._now()) | |
| if self._run_on_start and not self._stopped.is_set(): | |
| try: | |
| await self._safe_tick(planned_fire=self._now()) | |
| except Exception: | |
| logger.exception( | |
| "JobRunner run_on_start failed: job=%s instance=%s", | |
| self._name, | |
| self._instance_id, | |
| ) |
| return | ||
| logger.debug("tick lock renewed: key=%s", self._lock_key) | ||
| except asyncio.CancelledError: | ||
| return |
There was a problem hiding this comment.
head_sha: b772e455905ce072f7bc00174719a0d951bf07ab
🟡 Medium Priority
建议:在 _renew_loop 内对 renew_once 的异常做处理,失锁或异常都置 _lost=True 并退出,避免续期任务静默死亡。
| return | |
| async def _renew_loop(self, token: str) -> None: | |
| try: | |
| while True: | |
| await asyncio.sleep(self._renew_interval_sec) | |
| try: | |
| ok = await self.renew_once(token) | |
| except Exception: | |
| self._lost = True | |
| logger.exception( | |
| "tick lock renew error: key=%s token=%s", | |
| self._lock_key, | |
| token, | |
| ) | |
| return | |
| if not ok: | |
| self._lost = True | |
| logger.warning( | |
| "tick lock lost on renew: key=%s token=%s", | |
| self._lock_key, | |
| token, | |
| ) | |
| return | |
| logger.debug("tick lock renewed: key=%s", self._lock_key) | |
| except asyncio.CancelledError: | |
| return |
Paired: GitHub #41 ↔ GitCode !417
What type of PR is this?
/kind feature
Self-checklist:(请自检,在[ ]内打上x,我们将检视你的完成情况,否则会导致pr无法合入)
Summary
新增进程内主备(SingleLeader)周期任务 SDK(
foundation.periodic),对外通过工厂create_single_leader_job创建任务;并提供调用示例applications/periodic_job/example.py。核心能力:
IntervalSchedule)SET NX EX+ 持锁续期 + token 校验释放on_tick(无参回调)