Skip to content

Commit

Permalink
core/tasks/ivr/cron: fix missing location in call to Time.In
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Sep 29, 2023
1 parent f1ba44f commit f87515e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
39 changes: 31 additions & 8 deletions core/tasks/ivr/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ const (
cancelIVRCallsLock = "cancel_ivr_calls"
)

var location *time.Location
var locationTimezone *time.Location

func init() {
mailroom.RegisterCron("retry_ivr_calls", time.Minute, false, func(ctx context.Context, rt *runtime.Runtime) error {
var err error
location, err = time.LoadLocation(rt.Config.IVRTimeZone)
if err != nil {
if err := SetupLocationTimezone(rt.Config.IVRTimeZone); err != nil {
return err
}
currentHour := time.Now().In(location).Hour()
currentHour := time.Now().In(locationTimezone).Hour()
if currentHour >= rt.Config.IVRStartHour && currentHour < rt.Config.IVRStopHour {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(rt.Config.IVRRetryTimeout))
defer cancel()
Expand All @@ -47,7 +45,10 @@ func init() {
mailroom.RegisterCron(clearIVRLock, time.Hour, false, ClearStuckChannelConnections)

mailroom.RegisterCron(changeMaxConnNightLock, time.Minute*10, false, func(ctx context.Context, rt *runtime.Runtime) error {
currentHour := time.Now().In(location).Hour()
if err := SetupLocationTimezone(rt.Config.IVRTimeZone); err != nil {
return err
}
currentHour := time.Now().In(locationTimezone).Hour()
if currentHour >= rt.Config.IVRStopHour || currentHour < rt.Config.IVRStartHour {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
defer cancel()
Expand All @@ -57,7 +58,10 @@ func init() {
})

mailroom.RegisterCron(changeMaxConnDayLock, time.Minute*10, false, func(ctx context.Context, rt *runtime.Runtime) error {
currentHour := time.Now().In(location).Hour()
if err := SetupLocationTimezone(rt.Config.IVRTimeZone); err != nil {
return err
}
currentHour := time.Now().In(locationTimezone).Hour()
if currentHour >= rt.Config.IVRStartHour && currentHour < rt.Config.IVRStopHour {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
defer cancel()
Expand All @@ -67,7 +71,10 @@ func init() {
})

mailroom.RegisterCron(cancelIVRCallsLock, time.Hour*1, false, func(ctx context.Context, rt *runtime.Runtime) error {
currentHour := time.Now().In(location).Hour()
if err := SetupLocationTimezone(rt.Config.IVRTimeZone); err != nil {
return err
}
currentHour := time.Now().In(locationTimezone).Hour()
if currentHour == rt.Config.IVRCancelCronStartHour {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*20)
defer cancel()
Expand Down Expand Up @@ -361,3 +368,19 @@ type Channel struct {
Config map[string]interface{} `db:"config" json:"config,omitempty"`
IsActive bool `db:"is_active" json:"is_active,omitempty"`
}

func SetupLocationTimezone(timezone string) error {
if locationTimezone != nil {
return nil
}
var err error
locationTimezone, err = time.LoadLocation(timezone)
if err != nil {
return err
}
return nil
}

func GetLocationTimezone() *time.Location {
return locationTimezone
}
17 changes: 17 additions & 0 deletions core/tasks/ivr/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ivr_test
import (
"encoding/json"
"testing"
"time"

"github.com/nyaruka/gocommon/dbutil/assertdb"
"github.com/nyaruka/mailroom/core/ivr"
Expand Down Expand Up @@ -248,3 +249,19 @@ func TestUpdateMaxChannelsConnection(t *testing.T) {
assertdb.Query(t, db, `SELECT COUNT(*) FROM channels_channelconnection WHERE contact_id = $1 AND status = $2`,
testdata.Cathy.ID, models.ConnectionStatusWired).Returns(1)
}

func TestSetupLocation(t *testing.T) {
err := ivrtasks.SetupLocationTimezone(".invalid.")
assert.Error(t, err)
assert.ErrorContains(t, err, "unknown time zone .invalid.")

location := ivrtasks.GetLocationTimezone()
assert.Nil(t, location)
timezone := "Asia/Kolkata"
ivrtasks.SetupLocationTimezone(timezone)
location = ivrtasks.GetLocationTimezone()
assert.NotNil(t, location)
expectedLocation, err := time.LoadLocation("Asia/Kolkata")
assert.NoError(t, err)
assert.Equal(t, expectedLocation, location)
}

0 comments on commit f87515e

Please sign in to comment.