Esempio n. 1
0
        public void DeletePackagesServiceItems()
        {
            // impersonate thread
            if (userId != -1)
            {
                SecurityContext.SetThreadPrincipal(userId);
            }


            // delete package by package
            foreach (PackageInfo package in packages)
            {
                TaskManager.StartTask("SPACE", "DELETE_ITEMS", package.PackageName);
                TaskManager.WriteParameter("User", package.UserId);

                // get package service items
                List <ServiceProviderItem> items = PackageController.GetServiceItemsForStatistics(
                    0, package.PackageId, false, false, false, true); // disposable items

                // check vm serviceId
                bool changed = false;
                for (int i = 0; i < items.Count; i++)
                {
                    if (items[i] is VirtualMachine)
                    {
                        int newServiceId = VirtualizationServerController2012.DiscoverVirtualMachine(items[i].Id);
                        if (items[i].ServiceId != newServiceId && newServiceId != -1)
                        {
                            changed = true;
                        }
                    }
                }
                if (changed)
                {
                    items = PackageController.GetServiceItemsForStatistics(0, package.PackageId, false, false, false, true);
                }

                // order items by service
                Dictionary <int, List <ServiceProviderItem> > orderedItems =
                    PackageController.OrderServiceItemsByServices(items);

                // delete service items by service sets
                foreach (int serviceId in orderedItems.Keys)
                {
                    ServiceInfo service = ServerController.GetServiceInfo(serviceId);
                    //Delete Exchange Organization
                    if (service.ProviderId == 103 /*Organizations*/)
                    {
                        OrganizationController.DeleteOrganization(orderedItems[serviceId][0].Id);
                        //int exchangeId = PackageController.GetPackageServiceId(package.PackageId, ResourceGroups.Exchange2007);
                        //ExchangeServerController.DeleteOrganization(orderedItems[serviceId][0].Id);
                    }
                    else
                    {
                        ProcessServiceItems(false, false, serviceId, orderedItems[serviceId]);
                    }
                }

                #region Try to DeletePackage
                //Unfornatually, if we move that try-catch deeper in DataProvider, some SQL queries will execute extremely long.
                //That why try-catch implemented here and only for DeletePackage
                int  attempts  = 4;
                int  attempt   = 0;
                bool success   = false;
                var  exception = new Exception();
                while (attempt < attempts)
                {
                    attempt++;
                    try
                    {
                        // delete package from database
                        DataProvider.DeletePackage(SecurityContext.User.UserId, package.PackageId);

                        success = true;
                        break;
                    }
                    catch (System.Data.SqlClient.SqlException ex)
                    {
                        exception = ex;
                        TaskManager.WriteError(ex);
                        Thread.Sleep(5000 * attempt); //wait, if we get a deadlock exception
                    }
                    catch (Exception ex)              //Normally that Exception should never appears
                    {
                        exception = ex;
                        TaskManager.WriteError(ex);
                        Thread.Sleep(5000); //wait, if we get unknow exception
                    }
                }
                //if all attempts were failed throw an unhandled exception and try to figure out how to fix them (it is a typical situation before was added this code).
                //unhandled exceptions usually create stuck tasks in the serveradmin/users -> "Running Tasks"
                if (!success)
                {
                    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(exception).Throw(); //rethrow InnerException without losing stack trace
                }
                #endregion
            }

            // add log record
            TaskManager.CompleteTask();
        }
Esempio n. 2
0
 public int DiscoverVirtualMachine(int itemId)
 {
     return(VirtualizationServerController2012.DiscoverVirtualMachine(itemId));
 }