Skip to main content

Scheduled Tasks

This section will introduce how to use scheduled tasks.

Introduction

The scheduling task component is roughly divided into two categories. One type executes according to time intervals. This type of task is implemented using the standard library time.Ticker. The other type executes using Linux Crontab expressions. This type encapsulates the robfig/cron library under the hood. The scheduling task provides developers with simple and easy-to-use syntactic sugar.

Usage

Interval-based Tasks

main.go
import (
"github.com/keepchen/go-sail/v3/schedule"
)

func main() {
ch := make(chan, struct{})
task := func() {
fmt.Println("taskName...")
}
schedule.NewJob("taskName", task).EverySecond()
<-ch //waiting...
}

Here are the commonly used syntactic sugar methods:

MethodDescription
EveryExecute once every specified interval
EverySecondExecute once per second
EveryFiveSecondsExecute once every 5 seconds
EveryTenSecondsExecute once every 10 seconds
EveryFifteenSecondsExecute once every 15 seconds
EveryTwentySecondsExecute once every 20 seconds
EveryThirtySecondsExecute once every 30 seconds
EveryMinuteExecute once per minute
EveryFiveMinutesExecute once every 5 minutes
EveryTenMinutesExecute once every 10 minutes
EveryFifteenMinutesExecute once every 15 minutes
EveryTwentyMinutesExecute once every 20 minutes
EveryThirtyMinutesExecute once every 30 minutes
HourlyExecute once per hour
EveryFiveHoursExecute once every 5 hours
EveryTenHoursExecute once every 10 hours
EveryTwentyHoursExecute once every 20 hours
DailyExecute once per day (every 24 hours)
WeeklyExecute once per week (every 7 days)
MonthlyExecute once per month (every 30 days)
YearlyExecute once per year (every 365 days)

Linux Crontab-style Tasks

main.go
import (
"github.com/keepchen/go-sail/v3/schedule"
)

func main() {
ch := make(chan, struct{})
task := func() {
fmt.Println("taskName...")
}
schedule.NewJob("taskName", task).RunAt("* * * * *")
<-ch //waiting...
}

Delayed (One-time) Tasks

main.go
import (
"time"
"github.com/keepchen/go-sail/v3/schedule"
)

func main() {
ch := make(chan, struct{})
task := func() {
fmt.Println("taskName...")
}
schedule.NewJob("taskName", task).RunAfter(5*time.Second)
<-ch //waiting...
}

Preventing Task Overlap

The WithoutOverlapping() method prevents tasks from running simultaneously. It's particularly useful when your service has multiple replicas and you want to ensure task uniqueness.

warning

This feature requires initializing a Redis connection instance in advance, whether in standalone or cluster mode. Otherwise, it will panic.

main.go
import (
"github.com/keepchen/go-sail/v3/schedule"
)

func main() {
ch := make(chan, struct{})
task := func() {
fmt.Println("taskName...")
}
schedule.NewJob("taskName", task).WithoutOverlapping().EverySecond()
<-ch //waiting...
}
note

Note that your task should not be entirely a goroutine, otherwise unexpected situations may occur and WithoutOverlapping() will fail.

Here's an example of what NOT to do:

main.go
import (
"github.com/keepchen/go-sail/v3/schedule"
)

func main() {
ch := make(chan, struct{})
task := func() {
go func() {
...
fmt.Println("taskName...")
...
}
}
schedule.NewJob("taskName", task).WithoutOverlapping().EverySecond()
<-ch //waiting...
}

Cancellation

Tasks that have not yet started or are not running will be cancelled directly. Tasks that are currently running will wait for completion and then will not start again.

main.go
import (
"time"
"github.com/keepchen/go-sail/v3/schedule"
)

func main() {
ch := make(chan, struct{})
task := func() {
fmt.Println("taskName...")
}
cancel := schedule.NewJob("taskName", task).WithoutOverlapping().EverySecond()
time.Sleep(5*time.Second)
cancel()
<-ch //waiting...
}

Crontab Expression

Usage

main.go
import (
"github.com/keepchen/go-sail/v3/schedule"
)

func main() {
ch := make(chan, struct{})
task := func() {
fmt.Println("taskName...")
}
schedule.NewJob("taskName", task).WithoutOverlapping().RunAt(schedule.EveryFiveMinute)
<-ch //waiting...
}

The following lists the built-in expression constants

ConstantDescription
EveryMinuteAt 0 seconds of every minute
EveryFiveMinuteAt 0 seconds of every 5 minutes
EveryTenMinuteAt 0 seconds of every 10 minutes
EveryFifteenMinuteAt 0 seconds of every 15 minutes
EveryTwentyMinuteAt 0 seconds of every 20 minutes
EveryThirtyMinuteAt 0 seconds of every 30 minutes
EveryFortyFiveMinuteAt 0 seconds of every 45 minutes
FirstDayOfMonthAt 00:00 on the first day of every month
LastDayOfMonthAt 00:00 on the last day of every month
FirstDayOfWeekAt 00:00 on Monday (first day of week)
LastDayOfWeekAt 00:00 on Sunday (last day of week)
TenClockAtWeekdayAt 10:00 on every weekday (Monday through Friday)
TenClockAtWeekendAt 10:00 on every weekend (Saturday and Sunday)
HourlyBetween9And17ClockAtWeekdayEvery hour from 9:00 to 17:00 on weekdays (Monday through Friday)
HalfHourlyBetween9And17ClockAtWeekdayEvery 30 minutes from 9:00 to 17:00 on weekdays (Monday through Friday)