From d1d0faaa7ea0b1a2c09fc870d9ba8c312adc4f17 Mon Sep 17 00:00:00 2001 From: spiral Date: Tue, 6 Dec 2022 10:45:07 +0000 Subject: [PATCH] feat(stats): don't require a stats database url --- services/scheduled_tasks/db.go | 6 +++++- services/scheduled_tasks/repo.go | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/services/scheduled_tasks/db.go b/services/scheduled_tasks/db.go index 789a2893..b27a2a75 100644 --- a/services/scheduled_tasks/db.go +++ b/services/scheduled_tasks/db.go @@ -2,6 +2,7 @@ package main import ( "context" + "os" redis "github.com/go-redis/redis/v8" "github.com/jackc/pgx/v4/pgxpool" @@ -22,8 +23,11 @@ func run_simple_pg_query(c *pgxpool.Pool, sql string) { func connect_dbs() { data_db = pg_connect(get_env_var("DATA_DB_URI")) messages_db = pg_connect(get_env_var("MESSAGES_DB_URI")) - stats_db = pg_connect(get_env_var("STATS_DB_URI")) rdb = redis_connect(get_env_var("REDIS_ADDR")) + + if uri, ok := os.LookupEnv("STATS_DB_URI"); ok { + stats_db = pg_connect(uri) + } } func pg_connect(url string) *pgxpool.Pool { diff --git a/services/scheduled_tasks/repo.go b/services/scheduled_tasks/repo.go index 7813bb5c..48966747 100644 --- a/services/scheduled_tasks/repo.go +++ b/services/scheduled_tasks/repo.go @@ -70,6 +70,10 @@ func run_data_stats_query() map[string]interface{} { } func do_stats_insert(table string, value int) { + if stats_db == nil { + return + } + sql := fmt.Sprintf("insert into %s values (now(), $1)", table) log.Println("stats db query:", sql, "value:", value) _, err := stats_db.Exec(context.Background(), sql, value)