-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
81 lines (66 loc) · 1.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
package core
import (
"database/sql"
"fmt"
"github.com/Freeaqingme/GoDaemonSkeleton/log"
"os"
"sync"
)
var (
Config = *new(config)
hostname, _ = os.Hostname()
Log *log.Logger
cg = &Cluegetter{modules: make([]Module, 0)}
)
type Cluegetter struct {
Config config
Log *log.Logger
Redis RedisClient
instance uint
instanceMu sync.Mutex
modulesMu sync.RWMutex
modules []Module
}
func (cg *Cluegetter) Instance() uint {
cg.instanceMu.Lock()
defer cg.instanceMu.Unlock()
if cg.instance == 0 {
if cg.Config.ClueGetter.Instance == "" {
cg.Log.Fatal("No instance was set")
}
err := cg.Rdbms().QueryRow("SELECT id from instance WHERE name = ?", cg.Config.ClueGetter.Instance).
Scan(&instance)
if err != nil {
cg.Log.Fatal(fmt.Sprintf("Could not retrieve instance '%s' from database: %s",
cg.Config.ClueGetter.Instance, err))
}
Log.Notice("Instance name: %s. Id: %d", cg.Config.ClueGetter.Instance, instance)
cg.instance = instance
}
return cg.instance
}
func (cg *Cluegetter) Rdbms() *sql.DB {
if Rdbms == nil {
rdbmsStart()
}
return Rdbms
}
func CluegetterRecover(funcName string) {
if Config.ClueGetter.Exit_On_Panic {
return
}
r := recover()
if r == nil {
return
}
Log.Error("Panic caught in %s(). Recovering. Error: %s", funcName, r)
}
func InitCg() *Cluegetter {
cg.Config = Config
cg.Log = Log
cg.instance = instance
for _, module := range cg.modules {
module.SetCluegetter(cg)
}
return cg
}