Go Task Scheduler (Golang): cron, gocron, and asynq

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Task Scheduler in Go (Golang): from cron to asynq



In modern server-side development with Go (Golang), there is often a need to execute code on a schedule or with a delay. Cache clearing, sending notifications, report generation — all of this requires a reliable task scheduler. In this article, we will thoroughly examine three popular approaches: the standard cron package, the lightweight gocron library, and the distributed queue asynq. You will learn which tool to choose for your project on PythonLib.



1. Standard cron for Go



The github.com/robfig/cron/v3 library is the de facto standard for scheduling recurring tasks in Go. It uses the familiar cron expression syntax (5 fields) and supports seconds, time zones, and panic recovery.



Installation and basic example



go get github.com/robfig/cron/v3


Example of a scheduler that runs a task every 30 seconds:



package main

import ( "fmt" "github.com/robfig/cron/v3")

func main() { c := cron.New() // Add a task with a cron expression: every 30 seconds _, err := c.AddFunc("*/30 * * * * *", func() { fmt.Println("Task executed:", time.Now()) }) if err != nil { panic(err) } c.Start() // Block the main goroutine select {}}


Key features:

  • Support for standard cron expressions (5 or 6 fields with seconds)
  • Automatic recovery from panic in a task
  • Ability to set a time zone via cron.WithLocation()
  • Logging via cron.WithLogger()


Advanced usage: chained jobs



c := cron.New(cron.WithSeconds())

// Task with a name for managementid, _ := c.AddFunc("@every 5m", func() { fmt.Println("Runs every 5 minutes")})

// Remove task by IDc.Remove(id)

// Stop the schedulerc.Stop()


The library is ideal for simple periodic tasks within a single process. However, for distributed systems (multiple Go server replicas), an external lock is required to prevent the task from running simultaneously on all nodes.



2. Gocron — a lightweight scheduler with chains



github.com/go-co-op/gocron offers a more modern API without cron expressions. Instead of strings, you use method chains. This is especially convenient for beginners.



Example with gocron



package main

import ( "fmt" "time" "github.com/go-co-op/gocron")

func main() { s := gocron.NewScheduler(time.UTC) // Task runs every 2 minutes _, _ = s.Every(2).Minutes().Do(func() { fmt.Println("2 minutes have passed") }) // Task with a delay after start _, _ = s.Every(1).Day().At("10:30").Do(func() { fmt.Println("Daily at 10:30") }) s.StartBlocking()}


Advantages of gocron:

  • Human-readable API: .Every(10).Seconds()
  • Built-in support for distributed locks via Redis (optional)
  • Ability to set a limit on the number of runs
  • Global and local error handlers


If you need a simple solution for a microservice without external dependencies — gocron is an excellent choice. It is lighter than cron and does not require knowledge of cron expression syntax.



3. Asynq — a distributed delayed task queue



github.com/hibiken/asynq is not just a scheduler, but a full-fledged queue system based on Redis. It allows you to delay tasks, set priorities, retry failed tasks, and track their status via a web interface.



Asynq architecture



Asynq is

Recommendations