-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.cs
More file actions
167 lines (140 loc) · 6.76 KB
/
Copy pathWorker.cs
File metadata and controls
167 lines (140 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Xml;
namespace DispatcherService
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected private IConfiguration returnConfiguration()
{
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
return configuration;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
IConfiguration configuration = returnConfiguration();
int timerTime = Convert.ToInt32(configuration["DispatcherSettings:timerTime"]);
string scriptDir = configuration["DispatcherSettings:scriptDir"];
string configName = configuration["DispatcherSettings:configName"];
XmlDocument doc = new XmlDocument();
try
{
doc.Load(scriptDir + "\\" + configName);
}
catch (Exception e)
{
_logger.LogInformation("7A: Cannot open XML Configuration at " + scriptDir + "\\" + configName + e);
}
try
{
timerTime = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("/configuration/timerTime").InnerText);
}
catch (Exception e)
{
_logger.LogInformation("7C: Cannot reset timer time " + e);
}
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine("Processing.");
TimerElapsed();
await Task.Delay(timerTime, stoppingToken);
}
}
private void TimerElapsed()
{
int ErrorRun = 0;
String serviceStartStopStatus = "started";
IConfiguration configuration = returnConfiguration();
string scriptDir = configuration["DispatcherSettings:scriptDir"];
string configName = configuration["DispatcherSettings:configName"];
XmlDocument doc = new XmlDocument();
try
{
doc.Load(scriptDir + "\\" + configName);
String pattern = "\\\\+";
String replacement = "\\";
Regex rgx = new Regex(pattern);
String pattern2 = "\r";
String replacement2 = "";
Regex srp = new Regex(pattern2);
String scriptName = doc.DocumentElement.SelectSingleNode("/configuration/scriptName").InnerText;
String exeLocation = doc.DocumentElement.SelectSingleNode("/configuration/exeLocation").InnerText;
String exeName = doc.DocumentElement.SelectSingleNode("/configuration/exeName").InnerText;
String exeParameters = doc.DocumentElement.SelectSingleNode("/configuration/exeParameters").InnerText;
scriptDir = rgx.Replace(scriptDir, replacement);
scriptName = rgx.Replace(scriptName, replacement);
exeLocation = rgx.Replace(exeLocation, replacement);
exeName = rgx.Replace(exeName, replacement);
exeParameters = rgx.Replace(exeParameters, replacement);
scriptDir = srp.Replace(scriptDir, replacement2);
scriptName = srp.Replace(scriptName, replacement2);
exeLocation = srp.Replace(exeLocation, replacement);
exeName = srp.Replace(exeName, replacement);
exeParameters = srp.Replace(exeParameters, replacement);
if (!System.IO.Directory.Exists(scriptDir))
{
ErrorRun = 1;
_logger.LogInformation("1: The Script Directory " + scriptDir + " does not exist");
}
if (!File.Exists(scriptDir + "\\" + scriptName))
{
ErrorRun = 1;
_logger.LogInformation("2: The Executable Script File " + scriptDir + "\\" + scriptName + " does not exist");
}
if (!File.Exists(scriptDir + "\\" + configName))
{
ErrorRun = 1;
_logger.LogInformation("2: The Configuration File " + scriptDir + "\\" + configName + " does not exist");
}
if (!File.Exists(exeLocation + "\\" + exeName))
{
ErrorRun = 1;
_logger.LogInformation("4: The Executable File " + exeLocation + "\\" + exeName + " does not exist");
}
if (ErrorRun == 0)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "\"" + exeLocation + "\\" + exeName + "\"";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
//Reconfig php input vars
if (exeParameters == "" || exeParameters == null)
{
_logger.LogInformation("DispatcherService: Starting without parameters.");
startInfo.Arguments = "\"" + scriptDir + "\\" + scriptName + "\" \"STATUS=" + serviceStartStopStatus + "\" \"scriptDirectory=" + scriptDir + "\" \"configurationFile=" + configName + "\"";
}
else
{
_logger.LogInformation("DispatcherService: Starting with parameters.");
startInfo.Arguments = "\"" + exeParameters + " "+ scriptDir + "\\" + scriptName + "\" \"STATUS=" + serviceStartStopStatus + "\" \"scriptDirectory=" + scriptDir + "\" \"configurationFile=" + configName + "\"";
}
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
_logger.LogInformation("5: There was an error executing " + exeLocation + "\\" + exeName + " with the flags " + "\"" + scriptDir + "\\" + scriptName + "\"");
}
}
}
catch (Exception f)
{
_logger.LogInformation("7B: Cannot open XML Configuration at " + scriptDir + "\\" + configName + f);
}
}
}
}