Browse Source

Database update time to config

Volodymyr Tkach 2 years ago
parent
commit
208144143f
3 changed files with 10 additions and 9 deletions
  1. 2 0
      Makefile
  2. 1 0
      internal/consts/consts.go
  3. 7 9
      internal/workers/worker_reloader/worker_reloader.go

+ 2 - 0
Makefile

@@ -21,6 +21,7 @@ debug:
 	make all
 	./bin/ip2location \
 		-data_dir ${DATA_DIR} \
+		-db_update_time 60 \
 		-deployment development \
 		-host 0.0.0.0 \
 		-port 8080 \
@@ -32,6 +33,7 @@ docker-test:
 		--network host \
 		--name ${DOCKER_IMG_NAME}-test \
 		-e ENV_DATA_DIR="/app/data" \
+		-e ENV_DB_UPDATE_TIME="60" \
 		-e ENV_DEPLOYMENT="deployment" \
 		-e ENV_HOST="127.0.0.1" \
 		-e ENV_PORT="8080" \

+ 1 - 0
internal/consts/consts.go

@@ -12,6 +12,7 @@ const DBFileName = "IP2LOCATION-LITE-DB3.BIN"
 var Config struct {
 	AccessLogFile string `description:"Access log file"`
 	DataDir       string `description:"Application data directory"`
+	DbUpdateTime  int64  `default:"60" description:"Delay in minutes between database reloading"`
 	Deployment    string `default:"development" description:"Deployment type"`
 	ErrorLogFile  string `description:"Error log file"`
 	Host          string `default:"127.0.0.1" description:"Web server IP"`

+ 7 - 9
internal/workers/worker_reloader/worker_reloader.go

@@ -5,22 +5,20 @@ import (
 	"time"
 
 	"github.com/vladimirok5959/golang-ip2location/internal/client"
+	"github.com/vladimirok5959/golang-ip2location/internal/consts"
 	"github.com/vladimirok5959/golang-utils/utils/http/logger"
 	"github.com/vladimirok5959/golang-worker/worker"
 )
 
-var Delay = 60 * time.Minute
-
 func New(cl *client.Client) *worker.Worker {
-	time.Sleep(1000 * time.Millisecond)
 	return worker.New(func(ctx context.Context, w *worker.Worker, o *[]worker.Iface) {
-		if cl, ok := (*o)[0].(*client.Client); ok {
-			Run(ctx, cl)
-		}
 		select {
 		case <-ctx.Done():
-		case <-time.After(Delay):
 			return
+		case <-time.After(time.Duration(consts.Config.DbUpdateTime) * time.Minute):
+		}
+		if cl, ok := (*o)[0].(*client.Client); ok {
+			Run(ctx, cl)
 		}
 	}, &[]worker.Iface{
 		cl,
@@ -36,8 +34,8 @@ func Run(ctx context.Context, cl *client.Client) {
 	}
 
 	if err == nil {
-		logger.LogInfo("worker reloader: done\n")
+		logger.LogInfo("worker reloader: reloading done\n")
 	} else {
-		logger.LogInfo("worker reloader: done with error\n")
+		logger.LogInfo("worker reloader: reloading done with errors\n")
 	}
 }