Skip to content

Commit 6cccb27

Browse files
committed
Shut down bot gracefully
1 parent 743aada commit 6cccb27

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
_ "github.com/joho/godotenv/autoload"
77
"log"
88
"os"
9+
"os/signal"
10+
"syscall"
911
"time"
1012

1113
"gopkg.in/telebot.v3"
@@ -55,5 +57,22 @@ func main() {
5557
bot.Handle("/start", h.OnStart)
5658
bot.Handle("/stop", h.OnStop)
5759

60+
channel := make(chan os.Signal)
61+
signal.Notify(channel, os.Interrupt, syscall.SIGTERM)
62+
signal.Notify(channel, os.Interrupt, syscall.SIGKILL)
63+
signal.Notify(channel, os.Interrupt, syscall.SIGINT)
64+
go func() {
65+
<-channel
66+
log.Println("Stopping...")
67+
bot.Stop()
68+
err := db.Close()
69+
if err != nil {
70+
log.Println(err)
71+
os.Exit(1)
72+
return
73+
}
74+
os.Exit(0)
75+
}()
76+
5877
bot.Start()
5978
}

0 commit comments

Comments
 (0)