Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 113 additions & 4 deletions uSync.BackOffice/Hubs/uSyncCallbacks.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using uSync.BackOffice.Models;

Expand All @@ -11,26 +12,52 @@ namespace uSync.BackOffice;
public delegate void SyncEventCallback(SyncProgressSummary summary);

/// <summary>
/// callback delegate for SignalR messaging
/// async callback delegate for SignalR messaging
/// </summary>
public delegate Task SyncEventCallbackAsync(SyncProgressSummary summary);

/// <summary>
/// callback delegate for SignalR messaging
/// </summary>
public delegate void SyncUpdateCallback(string message, int count, int total);

/// <summary>
/// async callback delegate for SignalR messaging
/// </summary>
public delegate Task SyncUpdateCallbackAsync(string message, int count, int total);

/// <summary>
/// callback delegate to set the start and end range for the update counters.
/// </summary>
public delegate void SyncSetUpdateRange(int start, int end);

/// <summary>
/// async callback delegate to set the start and end range for the update counters.
/// </summary>
public delegate Task SyncSetUpdateRangeAsync(int start, int end);

/// <summary>
/// callback to send a update message and increment the counter by one so moving the progress bar.
/// </summary>
/// <param name="message"></param>
public delegate void SyncIncrementalUpdateCallback(string message);

/// <summary>
/// async callback to send a update message and increment the counter by one so moving the progress bar.
/// </summary>
/// <param name="message"></param>
public delegate Task SyncIncrementalUpdateCallbackAsync(string message);

/// <summary>
/// callback to signal that the sync is complete
/// </summary>
public delegate void SyncCompleteCallBack(Guid id, string message, bool success, IEnumerable<uSyncActionView> actions);

/// <summary>
/// async callback to signal that the sync is complete
/// </summary>
public delegate Task SyncCompleteCallBackAsync(Guid id, string message, bool success, IEnumerable<uSyncActionView> actions);


/// <summary>
/// Callback objects used to communicate via SignalR
Expand All @@ -42,29 +69,56 @@ public class uSyncCallbacks
/// </summary>
public SyncEventCallback? Callback { get; private set; }

/// <summary>
/// Async add event callback. Set alongside (or instead of) <see cref="Callback"/> when the
/// consumer needs to await the send (e.g. a SignalR hub push) rather than block on it.
/// </summary>
public SyncEventCallbackAsync? CallbackAsync { get; set; }

/// <summary>
/// Update event callback
/// </summary>
public SyncUpdateCallback? Update { get; private set; }

/// <summary>
/// Async update event callback. Set alongside (or instead of) <see cref="Update"/> when the
/// consumer needs to await the send rather than block on it.
/// </summary>
public SyncUpdateCallbackAsync? UpdateAsync { get; set; }

/// <summary>
/// set a start and end range for the counter.
/// </summary>
public SyncSetUpdateRange? SetRange { get; private set; }

/// <summary>
/// Async version of <see cref="SetRange"/>.
/// </summary>
public SyncSetUpdateRangeAsync? SetRangeAsync { get; set; }

/// <summary>
/// update and increment callback.
/// </summary>
public SyncIncrementalUpdateCallback? IncrementalUpdate { get; private set; }

/// <summary>
/// Async version of <see cref="IncrementalUpdate"/>.
/// </summary>
public SyncIncrementalUpdateCallbackAsync? IncrementalUpdateAsync { get; set; }

/// <summary>
/// callback to signal that the sync is complete
/// </summary>
public SyncCompleteCallBack? Complete { get; private set; }

/// <summary>
/// Async version of <see cref="Complete"/>.
/// </summary>
public SyncCompleteCallBackAsync? CompleteAsync { get; set; }


/// <summary>
/// generate a new callback object
/// generate a new callback object
/// </summary>
public uSyncCallbacks(SyncEventCallback? callback, SyncUpdateCallback? update)
{
Expand All @@ -75,9 +129,9 @@ public uSyncCallbacks(SyncEventCallback? callback, SyncUpdateCallback? update)
/// <summary>
/// generate a callback object with range and incremental update
/// </summary>
public uSyncCallbacks(SyncEventCallback? callback,
public uSyncCallbacks(SyncEventCallback? callback,
SyncUpdateCallback? update,
SyncSetUpdateRange? updateRange,
SyncSetUpdateRange? updateRange,
SyncIncrementalUpdateCallback incrementalUpdate,
SyncCompleteCallBack? complete)
: this(callback, update)
Expand All @@ -86,4 +140,59 @@ public uSyncCallbacks(SyncEventCallback? callback,
this.IncrementalUpdate = incrementalUpdate;
this.Complete = complete;
}

/// <summary>
/// raise the <see cref="Callback"/> / <see cref="CallbackAsync"/> event, awaiting the async
/// version (if set) so callers no longer need to block on it.
/// </summary>
public async Task RaiseCallbackAsync(SyncProgressSummary summary)
{
Callback?.Invoke(summary);
if (CallbackAsync is not null)
await CallbackAsync(summary).ConfigureAwait(false);
}

/// <summary>
/// raise the <see cref="Update"/> / <see cref="UpdateAsync"/> event, awaiting the async
/// version (if set) so callers no longer need to block on it.
/// </summary>
public async Task RaiseUpdateAsync(string message, int count, int total)
{
Update?.Invoke(message, count, total);
if (UpdateAsync is not null)
await UpdateAsync(message, count, total).ConfigureAwait(false);
}

/// <summary>
/// raise the <see cref="SetRange"/> / <see cref="SetRangeAsync"/> event, awaiting the async
/// version (if set) so callers no longer need to block on it.
/// </summary>
public async Task RaiseSetRangeAsync(int start, int end)
{
SetRange?.Invoke(start, end);
if (SetRangeAsync is not null)
await SetRangeAsync(start, end).ConfigureAwait(false);
}

/// <summary>
/// raise the <see cref="IncrementalUpdate"/> / <see cref="IncrementalUpdateAsync"/> event, awaiting
/// the async version (if set) so callers no longer need to block on it.
/// </summary>
public async Task RaiseIncrementalUpdateAsync(string message)
{
IncrementalUpdate?.Invoke(message);
if (IncrementalUpdateAsync is not null)
await IncrementalUpdateAsync(message).ConfigureAwait(false);
}

/// <summary>
/// raise the <see cref="Complete"/> / <see cref="CompleteAsync"/> event, awaiting the async
/// version (if set) so callers no longer need to block on it.
/// </summary>
public async Task RaiseCompleteAsync(Guid id, string message, bool success, IEnumerable<uSyncActionView> actions)
{
Complete?.Invoke(id, message, success, actions);
if (CompleteAsync is not null)
await CompleteAsync(id, message, success, actions).ConfigureAwait(false);
}
}
6 changes: 4 additions & 2 deletions uSync.BackOffice/Services/SyncActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public async Task<SyncActionResult> ImportPostAsync(SyncFinalActionRequest reque
request.ActionOptions.GetSetOrDefault(_uSyncConfig.Settings.DefaultSet),
request.Actions);

request.Callbacks?.Update?.Invoke("Post Import Complete", 1, 1);
if (request.Callbacks is not null)
await request.Callbacks.RaiseUpdateAsync("Post Import Complete", 1, 1);
return new SyncActionResult([.. actions.Where(x => x.Change > Core.ChangeType.NoChange)]);
}

Expand Down Expand Up @@ -218,7 +219,8 @@ public async Task<SyncActionResult> FinishProcessAsync(SyncFinalActionRequest re
request.HandlerAction, request.Actions.CountChanges(), request.Actions.Count(), elapsed);
}

request.Callbacks?.Update?.Invoke($"{request.HandlerAction} completed ({elapsed:#,#}ms)", 1, 1);
if (request.Callbacks is not null)
await request.Callbacks.RaiseUpdateAsync($"{request.HandlerAction} completed ({elapsed:#,#}ms)", 1, 1);

// for speed we return an empty list. the merge will just take
// what we where passed in, and we avoid a whole copy and compare step
Expand Down
22 changes: 15 additions & 7 deletions uSync.BackOffice/Services/SyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ public async Task<IEnumerable<uSyncAction>> ImportAsync(string[] folders, bool f
summary.UpdateHandler(
handler.Name, HandlerStatus.Processing, $"Importing {handler.Name}", 0);

callbacks?.Callback?.Invoke(summary);
if (callbacks is not null)
await callbacks.RaiseCallbackAsync(summary);

var handlerActions = await this.ImportHandlerAsync(handler.Alias, importOptions);

Expand Down Expand Up @@ -228,14 +229,18 @@ public async Task<IEnumerable<uSyncAction>> ImportAsync(string[] folders, bool f
if (actions.ContainsErrors())
_logger.LogWarning("uSync Import: Errors detected in import : {count}", actions.CountErrors());

callbacks?.Update?.Invoke($"Processed {actions.Count} items in {sw.ElapsedMilliseconds}ms", 1, 1);
if (callbacks is not null)
await callbacks.RaiseUpdateAsync($"Processed {actions.Count} items in {sw.ElapsedMilliseconds}ms", 1, 1);

summary.Message = "Completed";
summary.Total = handlers.Count() + 1;
var finalActions = actions.Select(x => x.AsActionView()).ToList();

callbacks?.Callback?.Invoke(summary);
callbacks?.Complete?.Invoke(requestId, "Sync complete", true, finalActions);
if (callbacks is not null)
{
await callbacks.RaiseCallbackAsync(summary);
await callbacks.RaiseCompleteAsync(requestId, "Sync complete", true, finalActions);
}

return actions;
}
Expand Down Expand Up @@ -392,7 +397,8 @@ public async Task<IEnumerable<uSyncAction>> ExportAsync(string folder, IEnumerab
summary.UpdateHandler(
handler.Name, HandlerStatus.Processing, $"Exporting {handler.Name}", 0);

callbacks?.Callback?.Invoke(summary);
if (callbacks is not null)
await callbacks.RaiseCallbackAsync(summary);

var handlerActions = await handler.ExportAllAsync([$"{folder}/{handler.DefaultFolder}"], configuredHandler.Settings, callbacks?.Update);

Expand All @@ -405,7 +411,8 @@ public async Task<IEnumerable<uSyncAction>> ExportAsync(string folder, IEnumerab


summary.UpdateMessage("Export Completed");
callbacks?.Callback?.Invoke(summary);
if (callbacks is not null)
await callbacks.RaiseCallbackAsync(summary);

await _mutexService.FireBulkCompleteAsync(new uSyncExportCompletedNotification(actions, null));

Expand All @@ -419,7 +426,8 @@ public async Task<IEnumerable<uSyncAction>> ExportAsync(string folder, IEnumerab
sw.ElapsedMilliseconds);
}

callbacks?.Update?.Invoke($"Processed {actions.Count} items in {sw.ElapsedMilliseconds}ms", 1, 1);
if (callbacks is not null)
await callbacks.RaiseUpdateAsync($"Processed {actions.Count} items in {sw.ElapsedMilliseconds}ms", 1, 1);

return actions;
}
Expand Down
21 changes: 13 additions & 8 deletions uSync.BackOffice/Services/SyncService_Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public async Task<IEnumerable<uSyncAction>> ReportPartialAsync(IList<OrderedNode
}


options.Callbacks?.Update?.Invoke(item.Node.GetAlias(),
CalculateProgress(index, total, options.ProgressMin, options.ProgressMax), 100);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync(item.Node.GetAlias(),
CalculateProgress(index, total, options.ProgressMin, options.ProgressMax), 100);

if (handlerPair != null)
{
Expand Down Expand Up @@ -131,8 +132,9 @@ public async Task<IEnumerable<uSyncAction>> ImportPartialAsync(IList<OrderedNode
continue;
}

options.Callbacks?.Update?.Invoke(node.GetAlias(),
CalculateProgress(index, total, options.ProgressMin, options.ProgressMax), 100);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync(node.GetAlias(),
CalculateProgress(index, total, options.ProgressMin, options.ProgressMax), 100);

if (handlerPair != null)
{
Expand Down Expand Up @@ -208,8 +210,9 @@ public async Task<IEnumerable<uSyncAction>> ImportPartialSecondPassAsync(IEnumer
continue;
}

options.Callbacks?.Update?.Invoke($"Second Pass: {action.Name}",
CalculateProgress(index, total, options.ProgressMin, options.ProgressMax), 100);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync($"Second Pass: {action.Name}",
CalculateProgress(index, total, options.ProgressMin, options.ProgressMax), 100);

secondPassActions.AddRange(await handlerPair.Handler.ImportSecondPassAsync(action, handlerPair.Settings, options));

Expand Down Expand Up @@ -273,7 +276,8 @@ public async Task<IEnumerable<uSyncAction>> ImportPartialPostImportAsync(IEnumer
{
if (handlerPair.Handler is ISyncPostImportHandler postImportHandler)
{
options.Callbacks?.Update?.Invoke(actionItem.alias, index, folders.Count);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync(actionItem.alias, index, folders.Count);

var handlerActions = actions.Where(x => x.HandlerAlias.InvariantEquals(handlerPair.Handler.Alias));
results.AddRange(await postImportHandler.ProcessPostImportAsync(handlerActions, handlerPair.Settings));
Expand Down Expand Up @@ -326,7 +330,8 @@ public async Task<IEnumerable<uSyncAction>> ImportPostCleanFilesAsync(IEnumerabl

if (handlerPair.Handler is ISyncCleanEntryHandler cleanEntryHandler)
{
options.Callbacks?.Update?.Invoke(actionItem.alias, index, cleans.Count);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync(actionItem.alias, index, cleans.Count);

var handlerActions = actions.Where(x => x.HandlerAlias.InvariantEquals(handlerPair.Handler.Alias));
results.AddRange(await cleanEntryHandler.ProcessCleanActionsAsync(actionItem.folder, handlerActions, handlerPair.Settings));
Expand Down
18 changes: 12 additions & 6 deletions uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,13 @@ public async Task<IEnumerable<uSyncAction>> ImportAllAsync(string[] folders, Han
var cacheKey = PrepCaches();
try
{
options.Callbacks?.Update?.Invoke("Calculating import order", 1, 9);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync("Calculating import order", 1, 9);

var items = await GetMergedItemsAsync(folders, new SyncMergeOptions(options.Callbacks?.Update));

options.Callbacks?.Update?.Invoke($"Processing {items.Count} items", 2, 9);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync($"Processing {items.Count} items", 2, 9);

// create the update list with items.count space. this is the max size we need this list.
List<uSyncAction> actions = new(items.Count);
Expand All @@ -280,7 +282,8 @@ public async Task<IEnumerable<uSyncAction>> ImportAllAsync(string[] folders, Han
int count = 0;
int total = items.Count;

options.Callbacks?.SetRange?.Invoke(count, total);
if (options.Callbacks is not null)
await options.Callbacks.RaiseSetRangeAsync(count, total);

foreach (var item in items)
{
Expand Down Expand Up @@ -326,7 +329,8 @@ public async Task<IEnumerable<uSyncAction>> ImportAllAsync(string[] folders, Han
await PerformImportCleanAsync(cleanMarkers, actions, config, options.Callbacks?.Update);
}

options.Callbacks?.Update?.Invoke("Done", 3, 3);
if (options.Callbacks is not null)
await options.Callbacks.RaiseUpdateAsync("Done", 3, 3);

if (logger.IsEnabled(LogLevel.Debug))
logger.LogDebug("ImportAll: {count} items imported", actions.Count);
Expand Down Expand Up @@ -458,7 +462,8 @@ virtual public async Task<IEnumerable<uSyncAction>> ImportElementAsync(XElement
{
var actions = new List<uSyncAction>();
var elements = node.Elements().ToList();
options.Callbacks?.SetRange?.Invoke(0, elements.Count);
if (options.Callbacks is not null)
await options.Callbacks.RaiseSetRangeAsync(0, elements.Count);
foreach (var item in elements)
{
actions.AddRange(await ImportSingleElementAsync(new XElement(item), filename, settings, options));
Expand Down Expand Up @@ -492,7 +497,8 @@ virtual protected async Task<IEnumerable<uSyncAction>> ImportSingleElementAsync(

try
{
options.Callbacks?.IncrementalUpdate?.Invoke(node.GetAlias());
if (options.Callbacks is not null)
await options.Callbacks.RaiseIncrementalUpdateAsync(node.GetAlias());

// merge the options from the handler and any import options into our serializer options.
var serializerOptions = new SyncSerializerOptions(options.Flags, settings.Settings, options.UserId);
Expand Down
Loading
Loading