Demonstrates using Replane in a long-running background service with real-time config updates.
- .NET 8.0 SDK or later
- A running Replane server
- An SDK key from your Replane project
-
Copy this directory to your local machine:
cp -r BackgroundWorker ~/my-replane-example cd ~/my-replane-example
-
Set your environment variables:
export REPLANE_BASE_URL="https://your-replane-server.com" export REPLANE_SDK_KEY="your-sdk-key"
-
Restore packages:
dotnet restore
dotnet runPress Ctrl+C to stop the worker.
Subscribe to config changes and react in real-time:
// Subscribe to ALL config changes
var unsubscribe = client.Subscribe((configName, config) =>
{
Console.WriteLine($"Config changed: {configName} = {config.Value}");
});
// Subscribe to a SPECIFIC config
var unsubscribe = client.SubscribeConfig("batch-size", config =>
{
Console.WriteLine($"Batch size is now: {config.Value}");
});Both Subscribe() and SubscribeConfig() return an unsubscribe function:
var unsubscribe = client.Subscribe(...);
// Later, when you want to stop receiving updates:
unsubscribe();The Replane client maintains a persistent SSE connection:
- Config changes are pushed from the server instantly
- No polling required
- Subscriptions fire immediately when configs change
This example shows how to dynamically adjust worker behavior:
worker-enabled: Toggle the worker on/offbatch-size: Change processing batch sizeinterval-seconds: Adjust the polling interval
=== Replane Background Worker Example ===
This example demonstrates real-time config updates.
Connecting to Replane...
Connected! Subscribed to config changes.
Starting background worker...
Press Ctrl+C to stop.
[14:30:00] Processing batch #1 (size: 100)
[14:30:00] Batch #1 completed
[14:30:00] Waiting 30 seconds...
[14:30:15] Config changed: batch-size = 200
[14:30:15] Batch size updated to: 200
-> Worker will adjust batch processing...
[14:30:30] Processing batch #2 (size: 200)
[14:30:30] Batch #2 completed
[14:30:30] Waiting 30 seconds...
- Background job processors: Adjust batch sizes, intervals dynamically
- Queue consumers: Enable/disable consumption without restart
- Scheduled tasks: Change cron expressions at runtime
- Feature flags: Enable new features for background processes
- Rate limiting: Dynamically adjust rate limits
- Always unsubscribe when shutting down to prevent memory leaks
- Handle exceptions in subscription callbacks
- Don't block the subscription callback - offload heavy work
- Use specific subscriptions when you only care about one config