-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (69 loc) · 1.87 KB
/
main.go
File metadata and controls
81 lines (69 loc) · 1.87 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/MathWebSearch/mws-cron/src/cron"
"github.com/MathWebSearch/mws-cron/src/docker"
)
func main() {
if shouldTrigger {
mainTrigger()
} else {
mainCron()
}
}
func mainCron() {
fmt.Printf("Cron: Using pidfile %s\n", pidFile)
// write the pidfile
if cron.WritePid(pidFile) != nil {
panic("Can't write pidfile")
}
// clear the pid on exit
defer cron.ClearPid(pidFile)
// and run cron
minute := 1 * time.Minute
cron.RunCron(cronLine, func(reason cron.Reason, retry func()) {
docker.UpdateMWS(mwsLabel, retry, &minute)
}, true, shouldRunInitial)
}
func mainTrigger() {
// load the pidfile
var proc *os.Process
var err error
if proc, err = cron.ReadPid(pidFile); err != nil {
panic("Can't load pidfile")
}
// and send the signal
cron.SignalCron(proc)
}
var pidFile string
var mwsLabel string
var cronLine string
var shouldTrigger bool
var shouldRunInitial bool
func init() {
flag.BoolVar(&shouldTrigger, "trigger", false, "Trigger manually running a cron job in running instance")
flag.BoolVar(&shouldRunInitial, "initial", true, "Run re-indexing on startup")
flag.StringVar(&pidFile, "pidfile", "", "Pidfile to use")
flag.StringVar(&cronLine, "schedule", getenv("MWS_CRON_SCHEDULE", "@midnight"), "Cronline representing time to run job on, use '@never' to disable cronjobs, can also be set using the 'MWS_CRON_SCHEDULE' environment variable")
flag.StringVar(&mwsLabel, "label", "org.mathweb.mwsd", "Label for MathWebSearch daemon")
flag.Parse()
// if pidfile is empty, use a file int he current directory
if pidFile == "" {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
pidFile = filepath.Join(dir, "mws-cron.pid")
}
}
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}