以前一直使用 Python 的任务调度库 APScheduler(支持任务持久化,支持多种存储方式),但由于没有找到与它功能和使用方式类似的 Golang 库,所以模仿 APScheduler 3.x 写了个简易版本的 AGScheduler。

AGScheduler

Advanced Golang Scheduler (AGScheduler) 是一款适用于 Golang 的任务调度库,支持多种调度类型,支持动态更改和持久化作业,支持作业队列,支持作业结果回收,支持事件监听,支持远程调用,支持集群

特性

  • 支持三种调度类型

    • [x] 一次性执行
    • [x] 间隔执行
    • [x] Cron 式调度
  • 支持多种作业存储方式

  • 支持多种作业队列

  • 支持多种作业结果后端

    • [x] Memory (不支持集群模式)
    • [x] GORM (任何 GORM 支持的 RDBMS 都能运行)
    • [x] MongoDB
  • 支持事件监听

    • [x] 调度器事件
    • [x] 作业事件
  • 支持远程调用

  • 支持集群

    • [x] 远程工作节点
    • [x] 调度器高可用 (实验性)

架构

framework.png

链接

https://github.com/agscheduler/agscheduler

安装

go get -u github.com/agscheduler/agscheduler

使用

package main

import (
    "context"
    "fmt"
    "log/slog"
    "time"

    "github.com/agscheduler/agscheduler"
    "github.com/agscheduler/agscheduler/stores"
)

func printMsg(ctx context.Context, j agscheduler.Job) (result string) {
    slog.Info(fmt.Sprintf("Run job `%s` %s\n\n", j.FullName(), j.Args))
    return
}

func main() {
    agscheduler.RegisterFuncs(
        agscheduler.FuncPkg{Func: printMsg},
    )

    scheduler := &agscheduler.Scheduler{}

    store := &stores.MemoryStore{}
    scheduler.SetStore(store)

    job1 := agscheduler.Job{
        Name:     "Job1",
        Type:     agscheduler.JOB_TYPE_INTERVAL,
        Interval: "2s",
        Timezone: "UTC",
        Func:     printMsg,
        Args:     map[string]any{"arg1": "1", "arg2": "2", "arg3": "3"},
    }
    job1, _ = scheduler.AddJob(job1)
    slog.Info(fmt.Sprintf("%s.\n\n", job1))

    job2 := agscheduler.Job{
        Name:     "Job2",
        Type:     agscheduler.JOB_TYPE_CRON,
        CronExpr: "*/1 * * * *",
        Timezone: "Asia/Shanghai",
        FuncName: "main.printMsg",
        Args:     map[string]any{"arg4": "4", "arg5": "5", "arg6": "6", "arg7": "7"},
    }
    job2, _ = scheduler.AddJob(job2)
    slog.Info(fmt.Sprintf("%s.\n\n", job2))

    job3 := agscheduler.Job{
        Name:     "Job3",
        Type:     agscheduler.JOB_TYPE_DATETIME,
        StartAt:  "2023-09-22 07:30:08",
        Timezone: "America/New_York",
        Func:     printMsg,
        Args:     map[string]any{"arg8": "8", "arg9": "9"},
    }
    job3, _ = scheduler.AddJob(job3)
    slog.Info(fmt.Sprintf("%s.\n\n", job3))

    jobs, _ := scheduler.GetAllJobs()
    slog.Info(fmt.Sprintf("Scheduler get all jobs %s.\n\n", jobs))

    scheduler.Start()

    select {}
}

注册函数

由于 golang 无法序列化函数,所以 scheduler.Start() 之前需要使用 RegisterFuncs 注册函数

队列

mq := &queues.MemoryQueue{}
broker := &agscheduler.Broker{
    Queues: map[string]agscheduler.QueuePkg{
        "default": {
            Queue:   mq,
            Workers: 2,
        },
    },
}

scheduler.SetBroker(broker)

结果回收

backend := &backends.MemoryBackend{}
recorder := &agscheduler.Recorder{Backend: backend}

scheduler.SetRecorder(recorder)

job, _ = scheduler.AddJob(job)
records, _ := recorder.GetRecords(job.Id)

事件监听

func jobCallback(ep agscheduler.EventPkg) {
    slog.Info(fmt.Sprintf("Event code: `%d`, job `%s`.\n\n", ep.Event, ep.JobId))
}

......

listener := &agscheduler.Listener{
    Callbacks: []agscheduler.CallbackPkg{
        {
            Callback: jobCallback,
            Event:    agscheduler.EVENT_JOB_ADDED | agscheduler.EVENT_JOB_DELETED,
        },
    },
}

scheduler.SetListener(listener)

gRPC

// Server
grservice := services.GRPCService{
    Scheduler: scheduler,
    Address:   "127.0.0.1:36360",
    // PasswordSha2: "xxxxxx",
}
grservice.Start()

// Client
conn, _ := grpc.NewClient("127.0.0.1:36360", grpc.WithTransportCredentials(insecure.NewCredentials()))
client := pb.NewSchedulerClient(conn)
client.AddJob(ctx, job)

HTTP

// Server
hservice := services.HTTPService{
    Scheduler: scheduler,
    Address:   "127.0.0.1:36370",
    // PasswordSha2: "xxxxxx",
}
hservice.Start()

// Client
mJob := map[string]any{...}
bJob, _ := json.Marshal(mJob)
resp, _ := http.Post("http://127.0.0.1:36370/scheduler/job", "application/json", bytes.NewReader(bJob))

集群

// Main Node
cnMain := &agscheduler.ClusterNode{
    Endpoint:     "127.0.0.1:36380",
    EndpointGRPC: "127.0.0.1:36360",
    EndpointHTTP: "127.0.0.1:36370",
    Queue:        "default",
}
schedulerMain.SetClusterNode(ctx, cnMain)
cserviceMain := &services.ClusterService{Cn: cnMain}
cserviceMain.Start()

// Worker Node
cnNode := &agscheduler.ClusterNode{
    EndpointMain: "127.0.0.1:36380",
    Endpoint:     "127.0.0.1:36381",
    EndpointGRPC: "127.0.0.1:36361",
    EndpointHTTP: "127.0.0.1:36371",
    Queue:        "worker",
}
schedulerNode.SetClusterNode(ctx, cnNode)
cserviceNode := &services.ClusterService{Cn: cnNode}
cserviceNode.Start()

集群 HA (高可用,实验性)

// HA 需要满足以下条件:
//
// 1. 集群中 HA 节点的数量必须为奇数
// 2. 所有 HA 节点都需要连接到同一个存储(不包含 MemoryStore)
// 3. ClusterNode 的 Mode 属性需要设置为 `HA`
// 4. HA 主节点必须先启动

// Main HA Node
cnMain := &agscheduler.ClusterNode{..., Mode: "HA"}

// HA Node
cnNode1 := &agscheduler.ClusterNode{..., Mode: "HA"}
cnNode2 := &agscheduler.ClusterNode{..., Mode: "HA"}

// Worker Node
cnNode3 := &agscheduler.ClusterNode{...}

Base API

gRPC FunctionHTTP MethodHTTP Path
GetInfoGET/info
GetFuncsGET/funcs

Scheduler API

gRPC FunctionHTTP MethodHTTP Path
AddJobPOST/scheduler/job
GetJobGET/scheduler/job/:id
GetAllJobsGET/scheduler/jobs
UpdateJobPUT/scheduler/job
DeleteJobDELETE/scheduler/job/:id
DeleteAllJobsDELETE/scheduler/jobs
PauseJobPOST/scheduler/job/:id/pause
ResumeJobPOST/scheduler/job/:id/resume
RunJobPOST/scheduler/job/run
ScheduleJobPOST/scheduler/job/schedule
StartPOST/scheduler/start
StopPOST/scheduler/stop

Broker API

gRPC FunctionHTTP MethodHTTP Path
GetQueuesGET/broker/queues

Recorder API

gRPC FunctionHTTP MethodHTTP Path
GetRecordsGET/recorder/records/:job_id
GetAllRecordsGET/recorder/records
DeleteRecordsDELETE/recorder/records/:job_id
DeleteAllRecordsDELETE/recorder/records

Cluster API

gRPC FunctionHTTP MethodHTTP Path
GetNodesGET/cluster/nodes

示例

完整示例

开发

# 克隆代码
git clone git@github.com:agscheduler/agscheduler.git

# 工作目录
cd agscheduler

# 安装依赖
make install

# 启动 CI 服务
make up-ci-services

# 运行检查
make check-all

Cli

cargo install agscheduler-cli

Web

docker run --rm -p 8080:80 ghcr.io/agscheduler/agscheduler-web:latest

致谢

APScheduler

simple-raft


kwkwc
52 声望3 粉丝