Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a couple of occurrences where scopes was auto-completed while modified db state #14947

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions src/Umbraco.Core/Services/ContentService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.DependencyInjection;

Check notice on line 1 in src/Umbraco.Core/Services/ContentService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v12/dev)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 2248 to 2252, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
Expand Down Expand Up @@ -372,7 +372,7 @@
public IContent CreateAndSave(string name, int parentId, string contentTypeAlias, int userId = Constants.Security.SuperUserId)
{
// TODO: what about culture?
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
// locking the content tree secures content types too
scope.WriteLock(Constants.Locks.ContentTree);
Expand All @@ -395,6 +395,8 @@

Save(content, userId);

scope.Complete();

return content;
}
}
Expand All @@ -416,7 +418,7 @@
throw new ArgumentNullException(nameof(parent));
}

using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
// locking the content tree secures content types too
scope.WriteLock(Constants.Locks.ContentTree);
Expand All @@ -431,6 +433,7 @@

Save(content, userId);

scope.Complete();
return content;
}
}
Expand Down Expand Up @@ -508,10 +511,11 @@
/// <inheritdoc />
public void PersistContentSchedule(IContent content, ContentScheduleCollection contentSchedule)
{
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
scope.WriteLock(Constants.Locks.ContentTree);
_documentRepository.PersistContentSchedule(content, contentSchedule);
scope.Complete();
}
}

Expand Down Expand Up @@ -2978,7 +2982,7 @@

public ContentDataIntegrityReport CheckDataIntegrity(ContentDataIntegrityReportOptions options)
{
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
scope.WriteLock(Constants.Locks.ContentTree);

Expand All @@ -2991,6 +2995,8 @@
scope.Notifications.Publish(new ContentTreeChangeNotification(root, TreeChangeTypes.RefreshAll, EventMessagesFactory.Get()));
}

scope.Complete();

return report;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ public IEnumerable<TItem> GetAll(IEnumerable<Guid>? ids)
}

using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))

{
scope.ReadLock(ReadLockIds);
return Repository.GetMany(ids.ToArray());
Expand Down
15 changes: 11 additions & 4 deletions src/Umbraco.Core/Services/ContentVersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public IReadOnlyCollection<ContentVersionMeta> PerformContentVersionCleanup(Date
/// <inheritdoc />
public void SetPreventCleanup(int versionId, bool preventCleanup, int userId = Constants.Security.SuperUserId)
{
using (ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
{
scope.WriteLock(Constants.Locks.ContentTree);
_documentVersionRepository.SetPreventCleanup(versionId, preventCleanup);
Expand All @@ -87,6 +87,7 @@ public void SetPreventCleanup(int versionId, bool preventCleanup, int userId = C
var message = $"set preventCleanup = '{preventCleanup}' for version '{versionId}'";

Audit(auditType, userId, version.ContentId, message, $"{version.VersionDate}");
scope.Complete();
}
}

Expand Down Expand Up @@ -120,7 +121,7 @@ private IReadOnlyCollection<ContentVersionMeta> CleanupDocumentVersions(DateTime
*
* tl;dr lots of scopes to enable other connections to use the DB whilst we work.
*/
using (ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
{
IReadOnlyCollection<ContentVersionMeta>? allHistoricVersions =
_documentVersionRepository.GetDocumentVersionsEligibleForCleanup();
Expand Down Expand Up @@ -154,6 +155,8 @@ private IReadOnlyCollection<ContentVersionMeta> CleanupDocumentVersions(DateTime

versionsToDelete.Add(version);
}

scope.Complete();
}

if (!versionsToDelete.Any())
Expand All @@ -169,7 +172,7 @@ private IReadOnlyCollection<ContentVersionMeta> CleanupDocumentVersions(DateTime

foreach (IEnumerable<ContentVersionMeta> group in versionsToDelete.InGroupsOf(Constants.Sql.MaxParameterCount))
{
using (ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
{
scope.WriteLock(Constants.Locks.ContentTree);
var groupEnumerated = group.ToList();
Expand All @@ -182,12 +185,16 @@ private IReadOnlyCollection<ContentVersionMeta> CleanupDocumentVersions(DateTime
scope.Notifications.Publish(
new ContentDeletedVersionsNotification(version.ContentId, messages, version.VersionId));
}

scope.Complete();
}
}

using (_scopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
{
Audit(AuditType.Delete, Constants.Security.SuperUserId, -1, $"Removed {versionsToDelete.Count} ContentVersion(s) according to cleanup policy");

scope.Complete();
}

return versionsToDelete;
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Services/DataTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public void Delete(IDataType dataType, int userId = Constants.Security.SuperUser

public IReadOnlyDictionary<Udi, IEnumerable<string>> GetReferences(int id)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete:true);
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
return _dataTypeRepository.FindUsages(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IEnumerable<ContentVersionMeta> Apply(DateTime asAtDate, IEnumerable<Cont

var theRest = new List<ContentVersionMeta>();

using (_scopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
{
var policyOverrides = _documentVersionRepository.GetCleanupPolicies()?
.ToDictionary(x => x.ContentTypeId);
Expand Down Expand Up @@ -77,6 +77,8 @@ public IEnumerable<ContentVersionMeta> Apply(DateTime asAtDate, IEnumerable<Cont
yield return version;
}
}

scope.Complete();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Umbraco.Core/Services/MediaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ public bool Sort(IEnumerable<IMedia> items, int userId = Constants.Security.Supe

public ContentDataIntegrityReport CheckDataIntegrity(ContentDataIntegrityReportOptions options)
{
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
{
scope.WriteLock(Constants.Locks.MediaTree);

Expand All @@ -1210,6 +1210,7 @@ public ContentDataIntegrityReport CheckDataIntegrity(ContentDataIntegrityReportO
scope.Notifications.Publish(new MediaTreeChangeNotification(root, TreeChangeTypes.RefreshAll, EventMessagesFactory.Get()));
}

scope.Complete();
return report;
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/Umbraco.Core/Services/TwoFactorLoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public TwoFactorLoginService(
/// <inheritdoc />
public async Task DeleteUserLoginsAsync(Guid userOrMemberKey)
{
using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
using ICoreScope scope = _scopeProvider.CreateCoreScope();
await _twoFactorLoginRepository.DeleteUserLoginsAsync(userOrMemberKey);

scope.Complete();
}

/// <inheritdoc />
Expand Down Expand Up @@ -138,8 +140,12 @@ public async Task<bool> IsTwoFactorEnabledAsync(Guid userOrMemberKey) =>
/// <inheritdoc />
public async Task<bool> DisableAsync(Guid userOrMemberKey, string providerName)
{
using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
return await _twoFactorLoginRepository.DeleteUserLoginsAsync(userOrMemberKey, providerName);
using ICoreScope scope = _scopeProvider.CreateCoreScope();
var result = await _twoFactorLoginRepository.DeleteUserLoginsAsync(userOrMemberKey, providerName);

scope.Complete();

return result;
}

/// <inheritdoc />
Expand All @@ -156,9 +162,10 @@ public bool ValidateTwoFactorSetup(string providerName, string secret, string co
/// <inheritdoc />
public Task SaveAsync(TwoFactorLogin twoFactorLogin)
{
using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
using ICoreScope scope = _scopeProvider.CreateCoreScope();
_twoFactorLoginRepository.Save(twoFactorLogin);

scope.Complete();
return Task.CompletedTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public override Task PerformExecuteAsync(object? state)
// but then what should be its "scope"? could we attach it to scopes?
// - and we should definitively *not* have to flush it here (should be auto)
using UmbracoContextReference contextReference = _umbracoContextFactory.EnsureUmbracoContext();
using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
using ICoreScope scope = _scopeProvider.CreateCoreScope();

/* We used to assume that there will never be two instances running concurrently where (IsMainDom && ServerRole == SchedulingPublisher)
* However this is possible during an azure deployment slot swap for the SchedulingPublisher instance when trying to achieve zero downtime deployments.
Expand All @@ -125,6 +125,8 @@ public override Task PerformExecuteAsync(object? state)
grouped.Count(),
grouped.Key);
}

scope.Complete();
}
finally
{
Expand Down
19 changes: 15 additions & 4 deletions src/Umbraco.Infrastructure/Logging/Viewer/LogViewerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.Scoping;
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;

namespace Umbraco.Cms.Core.Logging.Viewer;
Expand All @@ -10,6 +12,12 @@ public class LogViewerConfig : ILogViewerConfig
private readonly ILogViewerQueryRepository _logViewerQueryRepository;
private readonly IScopeProvider _scopeProvider;

public LogViewerConfig(ILogViewerQueryRepository logViewerQueryRepository, Umbraco.Cms.Core.Scoping.IScopeProvider scopeProvider)
: this(logViewerQueryRepository, StaticServiceProvider.Instance.GetRequiredService<IScopeProvider>())
{

}
bergmania marked this conversation as resolved.
Show resolved Hide resolved

public LogViewerConfig(ILogViewerQueryRepository logViewerQueryRepository, IScopeProvider scopeProvider)
{
_logViewerQueryRepository = logViewerQueryRepository;
Expand All @@ -26,9 +34,10 @@ public IReadOnlyList<SavedLogSearch> GetSavedSearches()

public IReadOnlyList<SavedLogSearch> AddSavedSearch(string name, string query)
{
using IScope scope = _scopeProvider.CreateScope(autoComplete: true);
using IScope scope = _scopeProvider.CreateScope();
_logViewerQueryRepository.Save(new LogViewerQuery(name, query));

scope.Complete();
return GetSavedSearches();
}

Expand All @@ -37,7 +46,7 @@ public IReadOnlyList<SavedLogSearch> AddSavedSearch(string name, string query)

public IReadOnlyList<SavedLogSearch> DeleteSavedSearch(string name)
{
using IScope scope = _scopeProvider.CreateScope(autoComplete: true);
using IScope scope = _scopeProvider.CreateScope();
ILogViewerQuery? item = _logViewerQueryRepository.GetByName(name);

if (item is not null)
Expand All @@ -46,6 +55,8 @@ public IReadOnlyList<SavedLogSearch> DeleteSavedSearch(string name)
}

// Return the updated object - so we can instantly reset the entire array from the API response
return GetSavedSearches();
IReadOnlyList<SavedLogSearch> result = GetSavedSearches();
scope.Complete();
return result;
}
}
6 changes: 4 additions & 2 deletions src/Umbraco.Infrastructure/Security/MemberUserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override Task<IdentityResult> CreateAsync(
throw new ArgumentNullException(nameof(user));
}

using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
using ICoreScope scope = _scopeProvider.CreateCoreScope();

// create member
IMember memberEntity = _memberService.CreateMember(
Expand Down Expand Up @@ -150,6 +150,7 @@ public override Task<IdentityResult> CreateAsync(
x.Value)));
}

scope.Complete();
return Task.FromResult(IdentityResult.Success);
}
catch (Exception ex)
Expand Down Expand Up @@ -179,7 +180,7 @@ public override Task<IdentityResult> UpdateAsync(
throw new InvalidOperationException("The user id must be an integer to work with the Umbraco");
}

using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
using ICoreScope scope = _scopeProvider.CreateCoreScope();

IMember? found = _memberService.GetById(asInt);
if (found != null)
Expand Down Expand Up @@ -220,6 +221,7 @@ public override Task<IdentityResult> UpdateAsync(
}
}

scope.Complete();
return Task.FromResult(IdentityResult.Success);
}
catch (Exception ex)
Expand Down
3 changes: 2 additions & 1 deletion src/Umbraco.Web.Website/Controllers/UmbProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void AddErrors(IdentityResult result)

private async Task<IdentityResult> UpdateMemberAsync(ProfileModel model, MemberIdentityUser currentMember)
{
using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
using ICoreScope scope = _scopeProvider.CreateCoreScope();

currentMember.Email = model.Email;
currentMember.Name = model.Name;
Expand Down Expand Up @@ -140,6 +140,7 @@ private async Task<IdentityResult> UpdateMemberAsync(ProfileModel model, MemberI

_memberService.Save(member);

scope.Complete();
return saveResult;
}
}
4 changes: 3 additions & 1 deletion src/Umbraco.Web.Website/Controllers/UmbRegisterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void AddErrors(IdentityResult result)
/// <returns>Result of registration operation.</returns>
private async Task<IdentityResult> RegisterMemberAsync(RegisterModel model)
{
using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
using ICoreScope scope = _scopeProvider.CreateCoreScope();

// U4-10762 Server error with "Register Member" snippet (Cannot save member with empty name)
// If name field is empty, add the email address instead.
Expand Down Expand Up @@ -160,6 +160,8 @@ private async Task<IdentityResult> RegisterMemberAsync(RegisterModel model)
}
}

scope.Complete();

return identityResult;
}
}
Loading
Loading