Skip to content

Commit 1037191

Browse files
committed
Tracer fix for csharp sync.
1 parent b258570 commit 1037191

File tree

2 files changed

+65
-39
lines changed

2 files changed

+65
-39
lines changed

PowerSync/PowerSync.Common/Client/Sync/Stream/StreamingSyncImplementation.cs

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -294,30 +294,37 @@ protected async Task StreamingSync(CancellationToken? signal, PowerSyncConnectio
294294
});
295295
});
296296

297-
/// This loops runs until [retry] is false or the abort signal is set to aborted.
298-
/// Aborting the nestedCts will:
299-
/// - Abort any pending fetch requests
300-
/// - Close any sync stream ReadableStreams (which will also close any established network requests)
297+
// This loops runs until [retry] is false or the abort signal is set to aborted.
298+
// Aborting the nestedCts will:
299+
// - Abort any pending fetch requests
300+
// - Close any sync stream ReadableStreams (which will also close any established network requests)
301301
while (true)
302302
{
303+
Console.WriteLine(1);
303304
UpdateSyncStatus(new SyncStatusOptions { Connecting = true });
305+
Console.WriteLine(2);
304306
var iterationResult = (StreamingSyncIterationResult?)null;
305307
try
306308
{
309+
Console.WriteLine(3);
307310
if (signal.Value.IsCancellationRequested)
308311
{
312+
Console.WriteLine("BREAKINGNNNNG");
309313
break;
310314
}
315+
Console.WriteLine(4);
311316
iterationResult = await StreamingSyncIteration(nestedCts.Token, options);
312-
if (!iterationResult.Retry.GetValueOrDefault(false))
317+
if (!iterationResult.Retry)
313318
{
319+
Console.WriteLine(5);
314320

315321
// A sync error ocurred that we cannot recover from here.
316322
// This loop must terminate.
317323
// The nestedCts will close any open network requests and streams below.
318324
break;
319325
}
320326
// Continue immediately
327+
Console.WriteLine(6);
321328
}
322329
catch (Exception ex)
323330
{
@@ -327,9 +334,9 @@ protected async Task StreamingSync(CancellationToken? signal, PowerSyncConnectio
327334
exMessage = "Stream closed or timed out -" + ex.InnerException.Message;
328335
}
329336

330-
337+
Console.WriteLine(7);
331338
logger.LogError("Caught exception in streaming sync: {message}", exMessage);
332-
339+
Console.WriteLine(8);
333340
// Either:
334341
// - A network request failed with a failed connection or not OKAY response code.
335342
// - There was a sync processing error.
@@ -345,9 +352,14 @@ protected async Task StreamingSync(CancellationToken? signal, PowerSyncConnectio
345352
DownloadError = ex
346353
}
347354
});
355+
356+
Console.WriteLine(9);
357+
await DelayRetry();
358+
Console.WriteLine(10);
348359
}
349360
finally
350361
{
362+
Console.WriteLine(11);
351363
notifyCompletedUploads = null;
352364

353365
if (!signal.Value.IsCancellationRequested)
@@ -358,17 +370,18 @@ protected async Task StreamingSync(CancellationToken? signal, PowerSyncConnectio
358370
}
359371

360372

361-
if (iterationResult != null && iterationResult.ImmediateRestart.GetValueOrDefault(false))
373+
// if (iterationResult != null && iterationResult.ImmediateRestart.GetValueOrDefault(false))
374+
// {
375+
Console.WriteLine(12);
376+
UpdateSyncStatus(new SyncStatusOptions
362377
{
363-
UpdateSyncStatus(new SyncStatusOptions
364-
{
365-
Connected = false,
366-
Connecting = true // May be unnecessary
367-
});
378+
Connected = false,
379+
Connecting = true // May be unnecessary
380+
});
368381

369-
// On error, wait a little before retrying
370-
await DelayRetry();
371-
}
382+
// On error, wait a little before retrying
383+
384+
// }
372385
}
373386
}
374387

@@ -382,7 +395,7 @@ protected async Task StreamingSync(CancellationToken? signal, PowerSyncConnectio
382395

383396
protected record StreamingSyncIterationResult
384397
{
385-
public bool? Retry { get; init; }
398+
public bool Retry { get; init; }
386399

387400
public bool? ImmediateRestart { get; init; }
388401
}
@@ -956,30 +969,37 @@ protected record UpdateSyncStatusOptions(
956969
);
957970
protected void UpdateSyncStatus(SyncStatusOptions options, UpdateSyncStatusOptions? updateOptions = null)
958971
{
959-
var updatedStatus = new SyncStatus(new SyncStatusOptions
972+
try
960973
{
961-
Connected = options.Connected ?? SyncStatus.Connected,
962-
Connecting = !options.Connected.GetValueOrDefault() && (options.Connecting ?? SyncStatus.Connecting),
963-
LastSyncedAt = options.LastSyncedAt ?? SyncStatus.LastSyncedAt,
964-
DataFlow = new SyncDataFlowStatus
974+
var updatedStatus = new SyncStatus(new SyncStatusOptions
965975
{
966-
Uploading = options.DataFlow?.Uploading ?? SyncStatus.DataFlowStatus.Uploading,
967-
Downloading = options.DataFlow?.Downloading ?? SyncStatus.DataFlowStatus.Downloading,
968-
DownloadError = updateOptions?.ClearDownloadError == true ? null : options.DataFlow?.DownloadError ?? SyncStatus.DataFlowStatus.DownloadError,
969-
UploadError = updateOptions?.ClearUploadError == true ? null : options.DataFlow?.UploadError ?? SyncStatus.DataFlowStatus.UploadError,
976+
Connected = options.Connected ?? SyncStatus.Connected,
977+
Connecting = !options.Connected.GetValueOrDefault() && (options.Connecting ?? SyncStatus.Connecting),
978+
LastSyncedAt = options.LastSyncedAt ?? SyncStatus.LastSyncedAt,
979+
DataFlow = new SyncDataFlowStatus
980+
{
981+
Uploading = options.DataFlow?.Uploading ?? SyncStatus.DataFlowStatus.Uploading,
982+
Downloading = options.DataFlow?.Downloading ?? SyncStatus.DataFlowStatus.Downloading,
983+
DownloadError = updateOptions?.ClearDownloadError == true ? null : options.DataFlow?.DownloadError ?? SyncStatus.DataFlowStatus.DownloadError,
984+
UploadError = updateOptions?.ClearUploadError == true ? null : options.DataFlow?.UploadError ?? SyncStatus.DataFlowStatus.UploadError,
985+
}
986+
});
987+
988+
if (!SyncStatus.Equals(updatedStatus))
989+
{
990+
SyncStatus = updatedStatus;
991+
logger.LogDebug("[Sync status updated]: {message}", updatedStatus.ToJSON());
992+
// Only trigger this if there was a change
993+
Emit(new StreamingSyncImplementationEvent { StatusChanged = updatedStatus });
970994
}
971-
});
972995

973-
if (!SyncStatus.Equals(updatedStatus))
996+
// Trigger this for all updates
997+
Emit(new StreamingSyncImplementationEvent { StatusUpdated = options });
998+
}
999+
catch (Exception ex)
9741000
{
975-
SyncStatus = updatedStatus;
976-
logger.LogDebug("[Sync status updated]: {message}", updatedStatus.ToJSON());
977-
// Only trigger this if there was a change
978-
Emit(new StreamingSyncImplementationEvent { StatusChanged = updatedStatus });
1001+
logger.LogError("Error updating sync status: {message}", ex.Message);
9791002
}
980-
981-
// Trigger this for all updates
982-
Emit(new StreamingSyncImplementationEvent { StatusUpdated = options });
9831003
}
9841004

9851005
private static DB.Crud.SyncPriorityStatus CoreStatusToSyncStatus(SyncPriorityStatus status)

PowerSync/PowerSync.Common/DB/Crud/SyncStatus.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace PowerSync.Common.DB.Crud;
22

33
using PowerSync.Common.Client.Sync.Stream;
44
using Newtonsoft.Json;
5+
using Microsoft.Extensions.Options;
56

67
public class SyncDataFlowStatus
78
{
@@ -13,14 +14,14 @@ public class SyncDataFlowStatus
1314
/// Error during downloading (including connecting).
1415
/// Cleared on the next successful data download.
1516
/// </summary>
16-
[JsonProperty("downloadError")]
17+
[JsonIgnore]
1718
public Exception? DownloadError { get; set; } = null;
1819

1920
/// <summary>
2021
/// Error during uploading.
2122
/// Cleared on the next successful upload.
2223
/// </summary>
23-
[JsonProperty("uploadError")]
24+
[JsonIgnore]
2425
public Exception? UploadError { get; set; } = null;
2526

2627

@@ -151,9 +152,14 @@ public SyncPriorityStatus StatusForPriority(int priority)
151152
};
152153
}
153154

155+
private string SerializeObject()
156+
{
157+
return JsonConvert.SerializeObject(new { Options = Options, UploadErrorMessage = Options.DataFlow?.UploadError?.Message, DownloadErrorMessage = DataFlowStatus.DownloadError?.Message });
158+
}
159+
154160
public bool IsEqual(SyncStatus status)
155161
{
156-
return JsonConvert.SerializeObject(Options) == JsonConvert.SerializeObject(status.Options);
162+
return this.SerializeObject() == status.SerializeObject();
157163
}
158164

159165
public string GetMessage()
@@ -165,7 +171,7 @@ public string GetMessage()
165171

166172
public string ToJSON()
167173
{
168-
return JsonConvert.SerializeObject(this);
174+
return SerializeObject();
169175
}
170176

171177
private static int ComparePriorities(SyncPriorityStatus a, SyncPriorityStatus b)

0 commit comments

Comments
 (0)