Golang AGScheduler動態(tài)持久化任務調(diào)度的強大庫使用實例
正文
以前一直使用 Python 的任務調(diào)度庫 APScheduler(支持任務持久化,支持多種存儲方式),但由于沒有找到與它功能和使用方式類似的 Golang 庫,所以模仿 APScheduler 3.x 寫了個簡易版本的 AGScheduler。
AGScheduler
Advanced Golang Scheduler (AGScheduler) 是一款適用于 Golang 的任務調(diào)度程序,支持多種調(diào)度方式,動態(tài)更改和持久化任務
警示
該庫處于實驗階段,不建議用于生產(chǎn)環(huán)境
鏈接 https://github.com/kwkwc/agscheduler
特性
支持三種調(diào)度方式
- [x] 一次性執(zhí)行
- [x] 間隔執(zhí)行
- [x] Cron 式調(diào)度
支持多種任務存儲方式
使用
package main
import (
"fmt"
"log/slog"
"time"
"github.com/kwkwc/agscheduler"
"github.com/kwkwc/agscheduler/stores"
)
func printMsg(j agscheduler.Job) {
slog.Info(fmt.Sprintf("Run %s %s\n", j.Name, j.Args))
}
func main() {
agscheduler.RegisterFuncs(printMsg)
store := &stores.MemoryStore{}
scheduler := &agscheduler.Scheduler{}
scheduler.SetStore(store)
job := agscheduler.Job{
Name: "Job",
Type: agscheduler.TYPE_INTERVAL,
Timezone: "UTC",
Func: printMsg,
Args: []any{"arg1", "arg2", "arg3"},
Interval: 2 * time.Second,
}
jobId := scheduler.AddJob(job)
job, _ = scheduler.GetJob(jobId)
slog.Info(fmt.Sprintf("Scheduler add %s %s.\n\n", job.Name, job))
scheduler.Start()
slog.Info("Scheduler Start.\n\n")
select {}
}注冊函數(shù)
由于 golang 無法序列化函數(shù),所以 scheduler.Start() 之前需要使用 RegisterFuncs 注冊函數(shù)
致謝
以上就是Golang AGScheduler動態(tài)持久化任務調(diào)度的強大庫使用實例的詳細內(nèi)容,更多關于Go AGScheduler任務調(diào)度的資料請關注腳本之家其它相關文章!
相關文章
Go語言中使用 buffered channel 實現(xiàn)線程安全的 pool
這篇文章主要介紹了Go語言中使用 buffered channel 實現(xiàn)線程安全的 pool,因為Go語言自帶的sync.Pool并不是很好用,所以自己實現(xiàn)了一線程安全的 pool,需要的朋友可以參考下2014-10-10
Golang?Template實現(xiàn)自定義函數(shù)的操作指南
這篇文章主要為大家詳細介紹了Golang如何利用Template實現(xiàn)自定義函數(shù)的操作,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下2025-02-02

