public Task <bool> UpdateAsync(IActiveLock activeLock, CancellationToken cancellationToken)
            {
                var isUpdating = _locks.ContainsKey(activeLock.StateToken);

                _locks[activeLock.StateToken] = ToActiveLock(activeLock);
                return(Task.FromResult(isUpdating));
            }
        /// <summary>
        /// Adds a lock to be tracked by this cleanup task.
        /// </summary>
        /// <param name="lockManager">The lock manager that created this active lock.</param>
        /// <param name="activeLock">The active lock to track</param>
        public void Add(ILockManager lockManager, IActiveLock activeLock)
        {
            if (_logger.IsEnabled(LogLevel.Trace))
            {
                _logger.LogTrace($"Adding lock {activeLock}");
            }

            // Don't track locks with infinite timeout
            if (activeLock.Timeout == TimeoutHeader.Infinite)
            {
                return;
            }

            lock (_syncRoot)
            {
                var newLockItem = new ActiveLockItem(lockManager, activeLock);
                _activeLocks.Add(activeLock.Expiration, newLockItem);
                if (_mostRecentExpirationLockItem != null &&
                    newLockItem.Expiration >= _mostRecentExpirationLockItem.Expiration)
                {
                    // New item is not the most recent to expire
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug($"New lock {activeLock.StateToken} item is not the most recent item");
                    }
                    return;
                }

                _mostRecentExpirationLockItem = newLockItem;
                ConfigureTimer(newLockItem);
            }
        }
            public Task <bool> UpdateAsync(IActiveLock activeLock, CancellationToken cancellationToken)
            {
                var entry = ToEntry(activeLock);

                _connection.InsertOrReplace(entry);
                return(Task.FromResult(true));
            }
            public Task <bool> AddAsync(IActiveLock activeLock, CancellationToken cancellationToken)
            {
                var entry        = ToEntry(activeLock);
                var affectedRows = _connection.Insert(entry);

                return(Task.FromResult(affectedRows != 0));
            }
 private static ActiveLock ToActiveLock(IActiveLock activeLock)
 {
     if (activeLock is ActiveLock l)
     {
         return(l);
     }
     return(_mapper.Map <ActiveLock>(activeLock));
 }
Exemplo n.º 6
0
 /// <inheritdoc />
 public Task <bool> AddAsync(IActiveLock activeLock, CancellationToken cancellationToken)
 {
     if (_locks.ContainsKey(activeLock.StateToken))
     {
         return(Task.FromResult(false));
     }
     _locks = _locks.Add(activeLock.StateToken, activeLock);
     return(Task.FromResult(true));
 }
Exemplo n.º 7
0
            /// <inheritdoc />
            public Task <bool> UpdateAsync(IActiveLock activeLock, CancellationToken cancellationToken)
            {
                var hadKey = _locks.ContainsKey(activeLock.StateToken);

                if (hadKey)
                {
                    _locks = _locks.Remove(activeLock.StateToken);
                }
                _locks = _locks.Add(activeLock.StateToken, activeLock);
                return(Task.FromResult(hadKey));
            }
        /// <summary>
        /// Removes the active lock so that it isn't tracked any more by this cleanup task.
        /// </summary>
        /// <param name="activeLock">The active lock to remove</param>
        public void Remove(IActiveLock activeLock)
        {
            if (_logger.IsEnabled(LogLevel.Trace))
            {
                _logger.LogTrace($"Try removing lock {activeLock}");
            }

            lock (_syncRoot)
            {
                IReadOnlyCollection <ActiveLockItem> lockItems;
                if (!_activeLocks.TryGetValue(activeLock.Expiration, out lockItems))
                {
                    // Lock item not found
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug($"Lock {activeLock.StateToken} is not tracked any more.");
                    }
                    return;
                }

                var lockItem = lockItems
                               .FirstOrDefault(x => string.Equals(x.ActiveLock.StateToken, activeLock.StateToken, StringComparison.Ordinal));

                if (lockItem == null)
                {
                    // Lock item not found
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug($"Lock {activeLock.StateToken} is not tracked any more.");
                    }
                    return;
                }

                // Remove lock item
                _activeLocks.Remove(lockItem.Expiration, lockItem);

                if (_mostRecentExpirationLockItem != null && lockItem.ActiveLock.StateToken == _mostRecentExpirationLockItem.ActiveLock.StateToken)
                {
                    // Removed lock item was the most recent
                    _mostRecentExpirationLockItem = FindMostRecentExpirationItem();
                    if (_mostRecentExpirationLockItem != null)
                    {
                        // Found a new one and reconfigure timer
                        ConfigureTimer(_mostRecentExpirationLockItem);
                    }
                    else if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug("No more locks to cleanup.");
                    }
                }
            }
        }
Exemplo n.º 9
0
 private static ActiveLock Refresh(IActiveLock activeLock, DateTime lastRefresh, TimeSpan timeout)
 {
     return(new ActiveLock(
                activeLock.Path,
                activeLock.Href,
                activeLock.Recursive,
                activeLock.GetOwner(),
                LockAccessType.Parse(activeLock.AccessType),
                LockShareMode.Parse(activeLock.ShareMode),
                timeout,
                activeLock.Issued,
                lastRefresh,
                activeLock.StateToken));
 }
Exemplo n.º 10
0
 private ActiveLockEntry ToEntry(IActiveLock activeLock)
 {
     return(activeLock as ActiveLockEntry
            ?? new ActiveLockEntry()
     {
         StateToken = activeLock.StateToken,
         Path = activeLock.Path,
         Recursive = activeLock.Recursive,
         Href = activeLock.Href,
         Owner = activeLock.GetOwner()?.ToString(SaveOptions.OmitDuplicateNamespaces),
         AccessType = activeLock.AccessType,
         ShareMode = activeLock.ShareMode,
         Timeout = activeLock.Timeout,
         Expiration = activeLock.Expiration,
         Issued = activeLock.Issued,
         LastRefresh = activeLock.LastRefresh,
     });
 }
Exemplo n.º 11
0
        /// <summary>
        /// Creates an <see cref="XElement"/> for a <see cref="IActiveLock"/>
        /// </summary>
        /// <param name="l">The active lock to create the <see cref="XElement"/> for</param>
        /// <param name="omitOwner">Should the owner be omitted?</param>
        /// <param name="omitToken">Should the lock state token be omitted?</param>
        /// <returns>The newly created <see cref="XElement"/> for the active lock</returns>
        public static XElement ToXElement(this IActiveLock l, bool omitOwner = false, bool omitToken = false)
        {
            var timeout   = l.Timeout == TimeoutHeader.Infinite ? "Infinite" : $"Second-{l.Timeout.TotalSeconds:F0}";
            var depth     = l.Recursive ? DepthHeader.Infinity : DepthHeader.Zero;
            var lockScope = LockShareMode.Parse(l.ShareMode);
            var lockType  = LockAccessType.Parse(l.AccessType);
            var owner     = l.GetOwner();
            var result    = new XElement(
                WebDavXml.Dav + "activelock",
                new XElement(
                    WebDavXml.Dav + "lockscope",
                    new XElement(lockScope.Name)),
                new XElement(
                    WebDavXml.Dav + "locktype",
                    new XElement(lockType.Name)),
                new XElement(
                    WebDavXml.Dav + "depth",
                    depth.Value));

            if (owner != null && !omitOwner)
            {
                result.Add(owner);
            }

            result.Add(
                new XElement(
                    WebDavXml.Dav + "timeout",
                    timeout));

            if (!omitToken)
            {
                result.Add(
                    new XElement(
                        WebDavXml.Dav + "locktoken",
                        new XElement(WebDavXml.Dav + "href", l.StateToken)));
            }

            result.Add(
                new XElement(
                    WebDavXml.Dav + "lockroot",
                    new XElement(WebDavXml.Dav + "href", l.Href)));

            return(result);
        }
 public ActiveLockItem(ILockManager lockManager, IActiveLock activeLock)
 {
     LockManager = lockManager;
     ActiveLock  = activeLock;
 }
Exemplo n.º 13
0
 /// <summary>
 /// Gets called when a lock was released
 /// </summary>
 /// <param name="activeLock">The lock that was released</param>
 protected virtual void OnLockReleased(IActiveLock activeLock)
 {
     LockReleased?.Invoke(this, new LockEventArgs(activeLock));
 }
Exemplo n.º 14
0
 public ActiveLockItem([NotNull] ILockManager lockManager, [NotNull] IActiveLock activeLock)
 {
     LockManager = lockManager;
     ActiveLock  = activeLock;
 }
Exemplo n.º 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LockResult"/> class.
 /// </summary>
 /// <param name="activeLock">The active lock when locking succeeded</param>
 public LockResult(IActiveLock activeLock)
 {
     Lock = activeLock;
 }
Exemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LockEventArgs"/> class.
 /// </summary>
 /// <param name="activeLock">The lock that got added to or remoted from the lock manager</param>
 public LockEventArgs(IActiveLock activeLock)
 {
     Lock = activeLock;
 }