Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0932a9e2

Přidáno uživatelem Pultak před více než 2 roky(ů)

re #9443 refactoring of detection classes

Zobrazit rozdíly:

ld_client/LDClient/detection/InfoFetcher.cs
1 1
using System.Diagnostics;
2
using LDClient.utils;
2 3
using LDClient.utils.loggers;
3 4

  
4 5
namespace LDClient.detection {
5 6

  
6
    public class InfoFetcher {
7
    public class InfoFetcher : IInfoFetcher {
7 8
        
8 9
        private const string UndefinedSerialNumber = "number";
9 10

  
......
15 16
        private readonly uint _waitPeriodMs;
16 17
        private readonly string _infoFilePath;
17 18

  
18
        public string HeadSerialNumber { get; private set; } = UndefinedSerialNumber;
19
        public string BodySerialNumber { get; private set; } = UndefinedSerialNumber;
19
        public IProcessUtils ProcessUtils;
20
        public IFileUtils FileUtils;
20 21

  
21
        public InfoFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable, string[] f32RemArguments, int f32SuccessExitCode, int f32WaitTimeoutMs) {
22
        public string HeadSerialNumber { get; set; } = UndefinedSerialNumber;
23
        public string BodySerialNumber { get; set; } = UndefinedSerialNumber;
24

  
25

  
26
        public InfoFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable,
27
            string[] f32RemArguments, int f32SuccessExitCode, int f32WaitTimeoutMs) {
22 28
            _maxAttempts = maxAttempts;
23 29
            _waitPeriodMs = waitPeriodMs;
24 30
            _infoFilePath = infoFilePath;
......
26 32
            _f32RemArguments = f32RemArguments;
27 33
            _f32SuccessExitCode = f32SuccessExitCode;
28 34
            _f32WaitTimeoutMs = f32WaitTimeoutMs;
35
            ProcessUtils = new ProcessUtils();
36
            FileUtils = new FileUtils();
37

  
29 38
        }
30 39

  
31 40
        public async Task<bool> FetchDataAsync() {
......
49 58

  
50 59
        private bool RetrieveDebuggerInfo(string filePath) {
51 60
            try {
52
                var fileContent = File.ReadAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
61
                var fileContent = FileUtils.ReadFileAllLines(filePath).Aggregate("", (current, line) => $"{current}{line}\n");
53 62
                var (headSerialNumber, bodySerialNumber) = DebuggerInfoParser.Parse(fileContent);
54 63
                HeadSerialNumber = headSerialNumber;
55 64
                BodySerialNumber = bodySerialNumber;
......
61 70
            return true;
62 71
        }
63 72

  
64
        private static bool SendRetrieveInfoCommands(string executableFile, IReadOnlyList<string>? arguments, int successExitCode, int waitTimeoutMs) {
73
        private bool SendRetrieveInfoCommands(string executableFile, IReadOnlyList<string>? arguments, int desiredExitCode, int waitTimeoutMs) {
65 74
            if (arguments == null) {
66 75
                Program.DefaultLogger.Error($"Failed to run {executableFile} - no parameters were given");
67 76
                return false;
68 77
            }
69 78
            foreach (var argument in arguments) {
70
                var t32RemProcess = new Process();
71
                t32RemProcess.StartInfo.FileName = executableFile;
72
                t32RemProcess.StartInfo.Arguments = argument;
73
                try {
74
                    t32RemProcess.Start();
75
                    if (!t32RemProcess.WaitForExit(waitTimeoutMs)) {
76
                        Program.DefaultLogger.Error($"Execution has not terminated within a predefined timeout of {waitTimeoutMs} ms");
77
                        return false;
78
                    }
79
                    if (t32RemProcess.ExitCode != successExitCode) {
80
                        Program.DefaultLogger.Error($"Execution terminated with an error code of {t32RemProcess.ExitCode}");
81
                        return false;
82
                    }
83
                } catch (Exception exception) {
84
                    Program.DefaultLogger.Error($"Failed to run {executableFile} {argument}. {exception.Message}");
79
                if (!ProcessUtils.ExecuteNewProcess(executableFile, argument, waitTimeoutMs, desiredExitCode)) {
85 80
                    return false;
86 81
                }
87 82
            }

Také k dispozici: Unified diff