forked from la5nta/pat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
43 lines (37 loc) · 821 Bytes
/
schedule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.
package main
import (
"log"
"time"
"github.com/gorhill/cronexpr"
)
type Job struct {
expr *cronexpr.Expression
cmd string
next time.Time
}
func scheduleLoop() {
jobs := make([]*Job, 0, len(config.Schedule))
for exprStr, cmd := range config.Schedule {
expr := cronexpr.MustParse(exprStr)
jobs = append(jobs, &Job{
expr,
cmd,
expr.Next(time.Now()),
})
}
go func() {
for range time.Tick(time.Second) {
for _, j := range jobs {
if time.Now().Before(j.next) {
continue
}
log.Printf("Executing scheduled command '%s'...", j.cmd)
execCmd(j.cmd)
j.next = j.expr.Next(time.Now())
}
}
}()
}