I have a Discord bot with this command.
[Command("synopses", RunMode = RunMode.Async)]
[Summary("Shows current synopses.")]
public async Task Synopses(string username)
{
var warning = await ReplyAsync($"{Context.User.Mention} I am querying the Tracker, this may take a moment.");
var synopses = await _trackerService.GetUserSynopses(username);
var pager = BuildSynopsesPager(synopses);
await warning.DeleteAsync();
await PagedReplyAsync(pager, new ReactionList());
}
The _trackerService.GetUserSynopses(username);
is a very long Task, takes about 10 seconds.
public async Task<List<Synopsis>> GetUserSynopses(string username)
{
var synopses = new List<Synopsis>();
// This request to the Google Sheets API is the bulk of the time.
// It gets about 4k rows.
var tracker = await GetTrackerAsync();
foreach (var sheet in tracker.Sheets)
{
var data = sheet.Data;
var writes = data
.Select(x => x.RowData)
.First()
.Where(x => x.Values.Count >= 11)
.Where(x => x.Values[3].FormattedValue == username);
static bool ParseFinal(string text)
{
return text switch
{
"Yes" => true,
_ => false
};
}
var result = writes
.Select(write => write.Values)
.Select(cells => new Synopsis
{
DateClaimed = cells[0].FormattedValue,
SeriesTitle = cells[1].FormattedValue,
SeriesHyperlink = cells[1].Hyperlink ?? "No Hyperlink",
ClaimType = Enum.Parse<ClaimType>(cells[2].FormattedValue),
Claimant = cells[3].FormattedValue,
Document = cells[4].Hyperlink ?? "No Hyperlink",
Final = ParseFinal(cells[5].FormattedValue)
});
synopses.AddRange(result);
}
return synopses;
}
public async Task<Spreadsheet> GetTrackerAsync()
{
await using var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read);
var credential = GoogleCredential
.FromStream(stream)
.CreateScoped();
var sheetsService = new SheetsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Hiromi"
});
const string spreadsheetId = "1wbIqigHMGFWZebum8mQjSL5eQzSAXuWZS-8ewbFX4PI";
var request = sheetsService
.Spreadsheets
.Get(spreadsheetId);
request.IncludeGridData = true;
request.Ranges = new[] {"'Synopses In Progress'!A3:K", "'Archive'!A2:L"};
return await request.ExecuteAsync();
}
So when I run this command while my application is running locally from my IDE, it works fine no matter how many times I execute. But when my application is running from my VPS, it works the first time, but the second time it executes var warning = await ReplyAsync($"{Context.User.Mention} I am querying the Tracker, this may take a moment.");
and then hangs for a while, and then literally stops.
As you can see
[09:41:13 DBG] Foreign key property 'Message.Id' detected as changed. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see property values.
[09:41:13 DBG] A data reader was disposed.
[09:41:13 DBG] Committing transaction.
[09:41:13 DBG] Committing transaction.
[09:41:13 DBG] Closing connection to database 'hiromi' on server 'tcp://149.28.226.90:5432'.
[09:41:13 DBG] Closed connection to database 'hiromi' on server 'tcp://149.28.226.90:5432'.
[09:41:13 DBG] Disposing transaction.
[09:41:13 DBG] An 'Message' entity tracked by 'HiromiContext' changed from 'Added' to 'Unchanged'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see key values.
[09:41:13 DBG] SaveChanges completed for 'HiromiContext' with 1 entities written to the database.
root@Hamsterland:~/Bots/Hiromi/Hiromi.Bot#
The process terminated at the end but not because I terminated it; it terminated automatically after a few seconds.
Any idea what the problem is?