@@ -52,7 +52,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
5252 {
5353 m_logger . LogInformation ( "Starting SyncService..." ) ;
5454
55- await InititalizeAsync ( ) ;
55+ await InititalizeAsync ( ) . ConfigureAwait ( false ) ;
5656
5757 if ( m_syncOptions . SyncDirection == SyncDirection . DiskToKepware )
5858 {
@@ -67,28 +67,37 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
6767 {
6868 try
6969 {
70- if ( await m_kepServerClient . TestConnectionAsync ( stoppingToken ) )
70+ if ( await m_kepServerClient . TestConnectionAsync ( stoppingToken ) . ConfigureAwait ( false ) )
7171 {
72- if ( m_syncOptions . SyncMode == SyncMode . TwoWay ||
73- m_syncOptions . SyncDirection == SyncDirection . KepwareToDisk )
72+ if ( m_changeQueue . TryDequeue ( out var changeEvent ) )
7473 {
75- if ( m_changeQueue . TryDequeue ( out var changeEvent ) )
74+ // check if there are more events for the same source
75+ while ( m_changeQueue . TryPeek ( out var nextPending ) && nextPending . Source == changeEvent ! . Source )
7676 {
77- while ( m_changeQueue . TryPeek ( out var nextPending ) && nextPending . Source == changeEvent ! . Source )
78- {
79- m_changeQueue . TryDequeue ( out changeEvent ) ;
80- }
81- await ProcessChangeAsync ( changeEvent ! , stoppingToken ) ;
77+ m_changeQueue . TryDequeue ( out changeEvent ) ;
78+ }
79+ // Process the change event
80+ await ProcessChangeAsync ( changeEvent ! , stoppingToken ) . ConfigureAwait ( false ) ;
81+ }
82+ // We don't need to read the project Id in "DiskToKepware"-only mode, due to the fact that we are not syncing from Kepware to disk
83+ else if ( m_syncOptions . SyncMode == SyncMode . TwoWay || m_syncOptions . SyncDirection != SyncDirection . DiskToKepware )
84+ {
85+ // If we are in two-way sync or Kepware to disk mode, we need to check the current project ID
86+ var currentProjectId = await FetchCurrentProjectIdAsync ( m_kepServerClient , stoppingToken ) . ConfigureAwait ( false ) ;
87+ if ( m_lastProjectId != currentProjectId )
88+ {
89+ // If the project ID has changed, we need to sync from Kepware to disk
90+ await ProcessChangeAsync ( new ChangeEvent { Source = ChangeSource . PrimaryKepServer , Reason = "Project changed" } , stoppingToken ) . ConfigureAwait ( false ) ;
8291 }
8392 else
8493 {
85- var currentProjectId = await FetchCurrentProjectIdAsync ( m_kepServerClient , stoppingToken ) ;
86- if ( m_lastProjectId != currentProjectId )
87- {
88- await ProcessChangeAsync ( new ChangeEvent { Source = ChangeSource . PrimaryKepServer , Reason = "Project changed" } , stoppingToken ) ;
89- }
94+ // No changes to process, but we still want to check the connection
9095 }
9196 }
97+ else
98+ {
99+ // No changes to process, but we still want to check the connection
100+ }
92101
93102 blnFirstDisconnect = true ;
94103 }
@@ -270,22 +279,30 @@ private async Task SyncProjectToKepServerAsync(string projectSource, Project pro
270279 targetOptions . OverwriteConfigFile , projectSource , clientName , kepServerClient . ClientHostName ) ;
271280
272281 project = await project . CloneAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
273- var overwrite = await RuntimeOverwriteConfig . LoadFromYamlFileAsync ( targetOptions . OverwriteConfigFile , cancellationToken ) . ConfigureAwait ( false ) ;
282+ var overwrite = await RuntimeOverwriteConfig . LoadFromYamlFileAsync ( targetOptions . OverwriteConfigFile , cancellationToken ) . ConfigureAwait ( false ) ;
274283 overwrite . Apply ( project ) ;
275284 }
276285
277- var ( inserts , updates , deletes ) = await kepServerClient . Project . CompareAndApply ( project , cancellationToken ) . ConfigureAwait ( false ) ;
278-
279- if ( updates > 0 || deletes > 0 || inserts > 0 )
286+ if ( await kepServerClient . TestConnectionAsync ( cancellationToken ) . ConfigureAwait ( false ) )
280287 {
281- m_logger . LogInformation ( "Completed synchronisation from {ProjectSource} to {ClientName}-kepserver ({ClientHostName}): {NumUpdates} updates, {NumInserts} inserts, {NumDeletes} deletes" ,
282- projectSource , clientName , kepServerClient . ClientHostName , updates , inserts , deletes ) ;
283- onSyncedWithChanges ? . Invoke ( ) ;
288+ var ( inserts , updates , deletes ) = await kepServerClient . Project . CompareAndApply ( project , cancellationToken ) . ConfigureAwait ( false ) ;
289+
290+ if ( updates > 0 || deletes > 0 || inserts > 0 )
291+ {
292+ m_logger . LogInformation ( "Completed synchronisation from {ProjectSource} to {ClientName}-kepserver ({ClientHostName}): {NumUpdates} updates, {NumInserts} inserts, {NumDeletes} deletes" ,
293+ projectSource , clientName , kepServerClient . ClientHostName , updates , inserts , deletes ) ;
294+ onSyncedWithChanges ? . Invoke ( ) ;
295+ }
296+ else
297+ {
298+ m_logger . LogInformation ( "Completed synchronisation from {ProjectSource} to {ClientName}-kepserver ({ClientHostName}):: no changes made" ,
299+ projectSource , clientName , kepServerClient . ClientHostName ) ;
300+ }
284301 }
285302 else
286303 {
287- m_logger . LogInformation ( "Completed synchronisation from {ProjectSource} to {ClientName}-kepserver ({ClientHostName}):: no changes made" ,
288- projectSource , clientName , kepServerClient . ClientHostName ) ;
304+ // No connection to the kepware server, log a warning that the sync could not be performed (conection error is alread logged)
305+ m_logger . LogWarning ( "No connection to {ClientName}-kepserver ({ClientHostName}). Sync from {ProjectSource} skipped." , clientName , kepServerClient . ClientHostName , projectSource ) ;
289306 }
290307 }
291308
0 commit comments