Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cron-tz #1369

Open
wants to merge 17 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/advanced-guide/using-cron/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,33 @@ func main() {
}
```

Gofr also allows to specify timezone against which the schedule will be evaluated.

### Example

```go
package main

import (
"time"

"gofr.dev/pkg/gofr"
)

func main() {
app := gofr.New()

// tzdata will be taking care of DST
estTimezone, _ := time.LoadLocation("America/New_York")

// Run the cron job every day at 7AM EST
app.AddCronJob("0 7 * * *", "", func(ctx *gofr.Context) {
ctx.Logger.Infof("current time is %v", time.Now())
}, gofr.WithTimezone(estTimezone))

app.Run()
}
```


> #### Check out the example on how to add cron jobs in GoFr: [Visit GitHub](https://github.com/gofr-dev/gofr/blob/main/examples/using-cron-jobs/main.go)
21 changes: 14 additions & 7 deletions pkg/gofr/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ type CronFunc func(ctx *Context)
type Crontab struct {
// contains unexported fields
ticker *time.Ticker
location *time.Location
jobs []*job
container *container.Container

mu sync.RWMutex
}

type job struct {
sec map[int]struct{}
min map[int]struct{}
hour map[int]struct{}
day map[int]struct{}
month map[int]struct{}
dayOfWeek map[int]struct{}
sec, min, hour, day, month, dayOfWeek map[int]struct{}

name string
fn CronFunc
Expand All @@ -62,22 +58,33 @@ type tick struct {
}

// NewCron initializes and returns new cron tab.
func NewCron(cntnr *container.Container) *Crontab {
func NewCron(cntnr *container.Container, options ...func(*Crontab)) *Crontab {
c := &Crontab{
ticker: time.NewTicker(time.Second),
location: time.Local,
container: cntnr,
jobs: make([]*job, 0),
}

for _, option := range options {
option(c)
}

go func() {
for t := range c.ticker.C {
t = t.In(c.location)
c.runScheduled(t)
}
}()

return c
}

// WithTimezone is a functional option for NewCron to specify timezone against which the cron schedule will be evaluated,
func WithTimezone(location *time.Location) func(*Crontab) {
return func(c *Crontab) { c.location = location }
}

// this will compile the regex once instead of compiling it each time when it is being called.
var (
matchSpaces = regexp.MustCompile(`\s+`)
Expand Down
33 changes: 33 additions & 0 deletions pkg/gofr/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,36 @@ func TestCron_parsePart(t *testing.T) {
})
}
}

func TestCron_WithTimezone(t *testing.T) {
est, _ := time.LoadLocation("America/New_York")
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved

tests := []struct {
name string
options []func(*Crontab)
expected *time.Location
}{
{
name: "Factory without options",
expected: time.Local,
},
{
name: "Factory with UTC timezone option",
options: []func(crontab *Crontab){WithTimezone(time.UTC)},
expected: time.UTC,
},
{
name: "Factory with EST/EDT timezone option",
options: []func(crontab *Crontab){WithTimezone(est)},
expected: est,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := NewCron(nil, test.options...)
got.ticker.Stop()
assert.Equal(t, test.expected, got.location)
})
}
}
4 changes: 2 additions & 2 deletions pkg/gofr/gofr.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,9 @@ func (a *App) UseMiddlewareWithContainer(middlewareHandler func(c *container.Con
// AddCronJob registers a cron job to the cron table.
// The cron expression can be either a 5-part or 6-part format. The 6-part format includes an
// optional second field (in beginning) and others being minute, hour, day, month and day of week respectively.
func (a *App) AddCronJob(schedule, jobName string, job CronFunc) {
func (a *App) AddCronJob(schedule, jobName string, job CronFunc, options ...func(*Crontab)) {
if a.cron == nil {
a.cron = NewCron(a.container)
a.cron = NewCron(a.container, options...)
}

if err := a.cron.AddJob(schedule, jobName, job); err != nil {
Expand Down