Skip to content

Commit

Permalink
Allow MongoDB to be unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed May 28, 2024
1 parent b4e4061 commit 408c086
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ protected override async Task<IReadOnlyDictionary<string, bool>> LoadBatchAsync(
IReadOnlyList<string> projectCodes,
CancellationToken cancellationToken)
{
if (!await _systemDbContext.IsAvailable())
return new Dictionary<string, bool>();

return await MongoExtensions.ToAsyncEnumerable(_systemDbContext.Projects.AsQueryable()
.Select(p => p.ProjectCode)
.Where(projectCode => projectCodes.Contains(projectCode)))

Check warning on line 30 in backend/LexBoxApi/GraphQL/CustomTypes/IsLanguageForgeProjectDataLoader.cs

View workflow job for this annotation

GitHub Actions / Build API / publish-api

Method referencing lambda parameter is not supported LINQ expression. (https://www.mongodb.com/docs/mongodb-analyzer/current/rules/#MALinq2001)
Expand Down
14 changes: 14 additions & 0 deletions backend/LfClassicData/SystemDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LfClassicData.Entities;
using MongoDB.Bson;
using MongoDB.Driver;

namespace LfClassicData;
Expand All @@ -7,6 +8,7 @@ public class SystemDbContext
{
public const string SystemDbName = "scriptureforge";
private readonly IMongoDatabase _mongoDatabase;
private bool? _isAvailable;

public SystemDbContext(MongoClient mongoClient)
{
Expand All @@ -17,4 +19,16 @@ public SystemDbContext(MongoClient mongoClient)

internal IMongoCollection<User> Users { get; }
public IMongoCollection<LfProject> Projects { get; }

public async Task<bool> IsAvailable()
{
if (_isAvailable is null or false)
{
_isAvailable = await Task.Run(() =>
{
return _mongoDatabase.RunCommandAsync((Command<BsonDocument>)"{ping:1}").Wait(100);
});
}
return _isAvailable.Value;
}
}

0 comments on commit 408c086

Please sign in to comment.