/
main.go
61 lines (53 loc) · 1.19 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
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/hi20160616/robotclick/configs"
"golang.org/x/sync/errgroup"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g, ctx := errgroup.WithContext(ctx)
// Jobs
for _, s := range configs.V.Snippets.Ss {
s := s
j := NewJob(&s)
g.Go(func() error {
log.Printf("Job [\"%s\"] start\n", j.s.FileName)
return j.Start(ctx)
})
g.Go(func() error {
defer log.Printf("Job [\"%s\"] stop done.\n", j.s.FileName)
<-ctx.Done() // wait for stop signal
log.Printf("Job [\"%s\"] stop now...\n", j.s.FileName)
return j.Stop(ctx) // TODO: stop cron and ever
})
}
// Graceful stop
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
g.Go(func() error {
select {
case sig := <-sigs:
fmt.Println()
log.Printf("signal caught: %s, ready to quit...", sig.String())
cancel()
case <-ctx.Done():
return ctx.Err()
}
return nil
})
if err := g.Wait(); err != nil {
if !errors.Is(err, context.Canceled) {
log.Printf("not canceled by context: %s", err)
} else {
log.Println(err)
}
}
}