added bot манагер
This commit is contained in:
parent
e4862eeef5
commit
c4793bcfa9
39
bot_manager/manager.v
Normal file
39
bot_manager/manager.v
Normal file
@ -0,0 +1,39 @@
|
||||
module bot_manager
|
||||
|
||||
import repository
|
||||
import tg_handle
|
||||
import context
|
||||
import models
|
||||
|
||||
pub struct BotManager {
|
||||
pub mut:
|
||||
active_bots map[i64]&tg_handle.TgBot
|
||||
repo repository.Repo
|
||||
}
|
||||
|
||||
pub fn new_bot_manager(repo repository.Repo) BotManager {
|
||||
return BotManager{
|
||||
active_bots: map[i64]&tg_handle.TgBot{}
|
||||
repo: repo
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut bm BotManager) manage_bots(new_bots map[i64]models.TelegramIntegration, mut ctx context.Context) ! {
|
||||
// перебираем переданную мапку, сраавниваем то что работает с тем что пришло, дублей быть не должно
|
||||
for bot_id, bot_data in new_bots {
|
||||
if bot_id !in bm.active_bots {
|
||||
bm.start_bot(bot_data, mut ctx)!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut bm BotManager) start_bot(bot models.TelegramIntegration, mut ctx context.Context) ! {
|
||||
// доп проверка ну а вдруг
|
||||
if bot.id in bm.active_bots {
|
||||
eprintln('already started: $bot.id')
|
||||
return
|
||||
}
|
||||
tg_bot := tg_handle.new_tg_bot(bot.bot_token, bm.repo, mut ctx)
|
||||
bm.active_bots[bot.id] = tg_bot
|
||||
println('started: $bot.id')
|
||||
}
|
@ -5,12 +5,14 @@ import time
|
||||
import models
|
||||
import dariotarantini.vgram
|
||||
import repository
|
||||
import bot_manager
|
||||
|
||||
pub struct InstanceWorker {
|
||||
pub mut:
|
||||
instance_id int
|
||||
tg_sender vgram.Bot
|
||||
repo repository.Repo
|
||||
bot_manager bot_manager.BotManager
|
||||
}
|
||||
|
||||
pub fn (mut worker InstanceWorker) start(mut ctx context.Context) {
|
||||
@ -76,6 +78,12 @@ pub fn (mut worker InstanceWorker) start(mut ctx context.Context) {
|
||||
worker.repo.pg_db.exec('COMMIT') or { panic(err) }
|
||||
// обновляем количество итераций
|
||||
count += 1
|
||||
// todo доработать пункты 4 последних юху
|
||||
worker.bot_manager.manage_bots(current_bots,mut ctx)or{
|
||||
eprintln(err)
|
||||
return
|
||||
}
|
||||
|
||||
// спим 5 минут перед следующим запуском
|
||||
time.sleep(models.check_interval)
|
||||
}
|
||||
@ -97,6 +105,10 @@ fn (mut worker InstanceWorker) get_max_checkout() !i64 {
|
||||
return error('Failed to get max checkout ${err}')
|
||||
}
|
||||
|
||||
if selected_members.len == 0{
|
||||
return time.now().unix()
|
||||
}
|
||||
|
||||
return selected_members[0].checkout
|
||||
}
|
||||
|
||||
|
4
main.v
4
main.v
@ -3,7 +3,6 @@ import json
|
||||
import log
|
||||
import os
|
||||
import models
|
||||
import tg_handle
|
||||
import repository
|
||||
import context
|
||||
import time
|
||||
@ -12,6 +11,7 @@ import veb
|
||||
import controllers
|
||||
import instance_worker
|
||||
import dariotarantini.vgram
|
||||
import bot_manager
|
||||
|
||||
//v -enable-globals -cc gcc -cflags "-IC:/PostgreSQL/16/include -LC:/PostgreSQL/16/lib" run main.v
|
||||
__global (
|
||||
@ -47,11 +47,11 @@ fn main() {
|
||||
})or {
|
||||
eprintln("err conect to db")
|
||||
}
|
||||
tg_handle.new_tg_bot("6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8",repo,mut ctx)
|
||||
|
||||
mut wc := instance_worker.InstanceWorker{
|
||||
repo: repo
|
||||
tg_sender: vgram.new_bot(models.telegram_bot_token)
|
||||
bot_manager: bot_manager.new_bot_manager(repo)
|
||||
}
|
||||
|
||||
wc.start(mut ctx)
|
||||
|
@ -15,14 +15,16 @@ pub struct TgBot {
|
||||
|
||||
// todo нужно наверное ограничить возврат пользователя на предыдущие вопросы, потому что храним только
|
||||
// последние положение, а предыдущие не храним, наверное стоит потом с базой продумать
|
||||
// нужно канал передавать вместо контекста чтоб завершить прицельно
|
||||
|
||||
pub fn new_tg_bot(token string,repo repository.Repo,mut ctx context.Context){
|
||||
pub fn new_tg_bot(token string,repo repository.Repo,mut ctx context.Context) &TgBot{
|
||||
bot := vgram.new_bot(token)
|
||||
mut b := &TgBot{
|
||||
bot: bot
|
||||
repo: repo
|
||||
}
|
||||
spawn b.start(mut ctx)
|
||||
return b
|
||||
}
|
||||
|
||||
fn (mut b TgBot) start(mut ctx context.Context) ! {
|
||||
|
Loading…
Reference in New Issue
Block a user