SoFunction
Updated on 2025-03-05

Golang AGScheduler's powerful library usage examples for dynamic persistent task scheduling

text

I used to use Python's task scheduling library APScheduler (supports task persistence and supports multiple storage methods), but since I did not find a Golang library similar to its functions and usage methods, I wrote a simple version of AGScheduler by imitating APScheduler.

AGScheduler

Advanced Golang Scheduler (AGScheduler) is a task scheduler for Golang that supports multiple scheduling methods, dynamically changing and persisting tasks

Warning

This library is in the experimental stage and is not recommended for use in production environments

Link/kwkwc/agscheduler

characteristic

  • Supports three scheduling methods

    • [x] Execute at one time
    • [x] Interval execution
    • [x] Cron-style scheduling
  • Supports multiple task storage methods

    • [x] Memory
    • [x] GROM(Any GROM-supported RDBMS can run)
    • [x] Redis
    • [x] MongoDB

use

package main
import (
    "fmt"
    "log/slog"
    "time"
    "/kwkwc/agscheduler"
    "/kwkwc/agscheduler/stores"
)
func printMsg(j ) {
    (("Run %s %s\n", , ))
}
func main() {
    (printMsg)
    store := &{}
    scheduler := &{}
    (store)
    job := {
        Name:     "Job",
        Type:     agscheduler.TYPE_INTERVAL,
        Timezone: "UTC",
        Func:     printMsg,
        Args:     []any{"arg1", "arg2", "arg3"},
        Interval: 2 * ,
    }
    jobId := (job)
    job, _ = (jobId)
    (("Scheduler add %s %s.\n\n", , job))
    ()
    ("Scheduler Start.\n\n")
    select {}
}

Register a function

Since golang cannot serialize functions,()Need to use it beforeRegisterFuncsRegister a function

Complete example

Acknowledgements

APScheduler

The above is the detailed content of the powerful library usage example of Golang AGScheduler dynamic persistent task scheduling. For more information about Go AGScheduler task scheduling, please follow my other related articles!