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
28 changes: 21 additions & 7 deletions uSync.BackOffice/Boot/FirstBootMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Threading.Tasks;

using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Migrations;

Expand All @@ -30,9 +31,10 @@ public FirstBootMigrationPlan()
/// <summary>
/// First boot Feature migration
/// </summary>
public class FirstBootMigration : AsyncMigrationBase
public class FirstBootMigration : UnscopedAsyncMigrationBase
{
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly IServerRoleAccessor _serverRoleAccessor;
private readonly ISyncConfigService _uSyncConfig;
private readonly ISyncService _uSyncService;
private readonly ILogger<FirstBootMigration> _logger;
Expand All @@ -43,26 +45,32 @@ public FirstBootMigration(
IUmbracoContextFactory umbracoContextFactory,
ISyncConfigService uSyncConfig,
ISyncService uSyncService,
ILogger<FirstBootMigration> logger) : base(context)
ILogger<FirstBootMigration> logger,
IServerRoleAccessor serverRoleAccessor) : base(context)
{
_umbracoContextFactory = umbracoContextFactory;
_uSyncConfig = uSyncConfig;
_uSyncService = uSyncService;
_logger = logger;
_serverRoleAccessor = serverRoleAccessor;
}

/// <inheritdoc/>
protected override async Task MigrateAsync()
{
// TODO: doesn't work in the betas. might need a new migration to add it.
// return;

// first boot migration.
try
{
if (!_uSyncConfig.Settings.ImportOnFirstBoot)
return;

if (_serverRoleAccessor.CurrentServerRole == ServerRole.Subscriber)
{
_logger.LogInformation("This is a Subscriber server in a load balanced setup - uSync only runs on single or schedulingPublisher (main) servers");
return;
}

var sw = Stopwatch.StartNew();
var changes = 0;

Expand All @@ -72,8 +80,9 @@ protected override async Task MigrateAsync()
// if config service is set to import on first boot then this
// will let uSync do a first boot import

// not sure about context on migrations so will need to test
// or maybe we fire something into a notification (or use a static)
// this runs as a 'un-scoped' migration, so we need to manage the context here.
// as we are in the context and not just a scope all the publish stuff works
// first time, just like it does in ImportOnStartup

using (var reference = _umbracoContextFactory.EnsureUmbracoContext())
{
Expand All @@ -92,7 +101,12 @@ protected override async Task MigrateAsync()
catch (Exception ex)
{
_logger.LogError(ex, "uSync First boot failed {message}", ex.Message);
throw;
}
finally
{
// we always complete the context - even if we fail.
// we don't want to keep trying this migration every time.
Context.Complete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ private async Task InituSyncAsync()
};

_logger.LogInformation("uSync: Running export at startup");


_uSyncService.StartupExportAsync(_uSyncConfig.GetWorkingFolder(), options).Wait();
}

Expand All @@ -131,10 +131,10 @@ private async Task InituSyncAsync()

if (!HasStopFile(_uSyncConfig.GetWorkingFolder()))
{
_uSyncService.StartupImportAsync(_uSyncConfig.GetFolders(), false, new SyncHandlerOptions
await _uSyncService.StartupImportAsync(_uSyncConfig.GetFolders(), false, new SyncHandlerOptions
{
Group = _uSyncConfig.Settings.ImportAtStartup
}).Wait();
});

await ProcessOnceFileAsync(_uSyncConfig.GetWorkingFolder());
}
Expand Down
22 changes: 22 additions & 0 deletions uSync.BackOffice/Services/SyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using uSync.BackOffice.SyncHandlers;
using uSync.BackOffice.SyncHandlers.Models;
using uSync.Core;
using uSync.Core.Extensions;
using uSync.Core.Serialization;

namespace uSync.BackOffice;
Expand Down Expand Up @@ -113,9 +114,28 @@ public bool HasRootFiles(string[] folders)
#region Importing
static readonly SemaphoreSlim _importSemaphoreLock = new SemaphoreSlim(1, 1);

/// <summary>
/// hash of the options last used in a startup import
/// </summary>
/// <remarks>
/// as both first boot and import at startup use this method,
/// and both can be triggered at startup, we use a hash of the
/// options and folders to make sure we are not running them
/// both if they are asking the same thing.
/// </remarks>
static int? _lastStartupRun;

/// <inheritdoc/>>
public async Task<IEnumerable<uSyncAction>> StartupImportAsync(string[] folders, bool force, SyncHandlerOptions handlerOptions, uSyncCallbacks? callbacks = null)
{
var runHash = $"{string.Join(",", folders)}{force}{handlerOptions.SerializeJsonString(false)}".GetDeterministicHashCode();

if (_lastStartupRun.HasValue && _lastStartupRun.Value == runHash)
{
_logger.LogInformation("uSync: Skipping [duplicate] startup import has already ran with these parameters");
return [];
}

handlerOptions ??= new SyncHandlerOptions();
handlerOptions.Action = HandlerActions.Import;
var handlers = _handlerFactory.GetValidHandlers(handlerOptions);
Expand All @@ -127,6 +147,8 @@ public async Task<IEnumerable<uSyncAction>> StartupImportAsync(string[] folders,
if (changes.Any(x => x.Change > ChangeType.NoChange && x.ItemType == "IContent"))
_distributedCache.RefreshAllPublishedSnapshot();

_lastStartupRun = runHash;

return changes;
}

Expand Down
Loading