-
-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathSyncEventHandlers.cs
More file actions
99 lines (86 loc) · 3.58 KB
/
Copy pathSyncEventHandlers.cs
File metadata and controls
99 lines (86 loc) · 3.58 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
99
using AgileConfig.Server.Common.EventBus;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.Event;
using AgileConfig.Server.IService;
using AgileConfig.Server.SyncPlugin;
using AgileConfig.Server.SyncPlugin.Contracts;
using AgileConfig.Server.SyncPlugin.Retry;
using Microsoft.Extensions.Logging;
namespace AgileConfig.Server.EventHandler;
/// <summary>
/// Event handler that syncs published configs to external systems via SyncPlugin
/// Uses "replace all" strategy - always fetches latest configs and replaces all
/// </summary>
public class ConfigSyncEventHandler : IEventHandler<PublishConfigSuccessful>
{
private readonly IConfigService _configService;
private readonly SyncEngine _syncEngine;
private readonly SyncRetryService _retryService;
private readonly Microsoft.Extensions.Logging.ILogger<ConfigSyncEventHandler> _logger;
public ConfigSyncEventHandler(
IConfigService configService,
SyncEngine syncEngine,
SyncRetryService retryService,
Microsoft.Extensions.Logging.ILogger<ConfigSyncEventHandler> logger)
{
_configService = configService;
_syncEngine = syncEngine;
_retryService = retryService;
_logger = logger;
}
public async Task Handle(IEvent evt)
{
var evtInstance = evt as PublishConfigSuccessful;
var timeline = evtInstance.PublishTimeline;
if (timeline == null)
{
_logger.LogWarning("PublishConfigSuccessful event has no timeline");
return;
}
try
{
// Get all published configs for this app and env
var configs = await _configService.GetPublishedConfigsAsync(timeline.AppId, timeline.Env);
if (configs == null || !configs.Any())
{
_logger.LogInformation("No published configs found for app {AppId} env {Env}", timeline.AppId, timeline.Env);
return;
}
// Clear existing failed records for this app+env before new sync
_retryService.ClearFailedRecord(timeline.AppId, timeline.Env);
// Convert to sync contexts
var contexts = configs.Select(c => new SyncContext
{
AppId = c.AppId,
AppName = timeline.AppId,
Env = c.Env,
Key = c.Key,
Value = c.Value ?? "",
Group = c.Group,
OperationType = SyncOperationType.Add,
Timestamp = DateTimeOffset.UtcNow
}).ToArray();
// Full sync using "replace all" strategy
var result = await _syncEngine.SyncAllAsync(contexts);
if (result.Success)
{
_logger.LogInformation("Successfully synced {Count} configs for app {AppId} env {Env}",
contexts.Length, timeline.AppId, timeline.Env);
}
else
{
_logger.LogWarning("Failed to sync configs for app {AppId} env {Env}: {Message}",
timeline.AppId, timeline.Env, result.Message);
// Record for retry
_retryService.RecordFailed(timeline.AppId, timeline.Env, result.Message);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception during config sync for app {AppId} env {Env}",
timeline.AppId, timeline.Env);
// Record for retry
_retryService.RecordFailed(timeline.AppId, timeline.Env, ex.Message);
}
}
}