-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_check_config.go
111 lines (102 loc) · 2.6 KB
/
job_check_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
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
package main
import (
"fmt"
"time"
)
type JobCheckConfig struct {
Method string `json:"method"`
Database string `json:"database,omitempty"`
Bucket string `json:"bucket,omitempty"`
Timeout string `json:"timeout,omitempty"`
storage Storage
}
func (c *JobCheckConfig) setup() *ConfigError {
c.Prepare()
return c.Validate()
}
func (c *JobCheckConfig) Prepare() {
if c.Method == "" {
c.Method = "none"
}
switch c.Method {
case JobCheckMethodNone:
case JobCheckMethodBuntDB:
if c.Database == "" {
c.Database = "blocks-gcs-proxy.db"
}
if c.Bucket == "" {
c.Bucket = "jobs:"
}
case JobCheckMethodGcslock:
if c.Database == "" {
c.Database = "gcslocks"
}
if c.Timeout == "" {
c.Timeout = "10m"
}
}
}
const (
JobCheckMethodNone = "none"
JobCheckMethodBuntDB = "buntdb"
JobCheckMethodGcslock = "gcslock"
)
var JobCheckMethods = []string{
JobCheckMethodNone,
JobCheckMethodBuntDB,
JobCheckMethodGcslock,
}
func (c *JobCheckConfig) Validate() *ConfigError {
switch c.Method {
case JobCheckMethodNone:
return nil
case JobCheckMethodBuntDB:
if c.Bucket == "" {
return &ConfigError{Name: "bucket", Message: fmt.Sprintf("bucket is required for method %q", c.Method)}
}
return nil
case JobCheckMethodGcslock:
if c.Bucket == "" {
return &ConfigError{Name: "bucket", Message: fmt.Sprintf("bucket is required for method %q", c.Method)}
}
_, err := time.ParseDuration(c.Timeout)
if err != nil {
return &ConfigError{Name: "timeout", Message: fmt.Sprintf("Invalid timeout %q", c.Timeout)}
}
return nil
default:
return &ConfigError{Name: "method", Message: fmt.Sprintf("%q is invalid. It must be one of %v", c.Method, JobCheckMethods)}
}
}
func (c *JobCheckConfig) Checker() func(string, func() error, func() error) error {
switch c.Method {
case JobCheckMethodNone:
return func(job_id string, ack, f func() error) error {
return f()
}
case JobCheckMethodBuntDB:
checker := &JobCheckByBuntDB{
File: c.Database,
Prefix: c.Bucket,
}
return checker.Check
case JobCheckMethodGcslock:
d, err := time.ParseDuration(c.Timeout)
if err != nil {
return func(job_id string, ack, f func() error) error {
return &ConfigError{Name: "timeout", Message: fmt.Sprintf("Invalid timeout %q", c.Timeout)}
}
}
checker := &JobCheckByGcslock{
Bucket: c.Bucket,
DirPath: c.Database,
Timeout: d,
Storage: c.storage,
}
return checker.Check
default:
return func(job_id string, ack, f func() error) error {
return &ConfigError{Name: "method", Message: fmt.Sprintf("%q is invalid. It must be one of %v", c.Method, JobCheckMethods)}
}
}
}