public bool Lock(string initiationSource, _DeploymentController controller)
        {
            lock (this)
            {
                if (initiationSource != null)
                {
                    _FileDeploymentController fileBasis = controller as _FileDeploymentController;
                    if (fileBasis != null)
                    {
                        if (fileBasis.RequireTargetNameCoordination)
                        {
                            RequiredTargetNameCoordination = true;
                        }
                    }

                    if (controller.UseSubnetCoordination)
                    {
                        if (ControllerManager.KeyManager.Lock(initiationSource, this, ControllerManager.CoordinateWith))
                        {
                            if (RequiredTargetNameCoordination)
                            {
                                string key = STEM.Sys.IO.Path.GetFileName(initiationSource);

                                if (ControllerManager.KeyManager.Lock(key, this, ControllerManager.CoordinateWith))
                                {
                                    LockTime         = DateTime.UtcNow;
                                    InitiationSource = initiationSource;
                                    return(true);
                                }
                                else
                                {
                                    ControllerManager.KeyManager.Unlock(initiationSource, this);
                                }
                            }
                            else
                            {
                                LockTime         = DateTime.UtcNow;
                                InitiationSource = initiationSource;
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        if (ControllerManager.KeyManager.Lock(initiationSource, this))
                        {
                            if (RequiredTargetNameCoordination)
                            {
                                string key = STEM.Sys.IO.Path.GetFileName(initiationSource);

                                if (ControllerManager.KeyManager.Lock(key, this))
                                {
                                    LockTime         = DateTime.UtcNow;
                                    InitiationSource = initiationSource;
                                    return(true);
                                }
                                else
                                {
                                    ControllerManager.KeyManager.Unlock(initiationSource, this);
                                }
                            }
                            else
                            {
                                LockTime         = DateTime.UtcNow;
                                InitiationSource = initiationSource;
                                return(true);
                            }
                        }
                    }
                }

                return(false);
            }
        }
        public void Unlock(STEM.Surge.Messages.ExecutionCompleted message)
        {
            lock (this)
            {
                if (ExecutionCompleted)
                {
                    return;
                }

                if (Branch != null)
                {
                    try
                    {
                        Branch.Remove(this);
                    }
                    catch { }
                }

                if (DeploymentDetails != null && message != null)
                {
                    try
                    {
                        if (DeploymentDetails.Completed == DateTime.MinValue || message.TimeCompleted > DateTime.MinValue)
                        {
                            DeploymentDetails.Exceptions = message.Exceptions;

                            DeploymentDetails.Completed = message.TimeCompleted;

                            if (DeploymentDetails.Completed == DateTime.MinValue)
                            {
                                DeploymentDetails.Completed = DateTime.UtcNow;
                            }

                            if (DeploymentDetails.Issued == DateTime.MinValue || DeploymentDetails.Issued > DeploymentDetails.Completed)
                            {
                                DeploymentDetails.Issued = DeploymentDetails.Completed;
                            }

                            if (DeploymentDetails.Received == DateTime.MinValue || DeploymentDetails.Received > DeploymentDetails.Completed)
                            {
                                DeploymentDetails.Received = DeploymentDetails.Completed;
                            }

                            DeploymentDetails.LastModified = DateTime.UtcNow;

                            if (message.InstructionSetXml != null)
                            {
                                try
                                {
                                    _InstructionSet iSet = _InstructionSet.Deserialize(message.InstructionSetXml) as _InstructionSet;

                                    if (iSet != null)
                                    {
                                        DeploymentDetails.ISet = iSet;
                                    }
                                    else if (DeploymentDetails.ISet != null)
                                    {
                                        DeploymentDetails.ISet.Assigned  = DeploymentDetails.Issued;
                                        DeploymentDetails.ISet.Started   = DeploymentDetails.Received;
                                        DeploymentDetails.ISet.Received  = DeploymentDetails.Received;
                                        DeploymentDetails.ISet.Completed = DeploymentDetails.Completed;
                                    }
                                }
                                catch { }
                            }
                            else if (DeploymentDetails.ISet != null)
                            {
                                DeploymentDetails.ISet.Assigned  = DeploymentDetails.Issued;
                                DeploymentDetails.ISet.Started   = DeploymentDetails.Received;
                                DeploymentDetails.ISet.Received  = DeploymentDetails.Received;
                                DeploymentDetails.ISet.Completed = DeploymentDetails.Completed;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        STEM.Sys.EventLog.WriteEntry("InitiationSourceLockOwner.Unlock", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
                    }
                }

                try
                {
                    if (message != null)
                    {
                        try
                        {
                            _DeploymentController controller = ControllerManager.ValidatedController;

                            if (controller != null)
                            {
                                _ExecutionCompletePool.BeginAsync(new ExecutionComplete(ControllerManager, DeploymentDetails, controller, message), TimeSpan.FromMilliseconds(100));
                            }
                            //if (ControllerManager.CurrentPhase != _ControllerManager.ExecutionPhase.Disposing)
                            //{
                            //    DateTime n = DateTime.UtcNow;
                            //    DateTime e = DateTime.UtcNow;

                            //    _DeploymentController controller = ControllerManager.ValidatedController;

                            //    if (controller != null)
                            //        try
                            //        {
                            //            if (DeploymentDetails != null)
                            //            {
                            //                controller.ExecutionComplete(DeploymentDetails, message.Exceptions);
                            //                controller.InstructionMessageReceived(message, DeploymentDetails);
                            //            }

                            //            controller.MessageReceived(message);

                            //            e = DateTime.UtcNow;
                            //            if ((e - n).TotalMilliseconds > 1000)
                            //                STEM.Sys.EventLog.WriteEntry("InitiationSourceLockOwner.InstructionMessageReceived", "Direct Call \r\nController: " + ControllerManager.DeploymentController + " - Time: " + (e - n).TotalMilliseconds + " ms", STEM.Sys.EventLog.EventLogEntryType.Error);
                            //        }
                            //        catch (Exception ex)
                            //        {
                            //            STEM.Sys.EventLog.WriteEntry("InitiationSourceLockOwner.Unlock", new Exception("Call into " + ControllerManager.DeploymentController + ".ExecutionComplete() threw an exception.", ex), STEM.Sys.EventLog.EventLogEntryType.Error);
                            //        }
                            //}
                        }
                        catch (Exception ex)
                        {
                            STEM.Sys.EventLog.WriteEntry("InitiationSourceLockOwner.Unlock", ex, STEM.Sys.EventLog.EventLogEntryType.Error);
                        }
                    }

                    if (InitiationSource != null)
                    {
                        try
                        {
                            if (RequiredTargetNameCoordination)
                            {
                                string key = STEM.Sys.IO.Path.GetFileName(InitiationSource);
                                ControllerManager.KeyManager.Unlock(key, this);
                            }

                            ControllerManager.KeyManager.Unlock(InitiationSource, this);
                        }
                        catch (Exception ex)
                        {
                            STEM.Sys.EventLog.WriteEntry("InitiationSourceLockOwner.Unlock", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
                        }
                    }
                    finally
                    {
                        InitiationSource = null;
                    }
                }