-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
125 lines (96 loc) · 2.88 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
118
119
120
121
122
123
124
125
package main
import (
"context"
_ "expvar"
"flag"
"fmt"
"strings"
"time"
"net/http"
"github.com/gorilla/mux"
"github.com/cyverse-de/async-tasks/database"
"github.com/cyverse-de/go-mod/otelutils"
"github.com/cyverse-de/async-tasks/behaviors/statuschangetimeout"
"github.com/cyverse-de/configurate"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
)
var log = logrus.WithFields(logrus.Fields{
"service": "async-tasks",
"art-id": "async-tasks",
"group": "org.cyverse",
})
const serviceName = "async-tasks"
const otelName = "github.com/cyverse-de/async-tasks"
func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
func makeRouter() *mux.Router {
router := mux.NewRouter()
router.Use(otelmux.Middleware("async-tasks"))
router.Handle("/debug/vars", http.DefaultServeMux)
router.HandleFunc("/", func(writer http.ResponseWriter, r *http.Request) {
fmt.Fprintf(writer, "Hello from async-tasks.\n")
}).Methods("GET")
return router
}
func fixAddr(addr string) string {
if !strings.HasPrefix(addr, ":") {
return fmt.Sprintf(":%s", addr)
}
return addr
}
func main() {
var (
cfgPath = flag.String("config", "/etc/iplant/de/async-tasks.yml", "The path to the config file")
port = flag.String("port", "60000", "The port number to listen on")
err error
cfg *viper.Viper
)
flag.Parse()
if *cfgPath == "" {
log.Fatal("--config must not be the empty string")
}
var tracerCtx, cancel = context.WithCancel(context.Background())
defer cancel()
shutdown := otelutils.TracerProviderFromEnv(tracerCtx, serviceName, func(e error) { log.Fatal(e) })
defer shutdown()
if cfg, err = configurate.Init(*cfgPath); err != nil {
log.Fatal(err.Error())
}
dburi := cfg.GetString("db.uri")
db, err := database.SetupDB(dburi, log)
if err != nil {
log.Fatal(err.Error())
}
defer db.Close()
count, err := db.GetCount(context.Background())
if err != nil {
log.Fatal(err.Error())
}
log.Infof("There are %d async tasks in the database", count)
// Make periodic updater
updater := NewAsyncTasksUpdater(db)
updater.AddBehavior("statuschangetimeout", statuschangetimeout.Processor)
ticker := time.NewTicker(30 * time.Second) // twice a minute means minutely updates behave basically decently, if we need faster we can change this
defer ticker.Stop()
go func() {
for {
t := <-ticker.C
log.Infof("Got periodic timer tick: %s", t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) // long timeout we can use to clear out totally stuck jobs
err := updater.DoPeriodicUpdate(ctx, t, db)
if err != nil {
log.Error(err)
}
cancel()
}
}()
// Make HTTP listeners
router := makeRouter()
app := NewAsyncTasksApp(db, router)
log.Debug(app)
log.Infof("Starting to listen on port %s", *port)
log.Fatal(http.ListenAndServe(fixAddr(*port), router))
}