크론구조 개선
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -17,8 +18,8 @@ import (
|
||||
const (
|
||||
defaultPort = "8080"
|
||||
defaultDBPath = "/initial_data/GeoLite2-City.mmdb"
|
||||
defaultCron = ""
|
||||
defaultScript = "./scripts/dump_and_import.sh"
|
||||
defaultCron = "5 0 * * *" // 매일 00:05 KST
|
||||
defaultJob = "user-program-sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -26,7 +27,7 @@ func main() {
|
||||
dbPath := env("GEOIP_DB_PATH", defaultDBPath)
|
||||
dbURL := os.Getenv("DATABASE_URL")
|
||||
lookupQuery := os.Getenv("GEOIP_LOOKUP_QUERY")
|
||||
port := env("PORT", defaultPort)
|
||||
port := env("SERVICE_PORT", defaultPort)
|
||||
|
||||
resolver, err := geo.NewResolver(geo.Config{
|
||||
Backend: backend,
|
||||
@@ -112,6 +113,21 @@ func env(key, fallback string) string {
|
||||
return fallback
|
||||
}
|
||||
|
||||
func envBool(key string, fallback bool) bool {
|
||||
val := os.Getenv(key)
|
||||
if val == "" {
|
||||
return fallback
|
||||
}
|
||||
switch strings.ToLower(val) {
|
||||
case "1", "t", "true", "y", "yes", "on":
|
||||
return true
|
||||
case "0", "f", "false", "n", "no", "off":
|
||||
return false
|
||||
default:
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
func sanitizeDBURL(raw string) string {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil {
|
||||
@@ -121,15 +137,16 @@ func sanitizeDBURL(raw string) string {
|
||||
}
|
||||
|
||||
func maybeStartScheduler() func() context.Context {
|
||||
cronExpr := env("USER_PROGRAM_CRON", defaultCron)
|
||||
if cronExpr == "" {
|
||||
enabled := envBool("USER_PROGRAM_CRON_ENABLE", false)
|
||||
if !enabled {
|
||||
return nil
|
||||
}
|
||||
script := env("USER_PROGRAM_SCRIPT", defaultScript)
|
||||
cronExpr := defaultCron
|
||||
command := defaultJob
|
||||
|
||||
sched, err := schedule.Start(schedule.Config{
|
||||
CronExpr: cronExpr,
|
||||
ScriptPath: script,
|
||||
Command: command,
|
||||
Logger: log.Default(),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user