Exemple #1
0
        //EBVMInstanceStatus is the condition in here
        private int PerformActionOnInstances(
            Tuple <string, EBVMInstanceAction, EBVMInstanceStatus>[] _Operations,
            Action _OnCompleted,
            Action _OnFailure,
            Action <string> _ErrorMessageAction = null)
        {
            int ProgressStackIx = Interlocked.Increment(ref CurrentActionIndex);

            var ProgressStack = new Stack <object>();

            if (_Operations != null && _Operations.Length > 0)
            {
                lock (ProgressStacks_Lock)
                {
                    ProgressStacks.Add(ProgressStackIx, ProgressStack);
                }

                var Request = new ConcurrentQueue <Task>();

                foreach (var _Operation in _Operations)
                {
                    var FoundInstance = FindInstanceByUniqueName(_Operation.Item1, _ErrorMessageAction);
                    if (FoundInstance != null)
                    {
                        if (GetStatusFromString(FoundInstance.PowerState.ToString()) == _Operation.Item3)
                        {
                            Task RequestAction = null;
                            if (_Operation.Item2 == EBVMInstanceAction.Start)
                            {
                                RequestAction = FoundInstance.StartAsync();
                                //FoundInstance.Start();
                            }
                            else if (_Operation.Item2 == EBVMInstanceAction.Stop)
                            {
                                RequestAction = FoundInstance.DeallocateAsync();
                            }

                            if (RequestAction != null)
                            {
                                Request.Enqueue(RequestAction);
                                ProgressStack.Push(new object());
                            }
                        }
                    }
                }
                if (ProgressStack.Count > 0)
                {
                    BTaskWrapper.Run(() =>
                    {
                        Thread.CurrentThread.IsBackground = true;

                        try
                        {
                            if (Request.TryDequeue(out Task CreatedTask))
                            {
                                using (CreatedTask)
                                {
                                    CreatedTask.Wait();
                                    lock (ProgressStacks_Lock)
                                    {
                                        if (ProgressStacks.TryGetValue(ProgressStackIx, out Stack <object> FoundStack) && FoundStack.Count > 0)
                                        {
                                            if (CreatedTask.Exception != null)
                                            {
                                                _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformActionOnInstances->Error: " + CreatedTask.Exception.Message);
                                                FoundStack.Clear();
                                                _OnFailure?.Invoke();
                                            }
                                            else
                                            {
                                                FoundStack.Pop();
                                                if (FoundStack.Count == 0)
                                                {
                                                    ProgressStacks.Remove(ProgressStackIx);
                                                    _OnCompleted?.Invoke();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformActionOnInstances->TryDequeue error occured.");
                                _OnFailure?.Invoke();
                            }
                        }
                        catch (Exception e)
                        {
                            _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformActionOnInstances->Exception: " + e.Message);
                            _OnFailure?.Invoke();
                        }
                    });
                }
                else
                {
                    lock (ProgressStacks_Lock)
                    {
                        ProgressStacks.Remove(ProgressStackIx);
                    }
                }
            }
            return(ProgressStack.Count);
        }
Exemple #2
0
        //EBVMInstanceStatus is the condition in here
        private int PerformRunCommandActions(
            string[] _UniqueInstanceNames,
            EBVMOSType _VMOperationSystemType,
            string[] _Commands,
            Action _OnCompleted,
            Action _OnFailure,
            Action <string> _ErrorMessageAction = null)
        {
            int ProgressStackIx = Interlocked.Increment(ref CurrentActionIndex);

            var ProgressStack = new Stack <object>();

            if (_UniqueInstanceNames != null && _UniqueInstanceNames.Length > 0)
            {
                lock (ProgressStacks_Lock)
                {
                    ProgressStacks.Add(ProgressStackIx, ProgressStack);
                }

                var Request = new ConcurrentQueue <Task>();

                foreach (var _InstanceName in _UniqueInstanceNames)
                {
                    var FoundInstance = FindInstanceByUniqueName(_InstanceName, _ErrorMessageAction);
                    if (FoundInstance != null)
                    {
                        if (GetStatusFromString(FoundInstance.PowerState.ToString()) == EBVMInstanceStatus.Running)
                        {
                            try
                            {
                                var _CommandId = "RunPowerShellScript";
                                if (_VMOperationSystemType == EBVMOSType.Linux)
                                {
                                    _CommandId = "RunShellScript";
                                }

                                var _RunCommandInput = new RunCommandInput()
                                {
                                    CommandId = _CommandId,
                                    Script    = _Commands.ToList()
                                };
                                Task RequestAction = FoundInstance.RunCommandAsync(_RunCommandInput);
                                Request.Enqueue(RequestAction);
                                ProgressStack.Push(new object());
                            }
                            catch (System.Exception ex)
                            {
                                _ErrorMessageAction?.Invoke($"BVMServiceAZ->PerformRunCommandActions->An error occurred when RunCommandInput is casting. Error: { ex.Message } - StackTrace: {ex.StackTrace}");
                                _OnFailure?.Invoke();
                            }
                        }
                        else
                        {
                            _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Virtual Machine is not running.");
                            _OnFailure?.Invoke();
                        }
                    }
                }
                if (ProgressStack.Count > 0)
                {
                    BTaskWrapper.Run(() =>
                    {
                        Thread.CurrentThread.IsBackground = true;

                        try
                        {
                            if (Request.TryDequeue(out Task CreatedTask))
                            {
                                using (CreatedTask)
                                {
                                    CreatedTask.Wait();
                                    lock (ProgressStacks_Lock)
                                    {
                                        if (ProgressStacks.TryGetValue(ProgressStackIx, out Stack <object> FoundStack) && FoundStack.Count > 0)
                                        {
                                            if (CreatedTask.Exception != null)
                                            {
                                                _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Error: " + CreatedTask.Exception.Message);
                                                FoundStack.Clear();
                                                _OnFailure?.Invoke();
                                            }
                                            else
                                            {
                                                FoundStack.Pop();
                                                if (FoundStack.Count == 0)
                                                {
                                                    ProgressStacks.Remove(ProgressStackIx);
                                                    _OnCompleted?.Invoke();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->TryDequeue error occured.");
                                _OnFailure?.Invoke();
                            }
                        }
                        catch (Exception e)
                        {
                            _ErrorMessageAction?.Invoke("BVMServiceAZ->PerformRunCommandActions->Exception: " + e.Message);
                            _OnFailure?.Invoke();
                        }
                    });
                }
                else
                {
                    lock (ProgressStacks_Lock)
                    {
                        ProgressStacks.Remove(ProgressStackIx);
                    }
                }
            }
            return(ProgressStack.Count);
        }
Exemple #3
0
        //EBVMInstanceStatus is the condition in here
        private int PerformActionOnInstances(
            Tuple <string, EBVMInstanceAction, EBVMInstanceStatus>[] _Operations,
            Action _OnCompleted,
            Action _OnFailure,
            Action <string> _ErrorMessageAction = null)
        {
            int ProgressStackIx = Interlocked.Increment(ref CurrentActionIndex);

            var ProgressStack = new Stack <object>();

            if (_Operations != null && _Operations.Length > 0)
            {
                lock (ProgressStacks_Lock)
                {
                    ProgressStacks.Add(ProgressStackIx, ProgressStack);
                }

                var Service = GetService(); //Will be disposed in async methods
                var Request = new BatchRequest(Service);

                foreach (var _Operation in _Operations)
                {
                    var FoundInstance = FindInstanceByUniqueName(_Operation.Item1, _ErrorMessageAction);
                    if (FoundInstance != null)
                    {
                        if (GetStatusFromString(FoundInstance.Status) == _Operation.Item3)
                        {
                            IClientServiceRequest RequestAction = null;
                            if (_Operation.Item2 == EBVMInstanceAction.Start)
                            {
                                RequestAction = Service.Instances.Start(ProjectID, ZoneName, FoundInstance.Name);
                            }
                            else if (_Operation.Item2 == EBVMInstanceAction.Stop)
                            {
                                RequestAction = Service.Instances.Stop(ProjectID, ZoneName, FoundInstance.Name);
                            }

                            if (RequestAction != null)
                            {
                                Request.Queue <Instance>(RequestAction,
                                                         (Content, Error, i, Message) =>
                                {
                                    lock (ProgressStacks_Lock)
                                    {
                                        if (ProgressStacks.TryGetValue(ProgressStackIx, out Stack <object> FoundStack) && FoundStack.Count > 0)
                                        {
                                            if (Error != null)
                                            {
                                                _ErrorMessageAction?.Invoke("BVMServiceGC->PerformActionOnInstances->Error: " + Error.Message);
                                                FoundStack.Clear();
                                                _OnFailure?.Invoke();
                                            }
                                            else
                                            {
                                                FoundStack.Pop();
                                                if (FoundStack.Count == 0)
                                                {
                                                    ProgressStacks.Remove(ProgressStackIx);
                                                    _OnCompleted?.Invoke();
                                                }
                                            }
                                        }
                                    }
                                });
                                ProgressStack.Push(new object());
                            }
                        }
                    }
                }
                if (ProgressStack.Count > 0)
                {
                    BTaskWrapper.Run(() =>
                    {
                        Thread.CurrentThread.IsBackground = true;

                        try
                        {
                            using (var CreatedTask = Request.ExecuteAsync())
                            {
                                CreatedTask.Wait();
                            }
                        }
                        catch (Exception e)
                        {
                            _ErrorMessageAction?.Invoke("BVMServiceGC->PerformActionOnInstances->Exception: " + e.Message);
                            _OnFailure?.Invoke();
                        }
                        Service?.Dispose();
                    });
                }
                else
                {
                    lock (ProgressStacks_Lock)
                    {
                        ProgressStacks.Remove(ProgressStackIx);
                    }
                    Service?.Dispose();
                }
            }
            return(ProgressStack.Count);
        }