Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0932a9e2

Přidáno uživatelem Pultak před asi 2 roky(ů)

re #9443 refactoring of detection classes

Zobrazit rozdíly:

ld_client/LDClient/detection/DebuggerInfoParser.cs
15 15
                throw new ArgumentException($"Expected {ExpectedNumberOfMatches} matches to be found in the text (actually found: {matches.Count})");
16 16
            }
17 17
            
18
            return (matches[0].ToString(), matches[1].ToString());
18
            return (matches[1].ToString().Trim(), matches[0].ToString().Trim());
19 19
        }
20 20
    }
21 21
}
ld_client/LDClient/detection/IInfoFetcher.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6

  
7
namespace LDClient.detection {
8
    public interface IInfoFetcher {
9

  
10
        public string HeadSerialNumber { get; set; }
11
        public string BodySerialNumber { get; set; }
12

  
13
        public Task<bool> FetchDataAsync();
14
    }
15
}
ld_client/LDClient/detection/IProcessUtils.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6

  
7
namespace LDClient.detection {
8
    public interface IProcessUtils {
9
        public bool IsProcessRunning(string name);
10

  
11
        public bool ExecuteNewProcess(string fileName, string argument, int timeout, int desiredExitCode);
12
    }
13
}
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
            }
ld_client/LDClient/detection/ProcessDetection.cs
4 4

  
5 5
namespace LDClient.detection {
6 6
   
7
	 public sealed class ProcessProcessDetection : IProcessDetection {
7
	 public sealed class ProcessDetection : IProcessDetection {
8 8
        
9 9
        private const string DatetimeFormat = "yyyy-MM-dd hh:mm:ss";
10 10

  
11 11
        private readonly string _processName;
12 12
        private readonly uint _detectionPeriodMs;
13
        private readonly InfoFetcher _infoFetcher;
13
        private readonly IInfoFetcher _infoFetcher;
14 14
        private readonly IApiClient _apiClient;
15
        
15
        private readonly IProcessUtils _processUtils;
16

  
16 17
        private bool _processIsActive;
17 18
        private bool _failedToRetrieveData;
18 19
        private Payload? _lastConnectedPayload;
19 20

  
20
        public ProcessProcessDetection(string processName, uint detectionPeriodMs, InfoFetcher infoFetcher, IApiClient apiClient) {
21
        public bool DetectionRunning = false;
22

  
23
        public ProcessDetection(string processName, uint detectionPeriodMs, IInfoFetcher infoFetcher, IApiClient apiClient, IProcessUtils processUtils) {
21 24
            _processName = processName;
22 25
            _detectionPeriodMs = detectionPeriodMs;
23 26
            _infoFetcher = infoFetcher;
24 27
            _apiClient = apiClient;
25 28
            _failedToRetrieveData = false;
29
            _processUtils = processUtils;
26 30
        }
27 31

  
28 32
        private async Task<bool> RetrieveDataFromDebugger() {
......
43 47
        }
44 48

  
45 49
        private async Task DetectProcessAsync() {
46
            var processExists = Process.GetProcessesByName(_processName).Length > 0;
50
            var processExists = _processUtils.IsProcessRunning(_processName);
47 51

  
48 52
            if (processExists && !_processIsActive) {
49 53
                Program.DefaultLogger.Info($"Process started: {_processName}");
......
77 81
        
78 82
        public async void RunPeriodicDetection() {
79 83
            Program.DefaultLogger.Info("Process periodic detector has started");
80
            while (true) {
84
            DetectionRunning = true;
85
            while (DetectionRunning) {
81 86
                await DetectProcessAsync();
82 87
                Thread.Sleep((int)_detectionPeriodMs);
83 88
            }
84
            // ReSharper disable once FunctionNeverReturns
85 89
        }
86 90
    }
87 91
}
ld_client/LDClient/detection/ProcessUtils.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7

  
8
namespace LDClient.detection {
9
    public class ProcessUtils : IProcessUtils{
10

  
11
        public bool IsProcessRunning(string name) {
12
            return Process.GetProcessesByName(name).Length > 0;
13
        }
14

  
15

  
16
        public bool ExecuteNewProcess(string fileName, string argument, int timeout, int desiredExitCode) {
17

  
18
            var t32RemProcess = new Process();
19
            t32RemProcess.StartInfo.FileName = fileName;
20
            t32RemProcess.StartInfo.Arguments = argument;
21
            try {
22
                t32RemProcess.Start();
23
                if (!t32RemProcess.WaitForExit(timeout)) {
24
                    Program.DefaultLogger.Error($"Execution has not terminated within a predefined timeout of {timeout} ms");
25
                    return false;
26
                }
27
                if (t32RemProcess.ExitCode != desiredExitCode) {
28
                    Program.DefaultLogger.Error($"Execution terminated with an error code of {t32RemProcess.ExitCode}");
29
                    return false;
30
                }
31
            } catch (Exception exception) {
32
                Program.DefaultLogger.Error($"Failed to run {fileName} {argument}. {exception.Message}");
33
                return false;
34
            }
35

  
36
            return true;
37
        }
38

  
39
    }
40
}

Také k dispozici: Unified diff