Skip to content

Commit

Permalink
Merge pull request #61 from moreal/fix-sync-context
Browse files Browse the repository at this point in the history
fix(worker): update `LatestBlockIndex` if it doesn't exist
  • Loading branch information
moreal authored May 16, 2024
2 parents a44bf0c + 4ffd7b6 commit 857a861
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Mimir.Worker/Models/SyncContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MongoDB.Bson.Serialization.Attributes;

namespace Mimir.Worker.Models;

public class SyncContext
{
[BsonId]
public string Id { get; } = "SyncContext";
public long LatestBlockIndex { get; set; }
}
12 changes: 10 additions & 2 deletions Mimir.Worker/Services/MongoDbStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ public async Task UpdateLatestBlockIndex(long blockIndex)
_logger.LogInformation($"Update latest block index to {blockIndex}");
var filter = Builders<BsonDocument>.Filter.Eq("_id", "SyncContext");
var update = Builders<BsonDocument>.Update.Set("LatestBlockIndex", blockIndex);
var updateModel = new UpdateOneModel<BsonDocument>(filter, update);
await MetadataCollection.BulkWriteAsync(new[] { updateModel });

var response = await MetadataCollection.UpdateOneAsync(filter, update);
if (response?.ModifiedCount < 1)
{
await MetadataCollection.InsertOneAsync(
new SyncContext
{
LatestBlockIndex = blockIndex,
}.ToBsonDocument());
}
}

public async Task<long> GetLatestBlockIndex()
Expand Down

0 comments on commit 857a861

Please sign in to comment.