/
main.go
126 lines (100 loc) · 3.05 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
126
package main
import (
"errors"
properties "github.com/dmotylev/goproperties"
"github.com/ogier/pflag"
r "github.com/robxu9/reservoir"
"log"
"os"
"path/filepath"
)
const (
MAJOR_VERSION = 1
MINOR_VERSION = 1
PATCH_VERSION = 0
CONFIG_WORKER_DIR = "workers"
)
// Flags
var helpFlag bool
var versionFlag bool
var verboseFlag bool
var workerDirFlag string
func init() {
pflag.BoolVar(&helpFlag, "help", false, "Show help.")
pflag.BoolVar(&versionFlag, "version", false, "Shows the current version of Reservoir.")
pflag.BoolVar(&verboseFlag, "verbose", false, "Be very verbose.")
pflag.StringVar(&workerDirFlag, "workers", CONFIG_WORKER_DIR, "Use directory for worker configuration files.")
}
func main() {
pflag.Parse()
if helpFlag {
pflag.PrintDefaults()
return
}
if versionFlag {
log.Printf("This is version %d.%d-%d.", MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
return
}
log.Printf("Starting up version %d.%d-%d...\n", MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
// Read configuration
_, err := os.Open(workerDirFlag)
if err != nil {
if err == os.ErrNotExist || err == os.ErrInvalid {
err = os.Mkdir(workerDirFlag, os.ModeDir)
if err != nil {
log.Panicf("Could not create worker dir: %s", err)
}
} else {
log.Panicf("Error accessing worker directory: %s", err)
}
}
log.Printf("Reading worker configuration:\n")
fileWalkErr := filepath.Walk(workerDirFlag, visit)
if fileWalkErr != nil {
log.Panicf("There seems to have been a problem reading configuration: %s\n", fileWalkErr)
}
log.Printf("Starting up scheduler...\n")
r.Scheduler_Run()
if r.SchedulerStatus == 0 {
log.Panicf("Scheduler failed to start!\n")
}
log.Printf("Ping and Queue up Workers...\n")
// Establish Ping GoRoutine and add them to Scheduler
// (Should this just be connecting the workers? This way we set them as
// connected and have reservoir ping constantly until we disconnect.)
log.Printf("Start the API interface...\n")
// Start the API web interface here (goweb)?
log.Printf("Now active.\n")
mainBlock := make(chan bool)
<-mainBlock // Won't happen, just used to wait the main thread b/c we need to keep the prog active.
}
func visit(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() { // Ignore directories
return nil
}
log.Printf("\tLoading configuration %s...", path)
props, err := properties.Load(path)
if err != nil {
return err
}
workername := props.GetString("name", "")
workerhost := props.GetString("host", "")
workerprocesses := props.GetUint("subworkers", 2)
if workername == "" || workerhost == "" || workerprocesses <= 0 {
return errors.New("Cannot initialise worker: \"name\" and/or \"host\" is empty, or \"subworkers\" <= 0!")
}
var counter uint64
for counter = 0; counter < workerprocesses; counter++ {
r.Workers.Set(r.WorkerID{workername, counter},
&r.WorkerConnection{
workerhost,
nil,
"",
false,
})
}
return nil
}