v1.2.1

排程器 API

最後更新


本頁列出 core 套件中建立、啟動與停止排程器的 API。
  1. import "github.com/pardnchiu/go-scheduler/core"

New 回傳未匯出的 *cron,其匯出方法可直接呼叫;需要儲存時以 := 推導型別。

Config


  1. type Config struct {
  2. Location *time.Location
  3. }

欄位說明
Location排程時區;nil 時為 time.Local

New


  1. func New(c Config) (*cron, error)

建立排程器並初始化最小堆、解析器、依賴子系統與 logger。目前實作的 error 恆為 nil。

Start


  1. func (c *cron) Start()

啟動依賴 Worker 池與事件迴圈,並為已註冊任務計算下次觸發時間。已啟動時為 no-op。

Stop


  1. func (c *cron) Stop() context.Context

停止事件迴圈並等待依賴 Worker 結束,回傳在進行中無依賴任務結束後 cancel 的 context。
  1. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  2. defer cancel()
  3. select {
  4. case <-c.Stop().Done():
  5. case <-ctx.Done():
  6. log.Println("shutdown timeout")
  7. }

註冊任務見 任務 API,前置任務型別見 依賴 API。
EN