/
config.go
71 lines (61 loc) · 1.78 KB
/
config.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
package server
import (
"fmt"
"runtime"
"time"
"github.com/BurntSushi/toml"
)
type Config struct {
ServerId uint `toml:"server_id"`
LogLevel string `toml:"log_level"`
Addr string `toml:"addr"`
Logfile string `toml:"log_file"`
DataDir string `toml:"data_dir"`
AccessLogfile string `toml:"access_log_file"`
Queues int64 `toml:"queues"`
Dispatchers int64 `toml:"dispatchers"`
MaxWorkers int64 `toml:"max_workers"`
ShutdownTimeout int64 `toml:"shutdown_timeout"`
JobLifetime int64 `toml:"job_lifetime"`
JobListDefaultLimit int `toml:"job_list_default_limit"`
UI bool `toml:"ui"`
UIBasename string `toml:"ui_basename"`
IDEpoch []int `toml:"id_epoch"`
}
func NewConfig() *Config {
numCPU := runtime.NumCPU()
return &Config{
ServerId: 0,
LogLevel: "info",
Addr: "0.0.0.0:19900",
Logfile: "",
DataDir: "",
AccessLogfile: "",
Queues: 8192,
Dispatchers: int64(numCPU),
MaxWorkers: 0,
ShutdownTimeout: 10,
// JobLifetime's unit is second
JobLifetime: 60 * 60 * 24 * 28,
JobListDefaultLimit: 0,
UI: true,
UIBasename: "/ui",
IDEpoch: []int{2019, 1, 1},
}
}
func (c *Config) SetLogLevel(level string) {
c.LogLevel = level
}
func (c *Config) LoadConfigFile(path string) error {
_, err := toml.DecodeFile(path, c)
if err != nil {
return err
}
return nil
}
func (c *Config) IDEpochTime() (time.Time, error) {
if len(c.IDEpoch) != 3 {
return time.Now(), fmt.Errorf("id_epoch must be 3 int values")
}
return time.Date(c.IDEpoch[0], time.Month(c.IDEpoch[1]), c.IDEpoch[2], 0, 0, 0, 0, time.UTC), nil
}