예제 #1
0
        public static BackgroundWork CreateWork(Window window, Action begin, Action func, Action <BackgroundWorkResult> end)
        {
            BackgroundWork work = new BackgroundWork();

            work.Window       = window;
            work.BeginAction  = begin;
            work.WorkFunction = new Func <int>(() => { func(); return(0); });
            work.EndAction    = new Action <BackgroundWorkResult <int> >(
                (result) => { BackgroundWorkResult newResult = new BackgroundWorkResult(result); end(newResult); });

            return(work);
        }
예제 #2
0
        public void DoWork()
        {
            startedWorksCount  = 0;
            finishedWorksCount = 0;

            foreach (KeyValuePair <string, MachineConnectivity> keyValue in connectivityResults)
            {
                MachineConnectivity connectivity = keyValue.Value;

                if (connectivity.CanPing ?? false)
                {
                    continue;
                }

                startedWorksCount++;

                string machineName = keyValue.Key;
                BackgroundWork <bool> .CreateWork(window, () => { },
                                                  () =>
                {
                    return(PingUtil.PingHost(machineName));
                },
                                                  (taskResult) =>
                {
                    bool result = false;
                    if (taskResult.HasError)
                    {
                        result = false;
                    }
                    else
                    {
                        result = taskResult.Result;
                    }

                    ConsolidatePingResult(machineName, result);
                }
                                                  ).Execute();
            }

            foreach (KeyValuePair <string, MachineConnectivity> keyValue in connectivityResults)
            {
                MachineConnectivity connectivity = keyValue.Value;

                if (connectivity.CanRemotePathAccess ?? false)
                {
                    continue;
                }

                startedWorksCount++;

                string machineName = keyValue.Key;
                string userName    = connectivity.Machine.Credential?.UserName;
                string password    = connectivity.Machine.Credential?.LoginPlainPassword;

                NetworkFileAccess fileAccess = new NetworkFileAccess(machineName, userName, password);

                BackgroundWork.CreateWork(window, () => { },
                                          () =>
                {
                    fileAccess.EnsureDirectory(testingFolderPath);
                    fileAccess.CanAccessDirectory(testingFolderPath);
                    return(0);
                },
                                          (taskResult) =>
                {
                    bool result = true;
                    if (taskResult.HasError)
                    {
                        logger.Error($"Testing of RemotePathAccess failed for machine [{ machineName }]. Message: { taskResult.Exception.ToString() }");
                        result = false;
                    }

                    ConsolidateRemotePathAccessResult(machineName, result);
                }
                                          ).Execute();
            }

            foreach (KeyValuePair <string, MachineConnectivity> keyValue in connectivityResults)
            {
                MachineConnectivity connectivity = keyValue.Value;
                MinerMachine        machine      = connectivity.Machine;
                if (connectivity.CanRemotePowershell ?? false)
                {
                    continue;
                }

                startedWorksCount++;

                string machineName = keyValue.Key;

                BackgroundWork.CreateWork(window, () => { },
                                          () =>
                {
                    TargetMachineExecutor executor = TargetMachineExecutor.GetExecutor(machineName);
                    if (machine.Credential != null)
                    {
                        executor.SetCredential(machine.Credential.UserName, machine.Credential.LoginPlainPassword);
                    }

                    executor.TestConnection();
                    return(0);
                },
                                          (taskResult) =>
                {
                    bool result = true;
                    if (taskResult.HasError)
                    {
                        logger.Error($"Testing of RemotePathAccess failed for machine [{ machineName }]. Message: { taskResult.Exception.ToString() }");
                        result = false;
                    }

                    ConsolidateRemotePowershellResult(machineName, result);
                }
                                          ).Execute();
            }

            while (finishedWorksCount < startedWorksCount)
            {
                Thread.Sleep(30);
            }
        }