Stabilize docker service startup
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -49,6 +51,7 @@ type PprofConfig struct {
|
||||
ApiAddr string `yaml:"api_addr"`
|
||||
WorkerAddr string `yaml:"worker_addr"`
|
||||
}
|
||||
|
||||
func Load(filename string) (Config, error) {
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
@@ -60,9 +63,71 @@ func Load(filename string) (Config, error) {
|
||||
return Config{}, fmt.Errorf("parse config %s: %w", filename, err)
|
||||
}
|
||||
|
||||
ApplyEnvOverrides(&cfg)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func ApplyEnvOverrides(cfg *Config) {
|
||||
if cfg == nil {
|
||||
return
|
||||
}
|
||||
if v := os.Getenv("SERVER_PORT"); v != "" {
|
||||
if port, err := strconv.Atoi(v); err == nil {
|
||||
cfg.Server.Port = port
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("MYSQL_HOST"); v != "" {
|
||||
cfg.Database.Host = v
|
||||
}
|
||||
if v := os.Getenv("MYSQL_PORT"); v != "" {
|
||||
if port, err := strconv.Atoi(v); err == nil {
|
||||
cfg.Database.Port = port
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("MYSQL_USER"); v != "" {
|
||||
cfg.Database.User = v
|
||||
}
|
||||
if v := os.Getenv("MYSQL_ROOT_PASSWORD"); v != "" {
|
||||
cfg.Database.Password = v
|
||||
}
|
||||
if v := os.Getenv("MYSQL_PASSWORD"); v != "" {
|
||||
cfg.Database.Password = v
|
||||
}
|
||||
if v := os.Getenv("MYSQL_DATABASE"); v != "" {
|
||||
cfg.Database.DBName = v
|
||||
}
|
||||
if v := os.Getenv("REDIS_HOST"); v != "" {
|
||||
cfg.Redis.Host = v
|
||||
}
|
||||
if v := os.Getenv("REDIS_PORT"); v != "" {
|
||||
if port, err := strconv.Atoi(v); err == nil {
|
||||
cfg.Redis.Port = port
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("REDIS_PASSWORD"); v != "" {
|
||||
cfg.Redis.Password = v
|
||||
}
|
||||
if v := os.Getenv("REDIS_DB"); v != "" {
|
||||
if db, err := strconv.Atoi(v); err == nil {
|
||||
cfg.Redis.DB = db
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("RABBITMQ_HOST"); v != "" {
|
||||
cfg.RabbitMQ.Host = v
|
||||
}
|
||||
if v := os.Getenv("RABBITMQ_PORT"); v != "" {
|
||||
if port, err := strconv.Atoi(v); err == nil {
|
||||
cfg.RabbitMQ.Port = port
|
||||
}
|
||||
}
|
||||
if v := os.Getenv("RABBITMQ_USER"); v != "" {
|
||||
cfg.RabbitMQ.Username = v
|
||||
}
|
||||
if v := os.Getenv("RABBITMQ_PASS"); v != "" {
|
||||
cfg.RabbitMQ.Password = v
|
||||
}
|
||||
}
|
||||
|
||||
// bool用来表示是否使用了默认配置,true表示使用了默认配置
|
||||
func LoadLocalDev(filename string) (Config, bool, error) {
|
||||
cfg, err := Load(filename)
|
||||
@@ -76,7 +141,7 @@ func LoadLocalDev(filename string) (Config, bool, error) {
|
||||
}
|
||||
|
||||
func DefaultLocalConfig() Config {
|
||||
return Config{
|
||||
cfg := Config{
|
||||
Server: ServerConfig{
|
||||
Port: 8080,
|
||||
},
|
||||
@@ -107,4 +172,6 @@ func DefaultLocalConfig() Config {
|
||||
},
|
||||
},
|
||||
}
|
||||
ApplyEnvOverrides(&cfg)
|
||||
return cfg
|
||||
}
|
||||
@@ -15,6 +15,11 @@ import (
|
||||
)
|
||||
|
||||
func StartOutboxPoller(db *gorm.DB, tmq *rabbitmq.TimelineMQ) {
|
||||
if db == nil || tmq == nil || tmq.RabbitMQ == nil || tmq.Ch == nil {
|
||||
log.Printf("Outbox poller disabled: timeline mq is not initialized")
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
var messages []video.OutboxMsg
|
||||
@@ -40,6 +45,15 @@ func StartOutboxPoller(db *gorm.DB, tmq *rabbitmq.TimelineMQ) {
|
||||
}
|
||||
|
||||
func StartConsumer(tmq *rabbitmq.TimelineMQ, queueName string, redisClient *redis.Client) {
|
||||
if tmq == nil || tmq.RabbitMQ == nil || tmq.Ch == nil {
|
||||
log.Printf("Timeline consumer disabled: timeline mq is not initialized")
|
||||
return
|
||||
}
|
||||
if redisClient == nil {
|
||||
log.Printf("Timeline consumer disabled: redis is not initialized")
|
||||
return
|
||||
}
|
||||
|
||||
msgs, err := tmq.Ch.Consume(
|
||||
queueName,
|
||||
"",
|
||||
|
||||
@@ -17,7 +17,7 @@ services:
|
||||
- --character-set-server=utf8mb4
|
||||
- --collation-server=utf8mb4_0900_ai_ci
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p123456 --silent"]
|
||||
test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$${MYSQL_ROOT_PASSWORD} --silent"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 20
|
||||
@@ -25,13 +25,15 @@ services:
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
restart: always
|
||||
environment:
|
||||
REDIS_PASSWORD: ${REDIS_PASSWORD:-123456}
|
||||
command: ["redis-server", "--appendonly", "yes", "--requirepass", "${REDIS_PASSWORD:-123456}"]
|
||||
ports:
|
||||
- "6379:6379"
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "-a", "123456", "ping"]
|
||||
test: ["CMD-SHELL", "redis-cli -a \"$${REDIS_PASSWORD}\" ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
@@ -61,6 +63,11 @@ services:
|
||||
restart: always
|
||||
environment:
|
||||
JWT_SECRET: ${JWT_SECRET:-feedsystem-dev-secret-key}
|
||||
MYSQL_DATABASE: ${MYSQL_DATABASE:-feedsystem}
|
||||
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123456}
|
||||
REDIS_PASSWORD: ${REDIS_PASSWORD:-123456}
|
||||
RABBITMQ_USER: ${RABBITMQ_USER:-admin}
|
||||
RABBITMQ_PASS: ${RABBITMQ_PASS:-password123}
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
@@ -87,6 +94,11 @@ services:
|
||||
restart: always
|
||||
environment:
|
||||
JWT_SECRET: ${JWT_SECRET:-feedsystem-dev-secret-key}
|
||||
MYSQL_DATABASE: ${MYSQL_DATABASE:-feedsystem}
|
||||
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-123456}
|
||||
REDIS_PASSWORD: ${REDIS_PASSWORD:-123456}
|
||||
RABBITMQ_USER: ${RABBITMQ_USER:-admin}
|
||||
RABBITMQ_PASS: ${RABBITMQ_PASS:-password123}
|
||||
volumes:
|
||||
- ./backend/configs/config.docker.yaml:/app/configs/config.yaml:ro
|
||||
depends_on:
|
||||
|
||||
Reference in New Issue
Block a user