Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions uSync.BackOffice/Configuration/uSyncSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace uSync.BackOffice.Configuration;

Expand Down Expand Up @@ -264,6 +265,7 @@ public class uSyncSettings
/// <summary>
/// uSync's folder mode - normal, root or production
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter<SyncFolderMode>))]
public enum SyncFolderMode
{
/// <summary>
Expand All @@ -285,6 +287,7 @@ public enum SyncFolderMode
/// <summary>
/// Mode is where the processing happens - normal or background
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter<SyncProcessingMode>))]
public enum SyncProcessingMode
{
/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions uSync.BackOffice/Models/SyncActionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class SyncActionOptions
/// </summary>
public string? Set { get; set; }


/// <summary>
/// the group within the set that is being processed - e.g settings, content, media etc.
/// </summary>
public string? Group { get; set; }

/// <summary>
/// SyncActions to use as the source for all individual actions
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CancelableuSyncBulkNotification : uSyncBulkNotification, ICancelabl
/// Notification constructor
/// </summary>
public CancelableuSyncBulkNotification()
: base(Enumerable.Empty<uSyncAction>())
: base(Enumerable.Empty<uSyncAction>(), null)
{ }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

using Umbraco.Cms.Core.Notifications;

Expand All @@ -13,11 +14,23 @@ public class uSyncBulkNotification : INotification
/// generate new BulkNotificationObject
/// </summary>
/// <param name="actions"></param>
public uSyncBulkNotification(IEnumerable<uSyncAction> actions, string? group)
{
this.Group = group;
this.Actions = actions;
}

[Obsolete("Use the constructor with group and actions instead - will be removed in v19")]
public uSyncBulkNotification(IEnumerable<uSyncAction> actions)
{
this.Actions = actions;
}

/// <summary>
/// The group e.g settings, content that this action ran under.
/// </summary>
public string? Group { get; set; }

/// <summary>
/// actions that have occured during the bulk operation
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace uSync.BackOffice;

Expand All @@ -7,8 +8,12 @@ namespace uSync.BackOffice;
/// </summary>
public class uSyncExportCompletedNotification : uSyncBulkNotification
{
/// <inheritdoc/>
public uSyncExportCompletedNotification(IEnumerable<uSyncAction> actions, string? group)
: base(actions, group) { }

/// <inheritdoc/>
[Obsolete("Use the constructor with the group parameter instead will be removed in v19")]
public uSyncExportCompletedNotification(IEnumerable<uSyncAction> actions)
: base(actions) { }
: base(actions, null) { }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace uSync.BackOffice;

Expand All @@ -7,7 +8,13 @@ namespace uSync.BackOffice;
/// </summary>
public class uSyncImportCompletedNotification : uSyncBulkNotification
{

/// <inheritdoc/>
public uSyncImportCompletedNotification(IEnumerable<uSyncAction> actions, string? group)
: base(actions, group) { }

/// <inheritdoc/>
[Obsolete("Use the constructor with the group parameter instead will be removed in v19")]
public uSyncImportCompletedNotification(IEnumerable<uSyncAction> actions)
: base(actions) { }
: base(actions, null) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ namespace uSync.BackOffice;
/// </summary>
public class uSyncReportCompletedNotification : uSyncBulkNotification
{
/// <inheritdoc/>
public uSyncReportCompletedNotification(IEnumerable<uSyncAction> actions, string? group )
: base(actions, group) { }


/// <inheritdoc/>
public uSyncReportCompletedNotification(IEnumerable<uSyncAction> actions)
: base(actions) { }
: base(actions, null) { }
}
8 changes: 7 additions & 1 deletion uSync.BackOffice/Services/ISyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,20 @@ public interface ISyncService
/// </summary>
Task StartBulkProcessAsync(HandlerActions action);


/// <summary>
/// trigger the end of the bulk process
/// </summary>
[Obsolete("Use StartBulkProcessAsync(HandlerActions action, string group) instead")]
Task FinishBulkProcessAsync(HandlerActions action, IEnumerable<uSyncAction> actions);

/// <summary>
/// trigger the end of the bulk process
/// </summary>
Task FinishBulkProcessAsync(HandlerActions action, string? group, IEnumerable<uSyncAction> actions);

/// <summary>
/// merge the given folders in single 'production' files for each handler.
/// </summary>
Task<int> MergeExportFolder(string[] paths, IEnumerable<HandlerConfigPair> handlers);

}
2 changes: 1 addition & 1 deletion uSync.BackOffice/Services/SyncActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public async Task StartProcessAsync(HandlerActions action)
/// <inheritdoc/>
public async Task<SyncActionResult> FinishProcessAsync(SyncFinalActionRequest request)
{
await _uSyncService.FinishBulkProcessAsync(request.HandlerAction, request.Actions);
await _uSyncService.FinishBulkProcessAsync(request.HandlerAction, request.ActionOptions.Group, request.Actions);

_timer?.Stop();
var elapsed = _timer?.ElapsedMilliseconds ?? 0;
Expand Down
4 changes: 2 additions & 2 deletions uSync.BackOffice/Services/SyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public async Task<IEnumerable<uSyncAction>> ImportAsync(string[] folders, bool f
var results = await SyncService.PerformPostImportAsync(handlers, actions);

// fire complete
await _mutexService.FireBulkCompleteAsync(new uSyncImportCompletedNotification(actions));
await _mutexService.FireBulkCompleteAsync(new uSyncImportCompletedNotification(actions, handlerOptions.Group));

_logger.LogInformation("uSync Import: {handlerCount} handlers, processed {itemCount} items, {changeCount} changes in {ElapsedMilliseconds}ms",
handlers.Count(),
Expand Down Expand Up @@ -402,7 +402,7 @@ public async Task<IEnumerable<uSyncAction>> ExportAsync(string folder, IEnumerab
summary.UpdateMessage("Export Completed");
callbacks?.Callback?.Invoke(summary);

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

sw.Stop();

Expand Down
14 changes: 10 additions & 4 deletions uSync.BackOffice/Services/SyncService_Handlers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -154,19 +155,24 @@ public async Task StartBulkProcessAsync(HandlerActions action)
}

/// <inheritdoc/>>
[Obsolete("Use the overload with the group parameter instead")]
public async Task FinishBulkProcessAsync(HandlerActions action, IEnumerable<uSyncAction> actions)
=> await FinishBulkProcessAsync(action, uSync.EverythingGroupName, actions);

/// <inheritdoc/>>
public async Task FinishBulkProcessAsync(HandlerActions action, string? group, IEnumerable<uSyncAction> actions)
{
switch (action)
{
case HandlerActions.Export:
await WriteVersionFileAsync(_uSyncConfig.GetWorkingFolder());
await _mutexService.FireBulkCompleteAsync(new uSyncExportCompletedNotification(actions));
await _mutexService.FireBulkCompleteAsync(new uSyncExportCompletedNotification(actions, group));
break;
case HandlerActions.Import:
await _mutexService.FireBulkCompleteAsync(new uSyncImportCompletedNotification(actions));
await _mutexService.FireBulkCompleteAsync(new uSyncImportCompletedNotification(actions, group));
break;
case HandlerActions.Report:
await _mutexService.FireBulkCompleteAsync(new uSyncReportCompletedNotification(actions));
await _mutexService.FireBulkCompleteAsync(new uSyncReportCompletedNotification(actions, group));
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions uSync.BackOffice/SyncHandlers/Models/HandlerActionNames.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Text.Json.Serialization;

namespace uSync.BackOffice.SyncHandlers.Models;

/// <summary>
/// Possible actions a handler can do (stored in config)
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter<HandlerActions>))]
public enum HandlerActions
{
/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions uSync.BackOffice/Tracker/ISyncTrackerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Threading.Tasks;

namespace uSync.BackOffice.Tracker;

public interface ISyncTrackerService
{
Task<DateTime?> GetLastSync(string group);
Task SaveLastSync(string group);
}
15 changes: 15 additions & 0 deletions uSync.BackOffice/Tracker/SyncTrackerBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;

using Umbraco.Cms.Core.DependencyInjection;

namespace uSync.BackOffice.Tracker;

internal static class SyncTrackerBuilderExtensions
{
public static IUmbracoBuilder AddSyncTracker(this IUmbracoBuilder builder)
{
builder.Services.AddSingleton<ISyncTrackerService, SyncTrackerService>();
builder.AddNotificationAsyncHandler<uSyncImportCompletedNotification, SyncTrackerNotificationHandler>();
return builder;
}
}
22 changes: 22 additions & 0 deletions uSync.BackOffice/Tracker/SyncTrackerNotificationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Threading;
using System.Threading.Tasks;

using Umbraco.Cms.Core.Events;

namespace uSync.BackOffice.Tracker;

internal class SyncTrackerNotificationHandler
: INotificationAsyncHandler<uSyncImportCompletedNotification>
{
private readonly ISyncTrackerService _syncTrackerService;

public SyncTrackerNotificationHandler(ISyncTrackerService syncTrackerService)
{
_syncTrackerService = syncTrackerService;
}

public async Task HandleAsync(uSyncImportCompletedNotification notification, CancellationToken cancellationToken)
{
await _syncTrackerService.SaveLastSync(notification.Group ?? "all");
}
}
64 changes: 64 additions & 0 deletions uSync.BackOffice/Tracker/SyncTrackerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.Extensions.Logging;

using System;
using System.Threading.Tasks;

using Umbraco.Cms.Core.Services;

namespace uSync.BackOffice.Tracker;


internal class SyncTrackerService : ISyncTrackerService
{
const string _keyPrefix = "uSync.SyncTracker.";

private readonly ILogger<SyncTrackerService> _logger;
private readonly IKeyValueService _keyValueService;

public SyncTrackerService(IKeyValueService keyValueService, ILogger<SyncTrackerService> logger)
{
_keyValueService = keyValueService;
_logger = logger;
}

public Task SaveLastSync(string group)
{
try
{
var key = $"{_keyPrefix}{group}";
_keyValueService.SetValue(key, DateTime.UtcNow.ToString("o"));
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Error saving last sync time for group {Group}", group);
}
return Task.CompletedTask;

}

public async Task<DateTime?> GetLastSync(string group)
{
var lastSync = await InternalGetLastSync(group);
var lastAllSync = await InternalGetLastSync("All");
Comment thread
KevinJump marked this conversation as resolved.
Outdated
return lastSync > lastAllSync ? lastSync : lastAllSync;
}

private Task<DateTime?> InternalGetLastSync(string group)
{
try
{
var key = $"{_keyPrefix}{group}";
var value = _keyValueService.GetValue(key);
if (DateTime.TryParse(value, null, System.Globalization.DateTimeStyles.RoundtripKind, out var result))
{
return Task.FromResult<DateTime?>(result);
}
}
catch(Exception ex)
{
_logger.LogWarning(ex, "Error retrieving last sync time for group {Group}", group);
}

return Task.FromResult<DateTime?>(null);
}
}
5 changes: 5 additions & 0 deletions uSync.BackOffice/uSyncBackOffice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class uSync
/// </summary>
public const string EventPausedKey = "uSync.PausedKey";

/// <summary>
/// the name we use internally for the 'everything' group.
/// </summary>
public const string EverythingGroupName = "all";

internal class Trees
{
internal const string uSync = "usync";
Expand Down
3 changes: 3 additions & 0 deletions uSync.BackOffice/uSyncBackOfficeBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using uSync.BackOffice.SyncHandlers;
using uSync.BackOffice.SyncHandlers.Handlers;
using uSync.BackOffice.SyncHandlers.Interfaces;
using uSync.BackOffice.Tracker;
using uSync.Core;

namespace uSync.BackOffice;
Expand Down Expand Up @@ -89,6 +90,8 @@ public static IUmbracoBuilder AdduSync(this IUmbracoBuilder builder, Action<uSyn
builder.Services.AddSignalR();
builder.Services.AdduSyncSignalR();

builder.AddSyncTracker();

builder.Services.AddTransient<ISyncLegacyService, SyncLegacyService>();

builder.Services.AddSingleton<IAuthorizationHandler, uSyncAllowedApplicationHandler>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<List<SyncActionGroup>> GetActions()
[ProducesResponseType(typeof(List<SyncActionGroup>), 200)]
public async Task<List<SyncActionGroup>> GetActionsBySet(string setName)
{
return await Task.FromResult(_syncManagementService.GetActions(setName));
return await _syncManagementService.GetActionsAsync(setName);
}


Expand Down
1 change: 1 addition & 0 deletions uSync.Backoffice.Management.Api/Models/SyncActionGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class SyncActionGroup
public string Icon { get; set; } = "icon-box";
public string GroupName { get; set; } = "settings";
public List<SyncActionButton> Buttons { get; set; } = [];
public DateTime? LastSync { get; set; }

}

Expand Down
Loading