Skip to content

Commit ef5a650

Browse files
author
escalopa
committed
fix: time get error
1 parent 37a6ba8 commit ef5a650

File tree

14 files changed

+75
-42
lines changed

14 files changed

+75
-42
lines changed

main.tf

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ variable "region" {
2525
default = "ru-central1-d"
2626
}
2727

28-
variable "environment" {
29-
type = string
30-
default = "stg"
28+
output "region" {
29+
value = var.region
3130
}
3231

3332
provider "yandex" {
@@ -42,7 +41,7 @@ provider "yandex" {
4241
###########################
4342

4443
resource "yandex_storage_bucket" "bucket" {
45-
bucket = "prayer-bot-bucket"
44+
bucket = "prayer-bot-bucket-${terraform.workspace}"
4645
folder_id = var.folder_id
4746
max_size = 1073741824 # 1 GB in bytes
4847
}
@@ -52,7 +51,7 @@ resource "yandex_storage_bucket" "bucket" {
5251
###########################
5352

5453
resource "yandex_ydb_database_serverless" "ydb" {
55-
name = "prayer-bot-ydb"
54+
name = "prayer-bot-ydb-${terraform.workspace}"
5655
location_id = "ru-central1"
5756
folder_id = var.folder_id
5857
deletion_protection = true
@@ -108,6 +107,11 @@ resource "yandex_ydb_table" "ydb_table_chats" {
108107
name = "subscribed_at"
109108
type = "Datetime"
110109
}
110+
111+
column {
112+
name = "created_at"
113+
type = "Datetime"
114+
}
111115
}
112116

113117
resource "yandex_ydb_table" "ydb_table_prayers" {
@@ -211,9 +215,9 @@ resource "yandex_function" "loader_fn" {
211215
user_hash = "v1"
212216

213217
environment = {
214-
APP_CONFIG = file("${path.module}/_config/${var.environment}/config.json")
218+
APP_CONFIG = file("${path.module}/_config/${terraform.workspace}/config.json")
215219

216-
S3_ENDPOINT = "https://storage.yandexcloud.net"
220+
S3_ENDPOINT = "https://storage.yandexcloud.net"
217221
YDB_ENDPOINT = yandex_ydb_database_serverless.ydb.ydb_full_endpoint
218222

219223
REGION = var.region
@@ -288,7 +292,7 @@ resource "yandex_function" "dispatcher_fn" {
288292
user_hash = "v1"
289293

290294
environment = {
291-
APP_CONFIG = file("${path.module}/_config/${var.environment}/config.json")
295+
APP_CONFIG = file("${path.module}/_config/${terraform.workspace}/config.json")
292296

293297
YDB_ENDPOINT = yandex_ydb_database_serverless.ydb.ydb_full_endpoint
294298

@@ -345,7 +349,7 @@ resource "yandex_function" "reminder_fn" {
345349
user_hash = "v1"
346350

347351
environment = {
348-
APP_CONFIG = file("${path.module}/_config/${var.environment}/config.json")
352+
APP_CONFIG = file("${path.module}/_config/${terraform.workspace}/config.json")
349353

350354
YDB_ENDPOINT = yandex_ydb_database_serverless.ydb.ydb_full_endpoint
351355

serverless/dispatcher/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/escalopa/prayer-bot/dispatcher
33
go 1.21
44

55
require (
6-
github.com/escalopa/prayer-bot v0.0.0-20250509151854-3e3cf0f5b265
6+
github.com/escalopa/prayer-bot v0.0.0-20250509233421-56ffdb42fd63
77
github.com/go-telegram/bot v1.15.0
88
github.com/ydb-platform/ydb-go-genproto v0.0.0-20241112172322-ea1f63298f77
99
github.com/ydb-platform/ydb-go-sdk/v3 v3.108.0

serverless/dispatcher/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,8 @@ github.com/escalopa/prayer-bot v0.0.0-20250509132525-37a6d5ff4d9d h1:CYxuerTyBke
573573
github.com/escalopa/prayer-bot v0.0.0-20250509132525-37a6d5ff4d9d/go.mod h1:HOpIxziLKMTC/a+Q6z40Rn5tJDEZsFyzhniLA4iB2i0=
574574
github.com/escalopa/prayer-bot v0.0.0-20250509151854-3e3cf0f5b265 h1:t1Kh/tBb+V42vPsBkiAahTepw5aoLVZgdlg9P1bsHZU=
575575
github.com/escalopa/prayer-bot v0.0.0-20250509151854-3e3cf0f5b265/go.mod h1:HOpIxziLKMTC/a+Q6z40Rn5tJDEZsFyzhniLA4iB2i0=
576+
github.com/escalopa/prayer-bot v0.0.0-20250509233421-56ffdb42fd63 h1:bnel/0nURIdPC3uQ9+C5PPAjaU5HOlKT4kD5dEaDIcU=
577+
github.com/escalopa/prayer-bot v0.0.0-20250509233421-56ffdb42fd63/go.mod h1:HOpIxziLKMTC/a+Q6z40Rn5tJDEZsFyzhniLA4iB2i0=
576578
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
577579
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
578580
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=

serverless/dispatcher/internal/handler/command.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (h *Handler) today(ctx context.Context, b *bot.Bot, update *models.Update)
9393
return fmt.Errorf("today: get chat: %v", err)
9494
}
9595

96-
prayerDay, err := h.db.GetPrayerDay(ctx, chat.BotID, h.now(chat.BotID))
96+
prayerDay, err := h.db.GetPrayerDay(ctx, chat.BotID, h.nowUTC(chat.BotID))
9797
if err != nil {
9898
log.Error("today: get prayer day", log.Err(err), log.BotID(chat.BotID), log.ChatID(chat.ChatID))
9999
return fmt.Errorf("today: get prayer day: %v", err)
@@ -140,7 +140,11 @@ func (h *Handler) next(ctx context.Context, b *bot.Bot, update *models.Update) e
140140
return fmt.Errorf("next: get chat: %v", err)
141141
}
142142

143-
date := h.now(chat.BotID)
143+
var (
144+
now = h.now(chat.BotID)
145+
date = h.nowUTC(chat.BotID)
146+
)
147+
144148
prayerDay, err := h.db.GetPrayerDay(ctx, chat.BotID, date)
145149
if err != nil {
146150
log.Error("next: get prayer day", log.Err(err), log.BotID(chat.BotID), log.ChatID(chat.ChatID))
@@ -149,18 +153,18 @@ func (h *Handler) next(ctx context.Context, b *bot.Bot, update *models.Update) e
149153

150154
prayerID, duration := domain.PrayerIDUnknown, time.Duration(0)
151155
switch {
152-
case prayerDay.Fajr.After(date):
153-
prayerID, duration = domain.PrayerIDFajr, prayerDay.Fajr.Sub(date)
154-
case prayerDay.Shuruq.After(date):
155-
prayerID, duration = domain.PrayerIDShuruq, prayerDay.Shuruq.Sub(date)
156-
case prayerDay.Dhuhr.After(date):
157-
prayerID, duration = domain.PrayerIDDhuhr, prayerDay.Dhuhr.Sub(date)
158-
case prayerDay.Asr.After(date):
159-
prayerID, duration = domain.PrayerIDAsr, prayerDay.Asr.Sub(date)
160-
case prayerDay.Maghrib.After(date):
161-
prayerID, duration = domain.PrayerIDMaghrib, prayerDay.Maghrib.Sub(date)
162-
case prayerDay.Isha.After(date):
163-
prayerID, duration = domain.PrayerIDIsha, prayerDay.Isha.Sub(date)
156+
case prayerDay.Fajr.After(now):
157+
prayerID, duration = domain.PrayerIDFajr, prayerDay.Fajr.Sub(now)
158+
case prayerDay.Shuruq.After(now):
159+
prayerID, duration = domain.PrayerIDShuruq, prayerDay.Shuruq.Sub(now)
160+
case prayerDay.Dhuhr.After(now):
161+
prayerID, duration = domain.PrayerIDDhuhr, prayerDay.Dhuhr.Sub(now)
162+
case prayerDay.Asr.After(now):
163+
prayerID, duration = domain.PrayerIDAsr, prayerDay.Asr.Sub(now)
164+
case prayerDay.Maghrib.After(now):
165+
prayerID, duration = domain.PrayerIDMaghrib, prayerDay.Maghrib.Sub(now)
166+
case prayerDay.Isha.After(now):
167+
prayerID, duration = domain.PrayerIDIsha, prayerDay.Isha.Sub(now)
164168
}
165169

166170
// when no prayer time is found, return the first prayer of the next day
@@ -170,7 +174,7 @@ func (h *Handler) next(ctx context.Context, b *bot.Bot, update *models.Update) e
170174
log.Error("next: get prayer day", log.Err(err), log.BotID(chat.BotID), log.ChatID(chat.ChatID))
171175
return fmt.Errorf("next: get prayer day: %v", err)
172176
}
173-
prayerID, duration = domain.PrayerIDFajr, prayerDay.Fajr.Sub(date)
177+
prayerID, duration = domain.PrayerIDFajr, prayerDay.Fajr.Sub(now)
174178
}
175179

176180
text := h.lp.GetText(chat.LanguageCode)
@@ -180,7 +184,13 @@ func (h *Handler) next(ctx context.Context, b *bot.Bot, update *models.Update) e
180184
ParseMode: models.ParseModeMarkdown,
181185
})
182186
if err != nil {
183-
log.Error("next: send message", log.Err(err), log.BotID(chat.BotID), log.ChatID(chat.ChatID))
187+
log.Error("next: send message",
188+
log.Err(err),
189+
log.BotID(chat.BotID),
190+
log.ChatID(chat.ChatID),
191+
log.String("date", date.String()),
192+
log.String("prayer_day", fmt.Sprintf("%+v", prayerDay)),
193+
)
184194
return fmt.Errorf("next: send message: %v", err)
185195
}
186196

serverless/dispatcher/internal/handler/helper.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,23 @@ func (h *Handler) formatPrayerDay(botID int64, date *domain.PrayerDay, languageC
3636
)
3737
}
3838

39+
// now returns the current time with seconds and nanoseconds set to 0
3940
func (h *Handler) now(botID int64) time.Time {
40-
return domain.Now(h.cfg[botID].Location.V())
41+
t := time.Now().In(h.cfg[botID].Location.V())
42+
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
43+
}
44+
45+
// nowUTC returns the current time in UTC with seconds and nanoseconds set to 0
46+
// Use this function to get prayerDay or current year for a specific botID timezone.
47+
func (h *Handler) nowUTC(botID int64) time.Time {
48+
t := time.Now().In(h.cfg[botID].Location.V())
49+
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, time.UTC)
4150
}
4251

4352
// daysInMonth returns the number of days in a month.
44-
func daysInMonth(month time.Month, t time.Time) int {
53+
func daysInMonth(month time.Month, year int) int {
4554
// month is incremented by 1 and day is 0 because we want the last day of the month.
46-
return time.Date(t.Year(), month+1, 0, 0, 0, 0, 0, t.Location()).Day()
55+
return time.Date(year, month+1, 0, 0, 0, 0, 0, time.UTC).Day()
4756
}
4857

4958
// layoutRowsInfo calculates number of rows needed to display input count and number of empty cells in the last row.

serverless/dispatcher/internal/handler/keyboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (h *Handler) monthsKeyboard(languageCode string) *models.InlineKeyboardMark
6565
}
6666

6767
func (h *Handler) daysKeyboard(now time.Time, month int) *models.InlineKeyboardMarkup {
68-
days := daysInMonth(time.Month(month), now)
68+
days := daysInMonth(time.Month(month), now.Year())
6969
rows, empty := layoutRowsInfo(days, daysPerRow)
7070

7171
kb := &models.InlineKeyboardMarkup{InlineKeyboard: make([][]models.InlineKeyboardButton, rows)}

serverless/dispatcher/internal/handler/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (h *Handler) monthQuery(ctx context.Context, b *bot.Bot, update *models.Upd
5151
ChatID: chat.ChatID,
5252
MessageID: update.CallbackQuery.Message.Message.ID,
5353
Text: h.lp.GetText(chat.LanguageCode).PrayerDate,
54-
ReplyMarkup: h.daysKeyboard(h.now(chat.BotID), month),
54+
ReplyMarkup: h.daysKeyboard(h.nowUTC(chat.BotID), month),
5555
})
5656
if err != nil {
5757
log.Error("monthQuery: edit message", log.Err(err), log.BotID(chat.BotID), log.ChatID(chat.ChatID))
@@ -78,8 +78,8 @@ func (h *Handler) dayQuery(ctx context.Context, b *bot.Bot, update *models.Updat
7878
month, _ := strconv.Atoi(parts[1])
7979
day, _ := strconv.Atoi(parts[2])
8080

81-
date := h.now(chat.BotID)
82-
date = domain.Date(day, time.Month(month), date.Year(), time.UTC)
81+
date := h.nowUTC(chat.BotID)
82+
date = domain.Date(day, time.Month(month), date.Year())
8383

8484
prayerDay, err := h.db.GetPrayerDay(ctx, chat.BotID, date)
8585
if err != nil {

serverless/dispatcher/internal/service/db.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ func (db *DB) CreateChat(ctx context.Context, botID int64, chatID int64, languag
5252
DECLARE $language_code AS Utf8;
5353
DECLARE $reminder_offset AS Int32;
5454
DECLARE $state AS Utf8;
55-
DECLARE $subscribed AS Bool;
5655
57-
INSERT INTO chats (bot_id, chat_id, language_code, reminder_offset, state, subscribed, subscribed_at)
58-
VALUES ($bot_id, $chat_id, $language_code, $reminder_offset, $state, $subscribed, CurrentUtcDatetime());
56+
INSERT INTO chats (bot_id, chat_id, language_code, reminder_offset, state, created_at)
57+
VALUES ($bot_id, $chat_id, $language_code, $reminder_offset, $state, CurrentUtcDatetime());
5958
`
6059

6160
params := table.NewQueryParameters(
@@ -64,7 +63,6 @@ func (db *DB) CreateChat(ctx context.Context, botID int64, chatID int64, languag
6463
table.ValueParam("$language_code", types.UTF8Value(languageCode)),
6564
table.ValueParam("$reminder_offset", types.Int32Value(reminderOffset)),
6665
table.ValueParam("$state", types.UTF8Value(state)),
67-
table.ValueParam("$subscribed", types.BoolValue(false)),
6866
)
6967

7068
err := db.client.Do(ctx, func(ctx context.Context, s table.Session) error {

serverless/loader/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/escalopa/prayer-bot/loader
22

33
go 1.21
44

5-
require github.com/escalopa/prayer-bot v0.0.0-20250509151854-3e3cf0f5b265
5+
require github.com/escalopa/prayer-bot v0.0.0-20250509233421-56ffdb42fd63
66

77
require (
88
github.com/aws/aws-sdk-go v1.55.7 // indirect

serverless/loader/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ github.com/escalopa/prayer-bot v0.0.0-20250509132525-37a6d5ff4d9d h1:CYxuerTyBke
587587
github.com/escalopa/prayer-bot v0.0.0-20250509132525-37a6d5ff4d9d/go.mod h1:HOpIxziLKMTC/a+Q6z40Rn5tJDEZsFyzhniLA4iB2i0=
588588
github.com/escalopa/prayer-bot v0.0.0-20250509151854-3e3cf0f5b265 h1:t1Kh/tBb+V42vPsBkiAahTepw5aoLVZgdlg9P1bsHZU=
589589
github.com/escalopa/prayer-bot v0.0.0-20250509151854-3e3cf0f5b265/go.mod h1:HOpIxziLKMTC/a+Q6z40Rn5tJDEZsFyzhniLA4iB2i0=
590+
github.com/escalopa/prayer-bot v0.0.0-20250509233421-56ffdb42fd63 h1:bnel/0nURIdPC3uQ9+C5PPAjaU5HOlKT4kD5dEaDIcU=
591+
github.com/escalopa/prayer-bot v0.0.0-20250509233421-56ffdb42fd63/go.mod h1:HOpIxziLKMTC/a+Q6z40Rn5tJDEZsFyzhniLA4iB2i0=
590592
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
591593
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
592594
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=

0 commit comments

Comments
 (0)