mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-22 05:54:36 +00:00
Fixed crash in BackgroundWorker
Additionally, added an indicator for showing that the background task is still running, in case this error occurs again. Restructured the server reporting task.
This commit is contained in:
parent
4a9149d726
commit
bedcf8ad2b
1 changed files with 50 additions and 17 deletions
|
@ -35,21 +35,24 @@ Class BackgroundWorker
|
||||||
''' Background task. Kicks off many other tasks.
|
''' Background task. Kicks off many other tasks.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
Private Async Function WorkerLoop() As Task
|
Private Async Function WorkerLoop() As Task
|
||||||
Try
|
While Not WorkerCancel.IsCancellationRequested
|
||||||
While Not WorkerCancel.IsCancellationRequested
|
Try
|
||||||
' Start background tasks.
|
' Start background tasks.
|
||||||
Dim bgTasks As New List(Of Task) From {
|
Dim bgTasks As New List(Of Task) From {
|
||||||
ReportServerCount(),
|
ReportAsync(),
|
||||||
BirthdayAsync()
|
BirthdayAsync()
|
||||||
}
|
}
|
||||||
Await Task.WhenAll(bgTasks)
|
Await Task.WhenAll(bgTasks)
|
||||||
|
|
||||||
' All done. Wait until we have to work again.
|
' All done. Wait until we have to work again.
|
||||||
Await Task.Delay(Interval * 1000, WorkerCancel.Token)
|
Await Task.Delay(Interval * 1000, WorkerCancel.Token)
|
||||||
End While
|
Catch ex As TaskCanceledException
|
||||||
Catch ex As TaskCanceledException
|
Return
|
||||||
Return
|
Catch ex As Exception
|
||||||
End Try
|
Log("Background task", "Unhandled exception in background task thread:")
|
||||||
|
Log("Background task", ex.ToString())
|
||||||
|
End Try
|
||||||
|
End While
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
#Region "Birthday handling"
|
#Region "Birthday handling"
|
||||||
|
@ -83,7 +86,7 @@ Class BackgroundWorker
|
||||||
guilds += 1
|
guilds += 1
|
||||||
End If
|
End If
|
||||||
Next
|
Next
|
||||||
If announces > 0 Then Log("Background", $"Announcing {announces} birthday(s) in {guilds} guild(s).")
|
If announces > 0 Then Log("Birthday task", $"Announcing {announces} birthday(s) in {guilds} guild(s).")
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
|
@ -115,7 +118,16 @@ Class BackgroundWorker
|
||||||
' Note: Don't quit here if zero people are having birthdays. Roles may still need to be removed by BirthdayApply.
|
' Note: Don't quit here if zero people are having birthdays. Roles may still need to be removed by BirthdayApply.
|
||||||
|
|
||||||
' Set birthday role, get list of users now having birthdays
|
' Set birthday role, get list of users now having birthdays
|
||||||
Dim announceNames = Await BirthdayApplyAsync(guild, role, birthdays)
|
Dim announceNames As IEnumerable(Of SocketGuildUser)
|
||||||
|
Try
|
||||||
|
announceNames = Await BirthdayApplyAsync(guild, role, birthdays)
|
||||||
|
Catch ex As Exception
|
||||||
|
SyncLock _bot.KnownGuilds
|
||||||
|
' Failed to apply role. Reset the warning.
|
||||||
|
_bot.KnownGuilds(guild.Id).RoleWarning = True
|
||||||
|
End SyncLock
|
||||||
|
Return 0
|
||||||
|
End Try
|
||||||
If announceNames.Count <> 0 Then
|
If announceNames.Count <> 0 Then
|
||||||
' Send out announcement message
|
' Send out announcement message
|
||||||
Await BirthdayAnnounceAsync(guild, channel, announceNames)
|
Await BirthdayAnnounceAsync(guild, channel, announceNames)
|
||||||
|
@ -233,14 +245,35 @@ Class BackgroundWorker
|
||||||
#End Region
|
#End Region
|
||||||
|
|
||||||
#Region "Activity reporting"
|
#Region "Activity reporting"
|
||||||
Private _activityCount As Integer = 0
|
''' <summary>
|
||||||
Private Async Function ReportServerCount() As Task
|
''' Increasing value for regulating how often certain tasks are done.
|
||||||
' Once roughly every 6 hours, with an initial ~2 minute delay. Be mindful of the interval value.
|
''' For anything relying on this value, also be mindful of the interval value.
|
||||||
_activityCount += 1
|
''' </summary>
|
||||||
If _activityCount Mod 400 <> 2 Then Return
|
Private _reportTick As Integer = 0
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Handles various periodic reporting tasks.
|
||||||
|
''' </summary>
|
||||||
|
Private Async Function ReportAsync() As Task
|
||||||
|
ReportHeartbeat(_reportTick)
|
||||||
|
Await ReportGuildCount(_reportTick)
|
||||||
|
|
||||||
|
_reportTick += 1
|
||||||
|
End Function
|
||||||
|
|
||||||
|
Private Sub ReportHeartbeat(tick As Integer)
|
||||||
|
' Roughly every 15 minutes (interval: 45)
|
||||||
|
If tick Mod 20 = 0 Then
|
||||||
|
Log("Background task", $"Still alive! Tick: {_reportTick}.")
|
||||||
|
End If
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Private Async Function ReportGuildCount(tick As Integer) As Task
|
||||||
|
' Roughly every 5 hours (interval: 45)
|
||||||
|
If tick Mod 400 <> 2 Then Return
|
||||||
|
|
||||||
Dim count = _bot.DiscordClient.Guilds.Count
|
Dim count = _bot.DiscordClient.Guilds.Count
|
||||||
Log("Activity Report", $"Currently in {count} guild(s).")
|
Log("Report", $"Currently in {count} guild(s).")
|
||||||
|
|
||||||
Dim dtok = _bot._cfg.DBotsToken
|
Dim dtok = _bot._cfg.DBotsToken
|
||||||
If dtok IsNot Nothing Then
|
If dtok IsNot Nothing Then
|
||||||
|
@ -253,9 +286,9 @@ Class BackgroundWorker
|
||||||
client.Headers(HttpRequestHeader.ContentType) = "application/json"
|
client.Headers(HttpRequestHeader.ContentType) = "application/json"
|
||||||
Try
|
Try
|
||||||
Await client.UploadStringTaskAsync(uri, data)
|
Await client.UploadStringTaskAsync(uri, data)
|
||||||
Log("Activity Report", "Count sent to Discord Bots.")
|
Log("Server Count", "Count sent to Discord Bots.")
|
||||||
Catch ex As WebException
|
Catch ex As WebException
|
||||||
Log("Activity Report", "Encountered error on sending to Discord Bots: " + ex.Message)
|
Log("Server Count", "Encountered error on sending to Discord Bots: " + ex.Message)
|
||||||
End Try
|
End Try
|
||||||
End Using
|
End Using
|
||||||
End If
|
End If
|
||||||
|
|
Loading…
Reference in a new issue