-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (105 loc) · 2.47 KB
/
main.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/figglewatts/gitupgitout/internal/ctxcron"
"github.com/figglewatts/gitupgitout/pkg/app"
"github.com/figglewatts/gitupgitout/pkg/logging"
"github.com/robfig/cron/v3"
)
func runApp(log logging.StdLogger) (err error) {
var (
verboseFlag = flag.Bool("verbose", false, "enable verbose logging")
configFlag = flag.String(
"config", "gugo.yaml", "path to config file",
)
concurrencyFlag = flag.Int(
"concurrency", 0, "number of mirrors to process at once",
)
cronFlag = flag.String("cron", "", "run on a cron schedule")
runBeforeCronFlag = flag.Bool(
"run-before-cron", false, "run anyway before first scheduled cron",
)
)
flag.Parse()
if *verboseFlag {
log.LogVerbose = true
}
// setup graceful shutdown
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM,
syscall.SIGQUIT,
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
<-signalChan
cancel()
close(signalChan)
}()
configFile, err := os.Open(*configFlag)
if err != nil {
return fmt.Errorf("opening config file: %w", err)
}
defer func(configFile *os.File) {
deferErr := configFile.Close()
if err == nil && deferErr != nil {
err = deferErr
}
}(configFile)
gugoApp, err := app.New(
configFile, log, app.WithConcurrency(*concurrencyFlag),
)
if err != nil {
return fmt.Errorf("initialising app: %w", err)
}
if *cronFlag == "" {
return gugoApp.Run(ctx)
} else {
schedule, err := cron.ParseStandard(*cronFlag)
if err != nil {
return fmt.Errorf("parsing cron schedule: %w", err)
}
log.Print("running on cron schedule: %v", *cronFlag)
cronScheduler := ctxcron.NewCronScheduler()
_ = cronScheduler.AddFunc(
*cronFlag, func() {
err := gugoApp.Run(ctx)
if err != nil {
log.Error("%s", err)
}
nextExecution := schedule.Next(time.Now())
log.Print(
"awaiting next cron execution at %s",
nextExecution.Format(time.RFC1123),
)
},
)
if *runBeforeCronFlag {
err := gugoApp.Run(ctx)
if err != nil {
log.Error("%s", err)
}
}
nextExecution := schedule.Next(time.Now())
log.Print(
"awaiting next cron execution at %s",
nextExecution.Format(time.RFC1123),
)
cronScheduler.Run(ctx)
return nil
}
}
func main() {
log := logging.NewStd()
if err := runApp(log); err != nil {
log.Error("%s", err)
os.Exit(1)
}
}