Created
February 5, 2021 15:05
-
-
Save dj-nitehawk/40b873897f0665af4988ea73b75a937c to your computer and use it in GitHub Desktop.
resume token demo for watcher/change-stream
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
using MongoDB.Bson; | |
using MongoDB.Entities; | |
using System; | |
using System.IO; | |
using System.Threading.Tasks; | |
namespace TestApplication | |
{ | |
public class Book : Entity | |
{ | |
public string Title { get; set; } | |
} | |
public static class Program | |
{ | |
private static async Task Main() | |
{ | |
await DB.InitAsync("test"); | |
var watcher = DB.Watcher<Book>("book-watcher"); | |
watcher.OnChanges += books => | |
{ | |
foreach (var book in books) | |
{ | |
Console.WriteLine($"received: {book.Title}"); | |
} | |
}; | |
if (File.Exists("resume.token")) | |
{ | |
var resumeToken = BsonDocument.Parse(File.ReadAllText("resume.token")); | |
watcher.StartWithToken(resumeToken, EventType.Created, batchSize: 10); | |
} | |
else | |
{ | |
watcher.Start(EventType.Created, batchSize: 10); | |
for (int i = 1; i <= 20; i++) | |
{ | |
await new Book { Title = $"book {i}" }.SaveAsync(); | |
await Task.Delay(500); | |
if (i == 10) | |
{ | |
File.WriteAllText("resume.token", watcher.ResumeToken.ToJson()); | |
} | |
} | |
} | |
Console.WriteLine("press any key to exit..."); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment