Skip to content

Commit 4ebce35

Browse files
authored
refactor(publisher): load the worker configurations from one same file (#193)
Signed-off-by: wuhuizuo <wuhuizuo@126.com> Signed-off-by: wuhuizuo <wuhuizuo@126.com>
1 parent 0ef76a1 commit 4ebce35

7 files changed

Lines changed: 86 additions & 63 deletions

File tree

publisher/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ go run ./cmd/publisher -config=config-publisher.yaml --debug --domain 0.0.0.0:80
3333
```bash
3434
go run ./cmd/worker --config=config-worker.yaml --debug
3535
```
36+
37+
### Test with the client CLI
38+
39+
```bash
40+
go run ./cmd/publisher-cli -url=http://localhost:8080 tiup request-to-publish -body '{ "artifact_url": "hub.pingcap.net/pingcap/tidb/package:master_linux_amd64" }'
41+
go run ./cmd/publisher-cli -url=http://localhost:8080 fileserver request-to-publish -body '{ "artifact_url": "hub.pingcap.net/pingcap/tidb/package:master_linux_amd64" }'
42+
```

publisher/cmd/worker/init.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@ import (
1515
)
1616

1717
// Load and parse configuration.
18-
func loadConfig(configFile string) (config.Worker, error) {
19-
var config config.Worker
20-
{
21-
configData, err := os.ReadFile(configFile)
22-
if err != nil {
23-
return config, fmt.Errorf("error reading config file: %v", err)
24-
}
25-
if err := yaml.Unmarshal(configData, &config); err != nil {
26-
return config, fmt.Errorf("error parsing config file: %v", err)
27-
}
18+
func loadConfig(configFile string) (*config.Workers, error) {
19+
configData, err := os.ReadFile(configFile)
20+
if err != nil {
21+
return nil, fmt.Errorf("error reading config file: %v", err)
22+
}
23+
24+
var config config.Workers
25+
if err := yaml.Unmarshal(configData, &config); err != nil {
26+
return nil, fmt.Errorf("error parsing config file: %v", err)
2827
}
29-
return config, nil
28+
29+
return &config, nil
3030
}
3131

32-
func initTiupWorkerFromConfig(configFile string) (*kafka.Reader, impl.Worker) {
33-
config, err := loadConfig(configFile)
34-
if err != nil {
35-
log.Fatal().Err(err).Msg("load config failed")
32+
func initTiupWorkerFromConfig(config *config.Worker) (*kafka.Reader, impl.Worker) {
33+
if config == nil {
34+
return nil, nil
3635
}
3736

3837
// Configure Redis client.
@@ -61,10 +60,9 @@ func initTiupWorkerFromConfig(configFile string) (*kafka.Reader, impl.Worker) {
6160
return kafkaReader, worker
6261
}
6362

64-
func initFsWorkerFromConfig(configFile string) (*kafka.Reader, impl.Worker) {
65-
config, err := loadConfig(configFile)
66-
if err != nil {
67-
log.Fatal().Err(err).Msg("load config failed")
63+
func initFsWorkerFromConfig(config *config.Worker) (*kafka.Reader, impl.Worker) {
64+
if config == nil {
65+
return nil, nil
6866
}
6967

7068
// Configure Redis client.
@@ -75,12 +73,9 @@ func initFsWorkerFromConfig(configFile string) (*kafka.Reader, impl.Worker) {
7573
DB: config.Redis.DB,
7674
})
7775

78-
var worker impl.Worker
79-
{
80-
worker, err = impl.NewFsWorker(&log.Logger, redisClient, config.Options)
81-
if err != nil {
82-
log.Fatal().Err(err).Msg("Error creating tiup publishing worker")
83-
}
76+
worker, err := impl.NewFsWorker(&log.Logger, redisClient, config.Options)
77+
if err != nil {
78+
log.Fatal().Err(err).Msg("Error creating tiup publishing worker")
8479
}
8580

8681
kafkaReader := kafka.NewReader(kafka.ReaderConfig{

publisher/cmd/worker/main.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import (
2121
func main() {
2222
// Parse command-line flags
2323
var (
24-
tiupConfigFile = flag.String("tiup-config", "config.yaml", "Path to config file")
25-
fsConfigFile = flag.String("fs-config", "config.yaml", "Path to config file")
26-
dbgF = flag.Bool("debug", false, "Enable debug mode")
24+
configFile = flag.String("config", "config.yaml", "Path to config file")
25+
dbgF = flag.Bool("debug", false, "Enable debug mode")
2726
)
2827
flag.Parse()
2928

@@ -35,8 +34,13 @@ func main() {
3534
zerolog.SetGlobalLevel(zerolog.InfoLevel)
3635
}
3736

38-
tiupPublishRequestKafkaReader, tiupWorker := initTiupWorkerFromConfig(*tiupConfigFile)
39-
fsPublishRequestKafkaReader, fsWorker := initFsWorkerFromConfig(*fsConfigFile)
37+
config, err := loadConfig(*configFile)
38+
if err != nil {
39+
log.Fatal().Err(err).Msg("load config failed")
40+
}
41+
42+
tiupPublishRequestKafkaReader, tiupWorker := initTiupWorkerFromConfig(config.Tiup)
43+
fsPublishRequestKafkaReader, fsWorker := initFsWorkerFromConfig(config.FileServer)
4044

4145
// Create channel used by both the signal handler and server goroutines
4246
// to notify the main goroutine when to stop the server.
@@ -65,6 +69,15 @@ func main() {
6569
}
6670

6771
func startWorker(ctx context.Context, wg *sync.WaitGroup, reader *kafka.Reader, worker impl.Worker) {
72+
if reader == nil {
73+
log.Warn().Msg("empty kafka reader, skip")
74+
return
75+
}
76+
if worker == nil {
77+
log.Warn().Msg("empty worker, skip")
78+
return
79+
}
80+
6881
(*wg).Add(1)
6982
go func() {
7083
defer (*wg).Done()

publisher/example/config/worker-example.fileserver.yaml

Lines changed: 0 additions & 18 deletions
This file was deleted.

publisher/example/config/worker-example.tiup.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
tiup:
2+
kafka:
3+
brokers:
4+
- example-bootstrap.kafka:9092
5+
topic: example-topic
6+
consumer_group: example-group
7+
8+
redis:
9+
addr: "redis-server:6379"
10+
db: 0
11+
password: "redis_password"
12+
13+
options:
14+
mirror_url: http://tiup.mirror.site
15+
lark_webhook_url: https://feishu.custom-bot-webhook # create and copy the url then paste here.
16+
nightly_interval: 1h
17+
18+
file_server:
19+
kafka:
20+
brokers:
21+
- example-bootstrap.kafka:9092
22+
topic: example-topic
23+
consumer_group: example-group-fs
24+
25+
redis:
26+
addr: "redis-server:6379"
27+
db: 0
28+
password: "redis_password"
29+
30+
options:
31+
lark_webhook_url: https://feishu.custom-bot-webhook # create and copy the url then paste here.
32+
s3.endpoint: <endpoint>
33+
s3.region: BEIJING
34+
s3.bucket_name: <bucket-name>
35+
s3.access_key: <access-key>
36+
s3.secret_key: <secret-key>

publisher/pkg/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package config
22

3+
type Workers struct {
4+
Tiup *Worker `yaml:"tiup,omitempty" json:"tiup,omitempty"`
5+
FileServer *Worker `yaml:"file_server,omitempty" json:"file_server,omitempty"`
6+
}
7+
38
type Worker struct {
49
Kafka struct {
510
KafkaBasic `yaml:",inline" json:",inline"`

0 commit comments

Comments
 (0)