-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (83 loc) · 2.66 KB
/
Copy pathProgram.cs
File metadata and controls
98 lines (83 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Replane;
Console.WriteLine("=== Replane Background Worker Example ===");
Console.WriteLine("This example demonstrates real-time config updates.\n");
// Create the client
await using var client = new ReplaneClient(new ReplaneClientOptions
{
Defaults = new Dictionary<string, object?>
{
["worker-enabled"] = true,
["batch-size"] = 100,
["interval-seconds"] = 30
}
});
// Subscribe to config changes using ConfigChanged event
client.ConfigChanged += (sender, e) =>
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Config changed: {e.ConfigName}");
// Handle specific config changes
if (e.ConfigName == "batch-size")
{
var newBatchSize = e.GetValue<int>();
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Batch size updated to: {newBatchSize}");
Console.WriteLine(" -> Worker will adjust batch processing...");
}
};
try
{
Console.WriteLine("Connecting to Replane...");
await client.ConnectAsync(new ConnectOptions
{
BaseUrl = Environment.GetEnvironmentVariable("REPLANE_BASE_URL")
?? "https://your-replane-server.com",
SdkKey = Environment.GetEnvironmentVariable("REPLANE_SDK_KEY")
?? "your-sdk-key"
});
Console.WriteLine("Connected! Subscribed to config changes.\n");
}
catch (ReplaneException ex)
{
Console.WriteLine($"Note: Running with defaults ({ex.Message})\n");
}
// Simulate a background worker
Console.WriteLine("Starting background worker...");
Console.WriteLine("Press Ctrl+C to stop.\n");
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
cts.Cancel();
};
var iteration = 0;
while (!cts.Token.IsCancellationRequested)
{
iteration++;
// Read current config values
var enabled = client.Get<bool>("worker-enabled");
var batchSize = client.Get<int>("batch-size");
var interval = client.Get<int>("interval-seconds");
if (!enabled)
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Worker is disabled, skipping...");
}
else
{
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Processing batch #{iteration} (size: {batchSize})");
// Simulate work
await Task.Delay(500, cts.Token);
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Batch #{iteration} completed");
}
// Wait for next interval
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Waiting {interval} seconds...\n");
try
{
await Task.Delay(TimeSpan.FromSeconds(interval), cts.Token);
}
catch (OperationCanceledException)
{
break;
}
}
// Cleanup
Console.WriteLine("\nShutting down...");
Console.WriteLine("Done!");