Example #1
0
        public AquireLockAction ShouldAquireLock(FileLock fileLock)
        {
            if (lockIo.LockExists(LockFilePath))
            {
                //Someone else owns the lock
                if (fileLock.GetType() == typeof(OtherProcessHasExclusiveLockOnFileLock))
                {
                    //we couldn't read the file as some other process has it open exlusively
                    return(AquireLockAction.DontAquireLock);
                }

                if (fileLock.GetType() == typeof(UnableToDeserialiseLockFile))
                {
                    var nonDeserialisedLockFile = (UnableToDeserialiseLockFile)fileLock;
                    if ((DateTime.Now - nonDeserialisedLockFile.CreationTime).TotalSeconds > LockTimeout.TotalSeconds)
                    {
                        log.Warn("Lock file existed but was not readable, and has existed for longer than lock timeout. Taking lock.");
                        return(AquireLockAction.AquireLock);
                    }
                    return(AquireLockAction.DontAquireLock);
                }

                //the file no longer exists
                if (fileLock.GetType() == typeof(MissingFileLock))
                {
                    return(AquireLockAction.AquireLock);
                }

                //This lock belongs to this process - we can reacquire the lock
                if (fileLock.BelongsToCurrentProcessAndThread())
                {
                    return(AquireLockAction.AquireLock);
                }

                if (!processFinder.ProcessIsRunning((int)fileLock.ProcessId, fileLock.ProcessName))
                {
                    log.Warn($"Process {fileLock.ProcessId}, thread {fileLock.ThreadId} had lock, but appears to have crashed. Taking lock.");

                    return(AquireLockAction.AquireLock);
                }

                var lockWriteTime = new DateTime(fileLock.Timestamp);
                //The lock has not timed out - we can't acquire it
                if (!(Math.Abs((DateTime.Now - lockWriteTime).TotalSeconds) > LockTimeout.TotalSeconds))
                {
                    return(AquireLockAction.DontAquireLock);
                }

                log.Warn($"Forcibly taking lock from process {fileLock.ProcessId}, thread {fileLock.ThreadId} as lock has timed out. If this happens regularly, please contact Octopus Support.");

                return(AquireLockAction.ForciblyAquireLock);
            }

            return(AquireLockAction.AquireLock);
        }
Example #2
0
 public FileLock ReadLock(string lockFilePath)
 {
     try
     {
         using (var stream = fileSystem.OpenFileExclusively(lockFilePath, FileMode.Open, FileAccess.Read))
         {
             using (var streamReader = new StreamReader(stream))
             {
                 var obj         = JObject.Load(new JsonTextReader(streamReader));
                 var lockContent = new FileLock
                 {
                     ProcessId   = obj["ProcessId"].ToObject <long>(),
                     ProcessName = obj["ProcessName"].ToString(),
                     Timestamp   = obj["Timestamp"].ToObject <long>(),
                     ThreadId    = obj["ThreadId"].ToObject <int>()
                 };
                 if (lockContent.BelongsToCurrentProcessAndThread())
                 {
                     return(lockContent);
                 }
                 return(new OtherProcessOwnsFileLock(lockContent));
             }
         }
     }
     catch (FileNotFoundException)
     {
         return(new MissingFileLock());
     }
     catch (IOException)
     {
         return(new OtherProcessHasExclusiveLockOnFileLock());
     }
     catch (JsonReaderException)
     {
         return(new UnableToDeserialiseLockFile(fileSystem.GetCreationTime(lockFilePath)));
     }
     catch (Exception) //We have no idea what went wrong - reacquire this lock
     {
         return(new OtherProcessHasExclusiveLockOnFileLock());
     }
 }