feat: add timezone
This commit is contained in:
parent
87608426bf
commit
0a6e490673
4 changed files with 25 additions and 3 deletions
|
|
@ -48,12 +48,15 @@ Create a `config.yml` file in the same directory as the binary (or `/etc/scron/`
|
||||||
|
|
||||||
Optionally, you can specify the `weekday` field (0=Sunday, 1=Monday, ..., 6=Saturday) to restrict jobs to specific days of the week.
|
Optionally, you can specify the `weekday` field (0=Sunday, 1=Monday, ..., 6=Saturday) to restrict jobs to specific days of the week.
|
||||||
|
|
||||||
|
You can also optionally specify the `timezone` field to set the timezone for each job. Default is UTC. You can use any timezone supported by Go (e.g., `Europe/Berlin`).
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
jobs:
|
jobs:
|
||||||
- name: "System Health Check"
|
- name: "System Health Check"
|
||||||
minute: "*/5"
|
minute: "*/5"
|
||||||
hour: "*"
|
hour: "*"
|
||||||
weekday: "*"
|
weekday: "*"
|
||||||
|
timezone: "Europe/Berlin"
|
||||||
command: "curl -f http://localhost:8080/health || exit 1"
|
command: "curl -f http://localhost:8080/health || exit 1"
|
||||||
|
|
||||||
- name: "Daily Backup"
|
- name: "Daily Backup"
|
||||||
|
|
|
||||||
18
config.go
18
config.go
|
|
@ -21,6 +21,7 @@ type jobconfig struct {
|
||||||
Hour string `yaml:"hour"`
|
Hour string `yaml:"hour"`
|
||||||
Minute string `yaml:"minute"`
|
Minute string `yaml:"minute"`
|
||||||
Weekday string `yaml:"weekday"`
|
Weekday string `yaml:"weekday"`
|
||||||
|
Timezone string `yaml:"timezone"`
|
||||||
Command string `yaml:"command"`
|
Command string `yaml:"command"`
|
||||||
Notification string `yaml:"notification,omitempty"`
|
Notification string `yaml:"notification,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
@ -66,8 +67,19 @@ func ReadFromFile(path string) (config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jc jobconfig) MatchCurrentTime(t time.Time) (bool, error) {
|
func (jc jobconfig) MatchCurrentTime(t time.Time) (bool, error) {
|
||||||
|
loc, err := time.LoadLocation("UTC")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if jc.Timezone != "" {
|
||||||
|
loc, err = time.LoadLocation(jc.Timezone)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
fmt.Println("weekday", jc.Weekday)
|
fmt.Println("weekday", jc.Weekday)
|
||||||
matchWeekDay, err := matchTime(jc.Weekday, int(t.Weekday()))
|
matchWeekDay, err := matchTime(jc.Weekday, int(t.In(loc).Weekday()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +87,7 @@ func (jc jobconfig) MatchCurrentTime(t time.Time) (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
matchHour, err := matchTime(jc.Hour, t.Hour())
|
matchHour, err := matchTime(jc.Hour, t.In(loc).Hour())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
@ -83,7 +95,7 @@ func (jc jobconfig) MatchCurrentTime(t time.Time) (bool, error) {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
matchMinute, err := matchTime(jc.Minute, t.Minute())
|
matchMinute, err := matchTime(jc.Minute, t.In(loc).Minute())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,5 @@ jobs:
|
||||||
minute: "*"
|
minute: "*"
|
||||||
hour: "*"
|
hour: "*"
|
||||||
weekday: "*"
|
weekday: "*"
|
||||||
|
timezone: "Europe/Berlin" # optional, default UTC
|
||||||
command: "echo 'Juhu'"
|
command: "echo 'Juhu'"
|
||||||
|
|
|
||||||
|
|
@ -168,6 +168,12 @@ func TestMatchJobTime(t *testing.T) {
|
||||||
expRes: false,
|
expRes: false,
|
||||||
expErrMsg: "cant parse time pattern",
|
expErrMsg: "cant parse time pattern",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "pass-with-timezone",
|
||||||
|
job: jobconfig{Hour: "17", Minute: "*", Weekday: "*", Timezone: "Africa/Nairobi"},
|
||||||
|
expRes: true,
|
||||||
|
expErrMsg: "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tts {
|
for _, tt := range tts {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue