Skip to content

Commit

Permalink
Shut down bot gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Mar 20, 2022
1 parent 743aada commit 6cccb27
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
_ "github.com/joho/godotenv/autoload"
"log"
"os"
"os/signal"
"syscall"
"time"

"gopkg.in/telebot.v3"
Expand Down Expand Up @@ -55,5 +57,22 @@ func main() {
bot.Handle("/start", h.OnStart)
bot.Handle("/stop", h.OnStop)

channel := make(chan os.Signal)
signal.Notify(channel, os.Interrupt, syscall.SIGTERM)
signal.Notify(channel, os.Interrupt, syscall.SIGKILL)
signal.Notify(channel, os.Interrupt, syscall.SIGINT)
go func() {
<-channel
log.Println("Stopping...")
bot.Stop()
err := db.Close()
if err != nil {
log.Println(err)
os.Exit(1)
return
}
os.Exit(0)
}()

bot.Start()
}

0 comments on commit 6cccb27

Please sign in to comment.