コード例 #1
0
        public FileBasedLock(PhysicalFileSystem fileSystem, ITracer tracer, string lockPath, string signature, ExistingLockCleanup existingLockCleanup)
        {
            this.fileSystem = fileSystem;
            this.tracer     = tracer;
            this.lockPath   = lockPath;
            this.Signature  = signature;

            if (existingLockCleanup != ExistingLockCleanup.LeaveExisting)
            {
                this.CleanupStaleLock(existingLockCleanup);
            }
        }
コード例 #2
0
        private void CleanupStaleLock(ExistingLockCleanup existingLockCleanup)
        {
            if (!this.LockFileExists())
            {
                return;
            }

            if (existingLockCleanup == ExistingLockCleanup.LeaveExisting)
            {
                throw new ArgumentException("CleanupStaleLock should not be called with LeaveExisting");
            }

            EventMetadata metadata = this.CreateLockMetadata();

            metadata.Add("existingLockCleanup", existingLockCleanup.ToString());

            long length = InvalidFileLength;

            try
            {
                FileProperties existingLockProperties = this.fileSystem.GetFileProperties(this.lockPath);
                length = existingLockProperties.Length;
            }
            catch (Exception e)
            {
                metadata.Add("Exception", "Exception while getting lock file length: " + e.ToString());
                this.tracer.RelatedEvent(EventLevel.Warning, "CleanupEmptyLock", metadata);
            }

            if (length == 0)
            {
                metadata.Add("Message", "Deleting empty lock file: " + this.lockPath);
                this.tracer.RelatedEvent(EventLevel.Warning, "CleanupEmptyLock", metadata);
            }
            else
            {
                metadata.Add("Length", length == InvalidFileLength ? "Invalid" : length.ToString());

                switch (existingLockCleanup)
                {
                case ExistingLockCleanup.DeleteExisting:
                    metadata.Add("Message", "Deleting stale lock file: " + this.lockPath);
                    this.tracer.RelatedEvent(EventLevel.Informational, "CleanupExistingLock", metadata);
                    break;

                case ExistingLockCleanup.DeleteExistingAndLogSignature:
                    string existingSignature;
                    try
                    {
                        string dummyLockerMessage;
                        this.ReadLockFile(out existingSignature, out dummyLockerMessage);
                    }
                    catch (Win32Exception e)
                    {
                        if (e.ErrorCode == NativeMethods.ERROR_FILE_NOT_FOUND)
                        {
                            // File was deleted before we could read its contents
                            return;
                        }

                        throw;
                    }

                    if (existingSignature == this.Signature)
                    {
                        metadata.Add("Message", "Deleting stale lock file: " + this.lockPath);
                        this.tracer.RelatedEvent(EventLevel.Informational, "CleanupExistingLock", metadata);
                    }
                    else
                    {
                        metadata.Add("ExistingLockSignature", existingSignature);
                        metadata.Add("Message", "Deleting stale lock file: " + this.lockPath + " with mismatched signature");
                        this.tracer.RelatedEvent(EventLevel.Warning, "CleanupSignatureMismatchLock", metadata);
                    }

                    break;

                default:
                    throw new InvalidOperationException("Invalid ExistingLockCleanup");
                }
            }

            this.fileSystem.DeleteFile(this.lockPath);
        }