Example #1
0
        private void ProcessServiceItems(bool changeState, bool enabled,
                                         int serviceId, List <ServiceProviderItem> items)
        {
            string methodName = changeState ? "ChangeServiceItemsState" : "DeleteServiceItems";

            int PACKET_SIZE = 10;
            int ATTEMPTS    = 3;

            TaskManager.Write(String.Format("Start analyze {0} service ({1} items)",
                                            serviceId, items.Count));

            try
            {
                // convert items to SoapObjects by packets of 0
                int startItem = 0;
                List <SoapServiceProviderItem> objItems = new List <SoapServiceProviderItem>();

                for (int i = 0; i < items.Count; i++)
                {
                    // add to the packet
                    objItems.Add(SoapServiceProviderItem.Wrap(items[i]));

                    if (((i > 0) && (i % PACKET_SIZE == 0)) ||
                        i == (items.Count - 1))    // packet is ready
                    {
                        if (objItems.Count == 0)
                        {
                            continue;
                        }

                        int  attempt = 0;
                        bool success = false;
                        while (attempt < ATTEMPTS)
                        {
                            // increment attempt
                            attempt++;

                            try
                            {
                                // send packet for calculation
                                // invoke service provider
                                TaskManager.Write(String.Format("Invoke {0} method ('{1}' - '{2}' items) - {3} attempt",
                                                                methodName, items[startItem].Name, items[i].Name, attempt));

                                ServiceProvider prov = new ServiceProvider();
                                ServiceProviderProxy.Init(prov, serviceId);

                                if (changeState)
                                {
                                    prov.ChangeServiceItemsState(objItems.ToArray(), enabled);
                                }
                                else
                                {
                                    if (items[i] is VirtualMachine)
                                    {
                                        VirtualizationServerController2012.CheckCustomPsScript(VirtualizationServerController2012.PsScriptPoint.before_deletion, (VirtualMachine)items[i]);
                                    }
                                    prov.DeleteServiceItems(objItems.ToArray());
                                }

                                // exit from the loop
                                success = true;
                                break;
                            }
                            catch (Exception ex)
                            {
                                TaskManager.WriteWarning(ex.ToString());
                            }
                        }

                        if (!success)
                        {
                            throw new Exception("The number of attemtps has been reached. The whole operation aborted.");
                        }

                        // reset packet counter
                        startItem = i + 1;
                        objItems.Clear();
                    }
                } // end for items
            }
            catch (Exception ex)
            {
                // log exception
                TaskManager.WriteWarning(ex.ToString());
            }

            TaskManager.Write(String.Format("End analyze {0} service ({1} items)",
                                            serviceId, items.Count));
        }