-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add donations worker, polling new donations on an interval
- Loading branch information
Showing
7 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using TPP.Core.Streamlabs; | ||
using TPP.Model; | ||
using TPP.Persistence; | ||
|
||
namespace TPP.Core; | ||
|
||
public sealed class DonationsWorker( | ||
ILoggerFactory loggerFactory, | ||
TimeSpan pollingInterval, | ||
StreamlabsClient streamlabsClient, | ||
IDonationRepo donationRepo, | ||
DonationHandler donationHandler | ||
) : IWithLifecycle | ||
{ | ||
private readonly ILogger<ChattersWorker> _logger = loggerFactory.CreateLogger<ChattersWorker>(); | ||
|
||
public async Task Start(CancellationToken cancellationToken) | ||
{ | ||
try { await Task.Delay(pollingInterval, cancellationToken); } | ||
catch (OperationCanceledException) { return; } | ||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
try | ||
{ | ||
Donation? mostRecentDonation = await donationRepo.GetMostRecentDonation(); | ||
_logger.LogDebug("Polling for new donations... most recent one is {Donation}", mostRecentDonation); | ||
List<StreamlabsClient.Donation> donations = | ||
await streamlabsClient.GetDonations(after: mostRecentDonation?.DonationId, currency: "USD"); | ||
_logger.LogDebug("Received new donations: {Donations}", string.Join(", ", donations)); | ||
foreach (var donation in donations.OrderBy(d => d.CreatedAt)) // process in chronological order | ||
await donationHandler.Process(DonationHandler.NewDonation.FromStreamlabs(donation)); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Failed polling for new donations"); | ||
} | ||
|
||
try { await Task.Delay(pollingInterval, cancellationToken); } | ||
catch (OperationCanceledException) { break; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,9 @@ | |
"string", | ||
"null" | ||
] | ||
}, | ||
"PollingInterval": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters