|
| 1 | +package viewer |
| 2 | + |
| 3 | +import ( |
| 4 | + "darkbot/app/settings/logus" |
| 5 | + "darkbot/app/settings/types" |
| 6 | + "darkbot/app/settings/utils" |
| 7 | + "darkbot/app/settings/worker" |
| 8 | + "darkbot/app/settings/worker/worker_types" |
| 9 | + "darkbot/app/viewer/apis" |
| 10 | + "fmt" |
| 11 | + "sync" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type TaskRefreshChannel struct { |
| 16 | + *worker.Task |
| 17 | + |
| 18 | + // any desired arbitary data |
| 19 | + api *apis.API |
| 20 | + channelID types.DiscordChannelID |
| 21 | + delayBetweenChannels types.ViewerDelayBetweenChannels |
| 22 | +} |
| 23 | + |
| 24 | +func NewRefreshChannelTask( |
| 25 | + api *apis.API, |
| 26 | + channelID types.DiscordChannelID, |
| 27 | + delayBetweenChannels types.ViewerDelayBetweenChannels, |
| 28 | +) *TaskRefreshChannel { |
| 29 | + task_id_gen += 1 |
| 30 | + return &TaskRefreshChannel{ |
| 31 | + Task: worker.NewTask(worker_types.TaskID(task_id_gen)), |
| 32 | + api: api, |
| 33 | + channelID: channelID, |
| 34 | + delayBetweenChannels: delayBetweenChannels, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +var task_id_gen int = 0 |
| 39 | + |
| 40 | +var guildAntiRateLimitMutexes map[string]*sync.Mutex |
| 41 | + |
| 42 | +func init() { |
| 43 | + guildAntiRateLimitMutexes = make(map[string]*sync.Mutex) |
| 44 | +} |
| 45 | + |
| 46 | +func GetMutex(MutexKey string) *sync.Mutex { |
| 47 | + value, ok := guildAntiRateLimitMutexes[MutexKey] |
| 48 | + |
| 49 | + if ok { |
| 50 | + return value |
| 51 | + } |
| 52 | + |
| 53 | + new_mutex := &sync.Mutex{} |
| 54 | + guildAntiRateLimitMutexes[MutexKey] = new_mutex |
| 55 | + return new_mutex |
| 56 | +} |
| 57 | + |
| 58 | +func (v *TaskRefreshChannel) RunTask(worker_id worker_types.WorkerID) worker_types.TaskStatusCode { |
| 59 | + channel_info, err := v.api.Discorder.GetDiscordSession().Channel(string(v.channelID)) |
| 60 | + if logus.CheckError(err, "unable to get channel info") { |
| 61 | + return worker.CodeFailure |
| 62 | + } |
| 63 | + |
| 64 | + MutexKey := channel_info.GuildID |
| 65 | + GuildMutex := GetMutex(MutexKey) |
| 66 | + GuildMutex.Lock() |
| 67 | + defer GuildMutex.Unlock() |
| 68 | + |
| 69 | + time_run_task_started := time.Now() |
| 70 | + time_new_channel := utils.NewTimeMeasure("new_channel", logus.ChannelID(v.channelID)) |
| 71 | + channel := NewChannelView(v.api, v.channelID) |
| 72 | + |
| 73 | + time_new_channel.Close() |
| 74 | + |
| 75 | + time_render := utils.NewTimeMeasure("channel.Render", logus.ChannelID(v.channelID)) |
| 76 | + channel.Render() |
| 77 | + time_render.Close() |
| 78 | + |
| 79 | + time_discover := utils.NewTimeMeasure("channel.Discover", logus.ChannelID(v.channelID)) |
| 80 | + err = channel.Discover() |
| 81 | + time_discover.Close() |
| 82 | + |
| 83 | + if logus.CheckWarn(err, "unable to grab Discord msgs", logus.ChannelID(v.channelID)) { |
| 84 | + return worker.CodeFailure |
| 85 | + } |
| 86 | + |
| 87 | + time_send := utils.NewTimeMeasure("channel.Send", logus.ChannelID(v.channelID)) |
| 88 | + channel.Send() |
| 89 | + time_send.Close() |
| 90 | + |
| 91 | + time_delete_old := utils.NewTimeMeasure("channel.DeleteOld", logus.ChannelID(v.channelID)) |
| 92 | + channel.DeleteOld() |
| 93 | + time_delete_old.Close() |
| 94 | + v.SetAsDone() |
| 95 | + logus.Info(fmt.Sprintf("RunTask finished, TaskID=%d, elapsed=%s, started_at=%s, finished_at=%s", |
| 96 | + v.Task.GetID(), |
| 97 | + time.Since(time_run_task_started).String(), |
| 98 | + time_run_task_started.String(), |
| 99 | + time.Now().String(), |
| 100 | + )) |
| 101 | + |
| 102 | + // Important for Mutex above! Prevents Guild level rate limits. looks like 5 msg edits per 5 second at one server is good |
| 103 | + time.Sleep(time.Duration(v.delayBetweenChannels) * time.Second) |
| 104 | + return worker.CodeSuccess |
| 105 | +} |
0 commit comments