runner

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package runner 是 ofd-server 的常驻编排层:取任务、跑转换、记结果、发通知。

三个组件各管一段,runner 只负责把它们接起来并控制并发:

jobstore   任务与通知的持久化队列
convertersvc  一次转换的执行
notify     状态变化的投递

并发分两条通道:fast 与 heavy。两者共享一个任务池,但各有独立的 worker 数与 令牌桶。原因是 heavy 任务会拉起 LibreOffice 或 Chrome,单个就能吃掉几百 MB 内存并占住一个进程;和 fast 任务抢同一组令牌时,几个 Office 文档就能让整个 服务对轻量请求失去响应。

Index

Constants

View Source
const DefaultBackoff = 5 * time.Second

DefaultBackoff 是任务失败后的重试退避基数,按 Attempt 指数放大。

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// FastWorkers 是快速通道的并发数。
	FastWorkers int
	// HeavyWorkers 是重通道的并发数。
	HeavyWorkers int
	// PollInterval 是队列为空时的轮询间隔。
	PollInterval time.Duration
	// NotifyWorkers 是通知投递的并发数。
	NotifyWorkers int
	// JobTimeout 是单次转换的整体超时,0 表示不限。
	JobTimeout time.Duration
	// MaxJobAttempts 是任务自动重试次数,0 表示只试一次。
	MaxJobAttempts int
	// Backoff 是重试退避基数,0 时用 DefaultBackoff。
	Backoff time.Duration
	// PruneInterval 是清理终态任务的间隔,小于等于 0 表示不清理。
	PruneInterval time.Duration
	// Retention 是终态任务的保留时长。
	Retention time.Duration
	// NotifyBatch 是一次最多取多少条到期通知,0 时用 32。
	NotifyBatch int
}

Config 是 runner 的配置。

type Converter

type Converter interface {
	Run(ctx context.Context, spec convertersvc.Spec) (convertersvc.Result, error)
}

Converter 是 runner 依赖的转换能力。*convertersvc.Service 直接满足它。

提成接口是为了能注入受控实现做并发压测:通道隔离、退避时序、关闭时的 在途任务处置,这些都要有"能拖多久就拖多久"的慢任务才测得出来,而真实的 OFD 转换快到没法构造这种场景。

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner 驱动任务与通知。

func New

func New(store *jobstore.Store, convert Converter, deliverer *notify.Deliverer, cfg Config, log *slog.Logger) *Runner

New 构造 runner。

func (*Runner) Start

func (r *Runner) Start(ctx context.Context) error

Start 启动 worker 并做崩溃恢复。

Recover 与 RecoverInflight 必须先跑:进程上次退出时留在 running / inflight 的 记录,对外部而言都是"卡住了",不重新排队就永远不会有结果。

func (*Runner) Stop

func (r *Runner) Stop()

Stop 停止所有 worker 并等待它们退出。已取到的任务会被写回失败态, 因为 ctx 已经取消,转换不可能再完成。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL