diff --git a/cmd/loader/main.go b/cmd/loader/main.go index 8a92a1d..b2f7b11 100644 --- a/cmd/loader/main.go +++ b/cmd/loader/main.go @@ -20,7 +20,7 @@ import ( const ( defaultMMDBPath = "/data/GeoLite2-City.mmdb" defaultSchema = "geoip" - defaultLoaderTimeout = 5 * time.Minute + defaultLoaderTimeout = 30 * time.Minute ) type cityRecord struct { @@ -57,18 +57,19 @@ type cityRow struct { } func main() { - ctx, cancel := context.WithTimeout(context.Background(), defaultLoaderTimeout) - defer cancel() - dbURL := os.Getenv("DATABASE_URL") if dbURL == "" { log.Fatal("DATABASE_URL is required") } mmdbPath := env("GEOIP_DB_PATH", defaultMMDBPath) + timeout := envDuration("GEOIP_LOADER_TIMEOUT", defaultLoaderTimeout) skipIfSame := envBool("GEOIP_LOADER_SKIP_IF_SAME_HASH", true) force := envBool("GEOIP_LOADER_FORCE", false) + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + log.Printf("starting mmdb load from %s", mmdbPath) hash, err := fileSHA256(mmdbPath) @@ -131,6 +132,18 @@ func envBool(key string, fallback bool) bool { return parsed } +func envDuration(key string, fallback time.Duration) time.Duration { + val := os.Getenv(key) + if val == "" { + return fallback + } + d, err := time.ParseDuration(val) + if err != nil { + return fallback + } + return d +} + func fileSHA256(path string) (string, error) { f, err := os.Open(path) if err != nil { diff --git a/docker-compose.yml b/docker-compose.yml index 1e27c43..0f57b1d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,9 +12,11 @@ services: - PORT=${PORT:-8080} - GEOIP_DB_PATH=${GEOIP_DB_PATH:-/data/GeoLite2-City.mmdb} - GEOIP_BACKEND=${GEOIP_BACKEND:-mmdb} + - GEOIP_LOADER_TIMEOUT=${GEOIP_LOADER_TIMEOUT:-30m} - DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST:-db}:${POSTGRES_PORT:-5432}/${POSTGRES_DB}?sslmode=disable command: > sh -c ' + set -e; if [ "${GEOIP_BACKEND}" = "postgres" ]; then echo "[api] running geoip-loader before api start"; geoip-loader;