feat: multi-user isolation and graceful shutdown

- Add chat_id to events, settings, and days_off for per-user data
- Add proper graceful shutdown (signal handling, WaitGroup)
- Separate polling and webhook modes with goroutine management
- Add schema migration from single-user to multi-user schema
- Refactor handlers with shared actionFunc pattern (msg + callback)
- Fix schema path for Docker deployment (/app/db/schema.sql)
- Remove emoji from output text for cleaner formatting
This commit is contained in:
2026-06-24 00:14:41 +03:30
parent 7aada8d118
commit 8616ed0867
7 changed files with 416 additions and 412 deletions

View File

@@ -4,8 +4,11 @@ import (
"log/slog"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@@ -40,29 +43,62 @@ func main() {
}
allowedUsers := parseAllowedUsers(os.Getenv("BOT_ALLOWED_USERS"))
handler := bot.NewHandler(botAPI, dbStore, allowedUsers)
slog.Info("bot started",
"bot_user", botAPI.Self.UserName,
"allowed_users", len(allowedUsers),
"polling", true,
)
stop := make(chan struct{})
var wg sync.WaitGroup
webhookURL := os.Getenv("BOT_WEBHOOK_URL")
if webhookURL != "" {
startWebhook(botAPI, handler, webhookURL)
if url := os.Getenv("BOT_WEBHOOK_URL"); url != "" {
wg.Add(1)
go func() {
defer wg.Done()
startWebhook(botAPI, handler, url)
}()
} else {
if _, err := botAPI.Request(tgbotapi.DeleteWebhookConfig{}); err != nil {
slog.Error("delete webhook", "error", err)
}
go dailyReportScheduler(handler, dbStore)
wg.Add(1)
go func() {
defer wg.Done()
dailyReportScheduler(handler, dbStore, stop)
}()
u := tgbotapi.NewUpdate(0)
u.Timeout = 30
updates := botAPI.GetUpdatesChan(u)
for update := range updates {
wg.Add(1)
go func() {
defer wg.Done()
startPolling(botAPI, handler, stop)
}()
}
slog.Info("bot started",
"bot_user", botAPI.Self.UserName,
"allowed_users", len(allowedUsers),
"mode", map[bool]string{true: "webhook", false: "polling"}[os.Getenv("BOT_WEBHOOK_URL") != ""],
)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
slog.Info("shutting down", "signal", s.String())
close(stop)
wg.Wait()
dbStore.Close()
}
func startPolling(botAPI *tgbotapi.BotAPI, handler *bot.Handler, stop chan struct{}) {
u := tgbotapi.NewUpdate(0)
u.Timeout = 30
updates := botAPI.GetUpdatesChan(u)
for {
select {
case <-stop:
return
case update, ok := <-updates:
if !ok {
return
}
if update.Message != nil {
handler.HandleMessage(update)
}
@@ -90,7 +126,7 @@ func parseAllowedUsers(raw string) map[int64]bool {
return m
}
func dailyReportScheduler(h *bot.Handler, store *db.Store) {
func dailyReportScheduler(h *bot.Handler, store *db.Store, stop chan struct{}) {
for {
now := time.Now()
next := time.Date(now.Year(), now.Month(), now.Day(), 23, 0, 0, 0, now.Location())
@@ -98,9 +134,14 @@ func dailyReportScheduler(h *bot.Handler, store *db.Store) {
next = next.AddDate(0, 0, 1)
}
slog.Info("daily report scheduler", "next_run", next)
time.Sleep(time.Until(next))
chatIDStr, err := store.GetSetting("chat_id")
select {
case <-stop:
return
case <-time.After(time.Until(next)):
}
chatIDStr, err := store.GetSetting(0, "report_chat_id")
if err != nil {
slog.Error("daily report: chat_id not found")
continue