Skip to content

Commit

Permalink
db: add graceful shutdown
Browse files Browse the repository at this point in the history
bump deps
  • Loading branch information
AshokShau committed Aug 28, 2024
1 parent 60013fd commit d6a3cfc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
24 changes: 23 additions & 1 deletion FallenSub/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -16,7 +19,6 @@ var (
)

func init() {

opt, err := redis.ParseURL(config.DatabaseURI)
if err != nil {
log.Fatalf("[Database][Connect]: %v", err)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dispatcher
package modules

import (
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/modules"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)

Expand All @@ -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
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
<h2>Installation Instructions</h2>
<h3>Install Go</h3>
<ol>
<li>Clone and install Go:
<pre><code>git clone https://github.com/udhos/update-golang && cd update-golang
sudo ./update-golang.sh
source /etc/profile.d/golang_path.sh</code></pre>
</li>

```shell
git clone https://github.com/udhos/update-golang dlgo && cd dlgo && sudo ./update-golang.sh && source /etc/profile.d/golang_path.sh
```

<li>Exit and reopen your terminal, then verify the installation with <code>go version</code>.</li>
</ol>

Expand Down
4 changes: 2 additions & 2 deletions api/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 2 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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{
Expand Down

0 comments on commit d6a3cfc

Please sign in to comment.