Skip to content

Commit e30dc50

Browse files
authored
V16/feature/move last synced id to db (#19884)
* Foundation work for moving last synced id * register manager and repo in dependency injection * Fixing to make tests work * Replacing the use of the old LastSyncedFileManager.cs with the new LastSyncedManager.cs * Testing to delete out of sync id and old entries * changing some stuff to please the reviewer. * Inverted saving methods id check and fixed documentation mishaps
1 parent 229d9ca commit e30dc50

File tree

14 files changed

+519
-33
lines changed

14 files changed

+519
-33
lines changed

src/Umbraco.Core/DependencyInjection/UmbracoBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Umbraco.Cms.Core.DynamicRoot;
1818
using Umbraco.Cms.Core.Editors;
1919
using Umbraco.Cms.Core.Events;
20+
using Umbraco.Cms.Core.Factories;
2021
using Umbraco.Cms.Core.Features;
2122
using Umbraco.Cms.Core.Handlers;
2223
using Umbraco.Cms.Core.Hosting;
@@ -344,6 +345,8 @@ private void AddCoreServices()
344345
factory.GetRequiredService<ILogger<LocalizedTextService>>()));
345346
Services.AddUnique<IRepositoryCacheVersionService, RepositoryCacheVersionService>();
346347
Services.AddUnique<ILongRunningOperationService, LongRunningOperationService>();
348+
Services.AddUnique<ILastSyncedManager, LastSyncedManager>();
349+
Services.AddUnique<IMachineInfoFactory, MachineInfoFactory>();
347350

348351
Services.AddUnique<IEntityXmlSerializer, EntityXmlSerializer>();
349352

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Umbraco.Cms.Core.Factories;
2+
3+
/// <summary>
4+
/// Fetches information of the host machine.
5+
/// </summary>
6+
public interface IMachineInfoFactory
7+
{
8+
/// <summary>
9+
/// Fetches the name of the Host Machine for identification.
10+
/// </summary>
11+
/// <returns>A name of the host machine.</returns>
12+
public string GetMachineIdentifier();
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Umbraco.Cms.Core.Factories;
2+
3+
internal sealed class MachineInfoFactory : IMachineInfoFactory
4+
{
5+
6+
/// <inheritdoc />
7+
public string GetMachineIdentifier() => Environment.MachineName;
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Umbraco.Cms.Core.Persistence.Repositories;
2+
3+
/// <summary>
4+
/// Handles saving and pruning of the LastSynced database table.
5+
/// </summary>
6+
public interface ILastSyncedRepository
7+
{
8+
/// <summary>
9+
/// Fetches the last synced internal ID from the database.
10+
/// </summary>
11+
/// <returns>The Internal ID from the database.</returns>
12+
Task<int?> GetInternalIdAsync();
13+
14+
/// <summary>
15+
/// Fetches the last synced external ID from the database.
16+
/// </summary>
17+
/// <returns>The External ID from the database.</returns>
18+
Task<int?> GetExternalIdAsync();
19+
20+
/// <summary>
21+
/// Saves the last synced Internal ID to the Database.
22+
/// </summary>
23+
/// <param name="id">The last synced internal ID.</param>
24+
Task SaveInternalIdAsync(int id);
25+
26+
/// <summary>
27+
/// Saves the last synced External ID to the Database.
28+
/// </summary>
29+
/// <param name="id">The last synced external ID.</param>
30+
Task SaveExternalIdAsync(int id);
31+
32+
/// <summary>
33+
/// Deletes entries older than the set parameter. This method also removes any entries where both
34+
/// IDs are higher than the lowest synced CacheInstruction ID.
35+
/// </summary>
36+
/// <param name="pruneDate">Any date entries in the DB before this parameter, will be removed from the Database.</param>
37+
Task DeleteEntriesOlderThanAsync(DateTime pruneDate);
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace Umbraco.Cms.Core.Sync;
2+
3+
/// <summary>
4+
/// Handles saving and pruning of the LastSynced database table.
5+
/// </summary>
6+
public interface ILastSyncedManager
7+
{
8+
/// <summary>
9+
/// Fetches the last synced internal ID from the database.
10+
/// </summary>
11+
/// <returns>The Internal ID from the database.</returns>
12+
Task<int?> GetLastSyncedInternalAsync();
13+
14+
/// <summary>
15+
/// Fetches the last synced external ID from the database.
16+
/// </summary>
17+
/// <returns>The External ID from the database.</returns>
18+
Task<int?> GetLastSyncedExternalAsync();
19+
20+
/// <summary>
21+
/// Saves the last synced Internal ID to the Database.
22+
/// </summary>
23+
/// <param name="id">The last synced internal ID.</param>
24+
Task SaveLastSyncedInternalAsync(int id);
25+
26+
/// <summary>
27+
/// Saves the last synced External ID to the Database.
28+
/// </summary>
29+
/// <param name="id">The last synced external ID.</param>
30+
Task SaveLastSyncedExternalAsync(int id);
31+
32+
/// <summary>
33+
/// Deletes entries older than the set parameter. This method also removes any entries where both
34+
/// IDs are higher than the lowest synced CacheInstruction ID.
35+
/// </summary>
36+
/// <param name="pruneDate">Any date entries in the DB before this parameter, will be removed from the Database.</param>
37+
Task DeleteOlderThanAsync(DateTime date);
38+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Umbraco.Cms.Core.Persistence.Repositories;
2+
using Umbraco.Cms.Core.Scoping;
3+
4+
namespace Umbraco.Cms.Core.Sync;
5+
6+
/// <inheritdoc/>
7+
internal sealed class LastSyncedManager : ILastSyncedManager
8+
{
9+
private readonly ILastSyncedRepository _lastSyncedRepository;
10+
private readonly ICoreScopeProvider _coreScopeProvider;
11+
12+
public LastSyncedManager(ILastSyncedRepository lastSyncedRepository, ICoreScopeProvider coreScopeProvider)
13+
{
14+
_lastSyncedRepository = lastSyncedRepository;
15+
_coreScopeProvider = coreScopeProvider;
16+
}
17+
18+
/// <inheritdoc/>
19+
public async Task<int?> GetLastSyncedInternalAsync()
20+
{
21+
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
22+
int? internalId = await _lastSyncedRepository.GetInternalIdAsync();
23+
scope.Complete();
24+
25+
return internalId;
26+
}
27+
28+
/// <inheritdoc/>
29+
public async Task<int?> GetLastSyncedExternalAsync()
30+
{
31+
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
32+
int? externalId = await _lastSyncedRepository.GetExternalIdAsync();
33+
scope.Complete();
34+
35+
return externalId;
36+
}
37+
38+
/// <inheritdoc/>
39+
public async Task SaveLastSyncedInternalAsync(int id)
40+
{
41+
if (id < 0)
42+
{
43+
throw new ArgumentException("Invalid last synced id. Must be non-negative.");
44+
}
45+
46+
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
47+
await _lastSyncedRepository.SaveInternalIdAsync(id);
48+
scope.Complete();
49+
}
50+
51+
/// <inheritdoc/>
52+
public async Task SaveLastSyncedExternalAsync(int id)
53+
{
54+
if (id < 0)
55+
{
56+
throw new ArgumentException("Invalid last synced id. Must be non-negative.");
57+
}
58+
59+
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
60+
await _lastSyncedRepository.SaveExternalIdAsync(id);
61+
scope.Complete();
62+
}
63+
64+
/// <inheritdoc/>
65+
public async Task DeleteOlderThanAsync(DateTime date)
66+
{
67+
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
68+
await _lastSyncedRepository.DeleteEntriesOlderThanAsync(date);
69+
scope.Complete();
70+
}
71+
}

src/Umbraco.Infrastructure/BackgroundJobs/Jobs/CacheInstructionsPruningJob.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Umbraco.Cms.Core.Configuration.Models;
33
using Umbraco.Cms.Core.Persistence.Repositories;
44
using Umbraco.Cms.Core.Scoping;
5-
using Umbraco.Cms.Infrastructure.Scoping;
5+
using Umbraco.Cms.Core.Sync;
66

77
namespace Umbraco.Cms.Infrastructure.BackgroundJobs.Jobs;
88

@@ -15,6 +15,7 @@ public class CacheInstructionsPruningJob : IRecurringBackgroundJob
1515
private readonly ICacheInstructionRepository _cacheInstructionRepository;
1616
private readonly ICoreScopeProvider _scopeProvider;
1717
private readonly TimeProvider _timeProvider;
18+
private readonly ILastSyncedManager _lastSyncedManager;
1819

1920
/// <summary>
2021
/// Initializes a new instance of the <see cref="CacheInstructionsPruningJob"/> class.
@@ -27,13 +28,15 @@ public CacheInstructionsPruningJob(
2728
IOptions<GlobalSettings> globalSettings,
2829
ICacheInstructionRepository cacheInstructionRepository,
2930
ICoreScopeProvider scopeProvider,
30-
TimeProvider timeProvider)
31+
TimeProvider timeProvider,
32+
ILastSyncedManager lastSyncedManager)
3133
{
3234
_globalSettings = globalSettings;
3335
_cacheInstructionRepository = cacheInstructionRepository;
3436
_scopeProvider = scopeProvider;
3537
_timeProvider = timeProvider;
3638
Period = globalSettings.Value.DatabaseServerMessenger.TimeBetweenPruneOperations;
39+
_lastSyncedManager = lastSyncedManager;
3740
}
3841

3942
/// <inheritdoc />
@@ -53,6 +56,7 @@ public Task RunJobAsync()
5356
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
5457
{
5558
_cacheInstructionRepository.DeleteInstructionsOlderThan(pruneDate.DateTime);
59+
_lastSyncedManager.DeleteOlderThanAsync(pruneDate.DateTime).GetAwaiter().GetResult();
5660
scope.Complete();
5761
}
5862

src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Repositories.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ internal static IUmbracoBuilder AddRepositories(this IUmbracoBuilder builder)
8484
builder.Services.AddUnique<IPublishStatusRepository, PublishStatusRepository>();
8585
builder.Services.AddUnique<IRepositoryCacheVersionRepository, RepositoryCacheVersionRepository>();
8686
builder.Services.AddUnique<ILongRunningOperationRepository, LongRunningOperationRepository>();
87+
builder.Services.AddUnique<ILastSyncedRepository, LastSyncedRepository>();
8788

8889
return builder;
8990
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
2+
3+
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_16_2_0;
4+
5+
public class AddLastSyncedTable : AsyncMigrationBase
6+
{
7+
public AddLastSyncedTable(IMigrationContext context)
8+
: base(context)
9+
{
10+
}
11+
12+
protected override Task MigrateAsync()
13+
{
14+
if (TableExists(LastSyncedDto.TableName) is false)
15+
{
16+
Create.Table<LastSyncedDto>().Do();
17+
}
18+
19+
return Task.CompletedTask;
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using NPoco;
2+
using Umbraco.Cms.Infrastructure.Persistence.DatabaseAnnotations;
3+
using Constants = Umbraco.Cms.Core.Constants;
4+
5+
namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
6+
7+
8+
[TableName(TableName)]
9+
[PrimaryKey("machineId", AutoIncrement = false)]
10+
[ExplicitColumns]
11+
public class LastSyncedDto
12+
{
13+
internal const string TableName = Constants.DatabaseSchema.Tables.LastSynced;
14+
15+
[Column("machineId")]
16+
[PrimaryKeyColumn(Name = "PK_lastSyncedMachineId", AutoIncrement = false, Clustered = true)]
17+
public required string MachineId { get; set; }
18+
19+
[Column("lastSyncedInternalId")]
20+
[NullSetting(NullSetting = NullSettings.Null)]
21+
public int? LastSyncedInternalId { get; set; }
22+
23+
[Column("lastSyncedExternalId")]
24+
[NullSetting(NullSetting = NullSettings.Null)]
25+
public int? LastSyncedExternalId { get; set; }
26+
27+
[Column("lastSyncedDate")]
28+
public DateTime LastSyncedDate { get; set; }
29+
}

0 commit comments

Comments
 (0)