Esempio n. 1
0
        private async Task <bool> releaseAsync(IDbConnectionAsync conn, LockInfo lockInfo)
        {
            var state = lockInfo.AsImmutable();

            if (!state.HasLock())
            {
                return(true); //released already
            }
            var newLockRef = Math.Max(1, state.LockReference + 1);

            using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, RELEASE_LOCK))
            {
                command.Parameters.Add(new SqlParameter("newLockRef", newLockRef));
                command.Parameters.Add(new SqlParameter("resource", state.Resource));
                command.Parameters.Add(new SqlParameter("lockReference", state.LockReference));
                command.Parameters.Add(new SqlParameter("lockedByPID", _PID));

                try
                {
                    await command.ExecuteNonQueryAsync();

                    lockInfo.Update(null, TimeSpan.Zero, 0, FrozenTimeClock.Instance);

                    return(true);
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
        }
Esempio n. 2
0
        private async Task <LockInfo> acquireLockAsync(IDbConnectionAsync conn, Guid resource, int lockReference, TimeSpan timeout)
        {
            var lockInfo = LockInfo.Empty(resource);

            var newLockRef = Math.Max(1, lockReference + 1);

            //only acquires lock if old lock reference number matches, and not locked yet.
            using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, ACQUIRE_lOCK_COMMANDS))
            {
                command.Parameters.Add(new SqlParameter("newLockRef", newLockRef));
                command.Parameters.Add(new SqlParameter("timeout", Convert.ToInt64(timeout.TotalMilliseconds)));
                command.Parameters.Add(new SqlParameter("lockedByMachine", _machineName));
                command.Parameters.Add(new SqlParameter("lockedByPID", _PID));
                command.Parameters.Add(new SqlParameter("resource", resource));
                command.Parameters.Add(new SqlParameter("lockReference", lockReference));

                try
                {
                    var dbLockedTill = (DateTime?)await command.ExecuteScalarAsync();

                    if (dbLockedTill != null)
                    {
                        var clock = _clockProvider.GetClock();

                        lockInfo.Update(dbLockedTill, timeout, newLockRef, clock);
                    }
                }
                catch (Exception ex)
                {
                    ex.LogException();
                }

                return(lockInfo);
            }
        }
Esempio n. 3
0
        private async Task <LockReferenceValue> GetLatestReferenceAsync(IDbConnectionAsync conn, Guid resource)
        {
            using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, SELECT_REFERENCE))
            {
                command.Parameters.Add(new SqlParameter("resource", resource));
                using (var reader = (await command.ExecuteReaderAsync()) as SqlDataReader)
                {
                    if (await reader.ReadAsync())
                    {
                        var lockReference = ReadInt(reader, 0);
                        var lockedTill    = ReadDateTime(reader, 1);
                        var utcNow        = ReadDateTime(reader, 2);

                        return(new LockReferenceValue()
                        {
                            LockReference = lockReference,
                            IsFree = lockReference != null && (lockedTill == null || lockedTill < utcNow)
                        });
                    }
                    else
                    {
                        return(new LockReferenceValue()
                        {
                            LockReference = null, IsFree = false
                        });
                    }
                }
            }
        }
Esempio n. 4
0
 private async Task <bool> insertEntryRecordAsync(IDbConnectionAsync conn, Guid resource)
 {
     using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, SQL_INSERT))
     {
         command.Parameters.Add(new SqlParameter("resource", resource));
         try
         {
             return(await command.ExecuteNonQueryAsync() > 0);
         }
         catch (Exception ex)
         {
             return(false);
         }
     }
 }
Esempio n. 5
0
        private async Task <bool> renewLockAsync(IDbConnectionAsync conn, TimeSpan renewTimeout, LockInfo lockInfo)
        {
            var state = lockInfo.AsImmutable();

            if (!state.HasLock())
            {
                return(false);
            }

            var newLockRef = Math.Max(1, state.LockReference + 1);

            using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, RENEW_LOCK_COMMANDS))
            {
                command.Parameters.Add(new SqlParameter("newLockRef", newLockRef));
                command.Parameters.Add(new SqlParameter("resource", state.Resource));
                command.Parameters.Add(new SqlParameter("lockReference", state.LockReference));
                command.Parameters.Add(new SqlParameter("timeout", Convert.ToInt64(renewTimeout.TotalMilliseconds)));
                command.Parameters.Add(new SqlParameter("lockedByPID", _PID));

                try
                {
                    var dbLockedTill = (DateTime?)await command.ExecuteScalarAsync();

                    if (dbLockedTill != null)
                    {
                        var clock = _clockProvider.GetClock();

                        lockInfo.Update(dbLockedTill, renewTimeout, newLockRef, clock);
                        return(lockInfo.AsImmutable().HasLock());
                    }

                    return(false);
                }
                catch (Exception ex)
                {
                    return(false);
                }
            }
        }