-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (87 loc) · 2.37 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
package main
import (
"blumfield/internal/blumfield"
"context"
"flag"
"math/rand/v2"
"os"
"os/signal"
"syscall"
"time"
"github.com/sirupsen/logrus"
)
func main() {
cfg := flag.String("config", "config", "config file without extension (example: -config=dev)")
flag.Parse()
logger := &logrus.Logger{
Out: os.Stdout,
Level: logrus.DebugLevel,
Formatter: &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
ForceColors: true,
PadLevelText: true,
},
}
// Create Blumfield instance
blum, err := blumfield.NewBlumfield(logger, *cfg)
if err != nil {
logger.Fatal("Error creating Blumfield instance: ", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Graceful shutdown on signal
go func() {
sig := <-sigChan
logger.Info("Received signal: ", sig)
cancel()
}()
if blum.Config.Settings.Daemon {
runDaemonMode(ctx, blum, logger)
} else {
runOnce(ctx, blum, logger)
}
logger.Info("Application has shut down! Goodbye!")
}
func runDaemonMode(ctx context.Context, blum *blumfield.Blumfield, logger *logrus.Logger) {
logger.Info("Daemon mode is enabled, running tasks perpetually with 8-hour intervals...")
for {
select {
case <-ctx.Done():
logger.Info("Shutdown requested, exiting daemon loop.")
return
default:
if err := blum.Start(ctx); err != nil {
if err == context.Canceled {
logger.Info("Task execution interrupted by shutdown request.")
return
}
logger.Error("Error during task execution: ", err)
}
logger.Info("Waiting for 8 hours before the next task execution...")
sleepDuration := 8*time.Hour + time.Duration(rand.IntN(15)+2)*time.Minute
timer := time.NewTimer(sleepDuration)
select {
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
logger.Info("Shutdown requested during sleep, exiting loop.")
return
case <-timer.C:
logger.Info("8 hours have passed, continuing to the next execution...")
}
}
}
}
func runOnce(ctx context.Context, blum *blumfield.Blumfield, logger *logrus.Logger) {
if err := blum.Start(ctx); err != nil {
if err == context.Canceled {
logger.Info("Task execution interrupted by shutdown request.")
} else {
logger.Error("Error during task execution: ", err)
}
}
}