From d6a3cfc6848d6e85ba5f016034584f7b85a8694c Mon Sep 17 00:00:00 2001 From: Ashok Bishnoi Date: Wed, 28 Aug 2024 21:27:01 +0530 Subject: [PATCH] db: add graceful shutdown bump deps --- FallenSub/db/db.go | 24 ++++++++++++++++++- .../{dispatcher => modules}/dispatcher.go | 5 ++-- README.md | 10 ++++---- api/bot.go | 4 ++-- go.sum | 13 ++-------- main.go | 4 ++-- 6 files changed, 36 insertions(+), 24 deletions(-) rename FallenSub/{dispatcher => modules}/dispatcher.go (69%) diff --git a/FallenSub/db/db.go b/FallenSub/db/db.go index 1ea18d4..0a63fb9 100644 --- a/FallenSub/db/db.go +++ b/FallenSub/db/db.go @@ -7,7 +7,10 @@ import ( "github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config" "github.com/go-redis/redis/v8" "log" + "os" + "os/signal" "strconv" + "syscall" ) var ( @@ -16,7 +19,6 @@ var ( ) func init() { - opt, err := redis.ParseURL(config.DatabaseURI) if err != nil { log.Fatalf("[Database][Connect]: %v", err) @@ -28,6 +30,26 @@ func init() { } log.Println("[Database][Connect]: Connected to Redis") + + // Setup signal handling to ensure graceful shutdown + go handleSignals() +} + +// Handle OS signals for graceful shutdown +func handleSignals() { + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + <-signals + Shutdown() +} + +// Shutdown performs cleanup tasks +func Shutdown() { + if err := rdb.Close(); err != nil { + log.Printf("error closing Redis client: %v", err) + } + + log.Println("shutdown complete") } type FSub struct { diff --git a/FallenSub/dispatcher/dispatcher.go b/FallenSub/modules/dispatcher.go similarity index 69% rename from FallenSub/dispatcher/dispatcher.go rename to FallenSub/modules/dispatcher.go index 10c6ccc..8fb1f63 100644 --- a/FallenSub/dispatcher/dispatcher.go +++ b/FallenSub/modules/dispatcher.go @@ -1,7 +1,6 @@ -package dispatcher +package modules import ( - "github.com/Abishnoi69/Force-Sub-Bot/FallenSub/modules" "github.com/PaulSonOfLars/gotgbot/v2/ext" ) @@ -10,7 +9,7 @@ var Dispatcher = newDispatcher() // newDispatcher creates a new dispatcher and loads modules. func newDispatcher() *ext.Dispatcher { dispatcher := ext.NewDispatcher(nil) - modules.LoadModules(dispatcher) + LoadModules(dispatcher) return dispatcher } diff --git a/README.md b/README.md index 07c1f1a..620d980 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,11 @@

Installation Instructions

Install Go

    -
  1. Clone and install Go: -
    git clone https://github.com/udhos/update-golang && cd update-golang
    -sudo ./update-golang.sh
    -source /etc/profile.d/golang_path.sh
    -
  2. + +```shell +git clone https://github.com/udhos/update-golang dlgo && cd dlgo && sudo ./update-golang.sh && source /etc/profile.d/golang_path.sh +``` +
  3. Exit and reopen your terminal, then verify the installation with go version.
diff --git a/api/bot.go b/api/bot.go index bb7d38d..7208cf1 100644 --- a/api/bot.go +++ b/api/bot.go @@ -3,12 +3,12 @@ package api import ( "encoding/json" "fmt" + "github.com/Abishnoi69/Force-Sub-Bot/FallenSub/modules" "io" "net/http" "os" "strings" - "github.com/Abishnoi69/Force-Sub-Bot/FallenSub/dispatcher" "github.com/PaulSonOfLars/gotgbot/v2" ) @@ -63,7 +63,7 @@ func Bot(w http.ResponseWriter, r *http.Request) { bot.Username = split[len(split)-1] - err = dispatcher.Dispatcher.ProcessUpdate(bot, &update, map[string]any{}) + err = modules.Dispatcher.ProcessUpdate(bot, &update, map[string]any{}) if err != nil { fmt.Printf("error while processing update: %v", err) } diff --git a/go.sum b/go.sum index 49eb629..c8d307d 100644 --- a/go.sum +++ b/go.sum @@ -16,21 +16,12 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= -golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= -golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= -golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= -golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/main.go b/main.go index 0ba25f4..0ebea76 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,10 @@ package main import ( + "github.com/Abishnoi69/Force-Sub-Bot/FallenSub/modules" "time" "github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config" - "github.com/Abishnoi69/Force-Sub-Bot/FallenSub/dispatcher" "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" ) @@ -15,7 +15,7 @@ func main() { config.ErrorLog.Fatal("failed to create new bot:", err) } - updater := ext.NewUpdater(dispatcher.Dispatcher, nil) + updater := ext.NewUpdater(modules.Dispatcher, nil) err = updater.StartPolling(b, &ext.PollingOpts{ DropPendingUpdates: true, GetUpdatesOpts: &gotgbot.GetUpdatesOpts{