@@ -3,6 +3,7 @@ package compose
3
3
import (
4
4
"fmt"
5
5
"strings"
6
+ "time"
6
7
7
8
"github.com/rss3-network/node/config"
8
9
)
@@ -12,44 +13,82 @@ type Compose struct {
12
13
Volumes map [string ]* string
13
14
}
14
15
16
+ type Healthcheck struct {
17
+ Test []string `yaml:"test,omitempty"`
18
+ Interval time.Duration `yaml:"interval,omitempty"`
19
+ Timeout time.Duration `yaml:"timeout,omitempty"`
20
+ Retries int `yaml:"retries,omitempty"`
21
+ }
22
+
23
+ type DependsOn struct {
24
+ Condition string `yaml:"condition,omitempty"`
25
+ }
26
+
15
27
type Service struct {
16
- Command string `yaml:"command,omitempty"`
17
- ContainerName string `yaml:"container_name,omitempty"`
18
- Environment map [string ]string `yaml:"environment,omitempty"`
19
- Expose []string `yaml:"expose,omitempty"`
20
- Image string `yaml:"image"`
21
- Restart string `yaml:"restart,omitempty"`
22
- Ports []string `yaml:"ports,omitempty"`
23
- Volumes []string `yaml:"volumes,omitempty"`
28
+ Command string `yaml:"command,omitempty"`
29
+ ContainerName string `yaml:"container_name,omitempty"`
30
+ Environment map [string ]string `yaml:"environment,omitempty"`
31
+ Expose []string `yaml:"expose,omitempty"`
32
+ Image string `yaml:"image"`
33
+ Restart string `yaml:"restart,omitempty"`
34
+ Ports []string `yaml:"ports,omitempty"`
35
+ Volumes []string `yaml:"volumes,omitempty"`
36
+ Healthcheck Healthcheck `yaml:"healthcheck,omitempty"`
37
+ DependsOn map [string ]DependsOn `yaml:"depends_on,omitempty"`
24
38
}
25
39
26
40
type Option func (* Compose )
27
41
42
+ // use a prefix to avoid conflict with other containers
43
+ const dockerComposeContainerNamePrefix = "rss3_node"
44
+
28
45
func NewCompose (options ... Option ) * Compose {
29
- // use a prefix to avoid conflict with other containers
30
- prefix := "rss3_node"
31
46
cockroachdbVolume := "cockroachdb"
32
47
33
48
compose := & Compose {
34
49
Services : map [string ]Service {
35
- fmt .Sprintf ("%s_redis" , prefix ): {
36
- ContainerName : fmt .Sprintf ("%s_redis" , prefix ),
50
+ fmt .Sprintf ("%s_redis" , dockerComposeContainerNamePrefix ): {
51
+ ContainerName : fmt .Sprintf ("%s_redis" , dockerComposeContainerNamePrefix ),
37
52
Expose : []string {"6379" },
38
53
Image : "redis:7-alpine" ,
54
+ Healthcheck : Healthcheck {
55
+ Test : []string {"CMD" , "redis-cli" , "ping" },
56
+ Interval : 5 * time .Second ,
57
+ Timeout : 10 * time .Second ,
58
+ Retries : 3 ,
59
+ },
39
60
},
40
- fmt .Sprintf ("%s_cockroachdb" , prefix ): {
61
+ fmt .Sprintf ("%s_cockroachdb" , dockerComposeContainerNamePrefix ): {
41
62
Command : "start-single-node --cluster-name=node --insecure" ,
42
- ContainerName : fmt .Sprintf ("%s_cockroachdb" , prefix ),
63
+ ContainerName : fmt .Sprintf ("%s_cockroachdb" , dockerComposeContainerNamePrefix ),
43
64
Expose : []string {"26257" , "8080" },
44
65
Image : "cockroachdb/cockroach:v23.2.5" ,
45
66
Volumes : []string {fmt .Sprintf ("%s:/cockroach/cockroach-data" , cockroachdbVolume )},
67
+ // we use similar healthcheck as the official cockroachdb operator
68
+ // ref: https://github.com/cockroachdb/cockroach-operator/blob/28d139cb0c19d3c7984b2b2da1b25c5ba388d814/pkg/resource/testdata/TestStatefulSetBuilder/default_secure.golden#L76-L83
69
+ Healthcheck : Healthcheck {
70
+ Test : []string {"CMD" , "curl" , "-f" , "http://localhost:8080/health?ready=1" },
71
+ Interval : 5 * time .Second ,
72
+ Timeout : 1 * time .Second ,
73
+ Retries : 3 ,
74
+ },
46
75
},
47
- fmt .Sprintf ("%s_core" , prefix ): {
76
+ fmt .Sprintf ("%s_core" , dockerComposeContainerNamePrefix ): {
48
77
Command : "--module=core" ,
49
- ContainerName : fmt .Sprintf ("%s_core" , prefix ),
78
+ ContainerName : fmt .Sprintf ("%s_core" , dockerComposeContainerNamePrefix ),
50
79
Ports : []string {"8080:80" },
51
80
Image : "rss3/node" ,
52
81
},
82
+ fmt .Sprintf ("%s_monitor" , prefix ): {
83
+ Command : "--module=monitor" ,
84
+ ContainerName : fmt .Sprintf ("%s_monitor" , prefix ),
85
+ Image : "rss3/node" ,
86
+ },
87
+ fmt .Sprintf ("%s_broadcaster" , prefix ): {
88
+ Command : "--module=broadcaster" ,
89
+ ContainerName : fmt .Sprintf ("%s_broadcaster" , prefix ),
90
+ Image : "rss3/node" ,
91
+ },
53
92
},
54
93
Volumes : map [string ]* string {
55
94
cockroachdbVolume : nil ,
@@ -119,3 +158,23 @@ func WithWorkers(workers []*config.Module) Option {
119
158
c .Services = services
120
159
}
121
160
}
161
+
162
+ // SetDependsOnCRDB would set all the rss3 node service to depend on the cockroachdb service
163
+ func SetDependsOnCRDB () Option {
164
+ return func (c * Compose ) {
165
+ services := c .Services
166
+ for k , v := range services {
167
+ if strings .Contains (v .Image , "rss3/node" ) {
168
+ v .DependsOn = map [string ]DependsOn {
169
+ fmt .Sprintf ("%s_cockroachdb" , dockerComposeContainerNamePrefix ): {
170
+ Condition : "service_healthy" ,
171
+ },
172
+ fmt .Sprintf ("%s_redis" , dockerComposeContainerNamePrefix ): {
173
+ Condition : "service_healthy" ,
174
+ },
175
+ }
176
+ c .Services [k ] = v
177
+ }
178
+ }
179
+ }
180
+ }
0 commit comments