-
Notifications
You must be signed in to change notification settings - Fork 915
/
backgroundworkers.go
67 lines (52 loc) · 1.51 KB
/
backgroundworkers.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
package backgroundworkers
import (
"context"
"net/http"
"sync"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"goji.io"
)
var HTTPAddr = config.RegisterOption("yagpdb.bgworker.http_server_addr", "Backgroundn worker http server address", "localhost:5004")
var RESTServerMuxer *goji.Mux
var restServer *http.Server
var logger = common.GetFixedPrefixLogger("bgworkers")
type BackgroundWorkerPlugin interface {
RunBackgroundWorker()
StopBackgroundWorker(wg *sync.WaitGroup)
}
func RunWorkers() {
common.ServiceTracker.RegisterService(common.ServiceTypeBGWorker, "Background worker", "", nil)
RESTServerMuxer = goji.NewMux()
for _, p := range common.Plugins {
if bwc, ok := p.(BackgroundWorkerPlugin); ok {
logger.Info("Running background worker: ", p.PluginInfo().Name)
go bwc.RunBackgroundWorker()
}
}
go runWebserver()
}
func StopWorkers(wg *sync.WaitGroup) {
logger.Info("Shutting down http server...")
if restServer != nil {
restServer.Shutdown(context.Background())
}
for _, p := range common.Plugins {
if bwc, ok := p.(BackgroundWorkerPlugin); ok {
logger.Info("Stopping background worker: ", p.PluginInfo().Name)
wg.Add(1)
go bwc.StopBackgroundWorker(wg)
}
}
}
func runWebserver() {
logger.Info("Starting bgworker http server on ", HTTPAddr)
restServer := &http.Server{
Handler: RESTServerMuxer,
Addr: HTTPAddr.GetString(),
}
err := restServer.ListenAndServe()
if err != nil {
logger.WithError(err).Error("Failed starting http server")
}
}