-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCron.cs
383 lines (351 loc) · 14.6 KB
/
Cron.cs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using Cronix.Extensions;
using System.Text.RegularExpressions;
using System.Reflection.Emit;
namespace Cronix
{
/// <summary>
/// A class that holds information for a cron job.
/// </summary>
public class Cron
{
/// <summary>
/// The time the cron job was started.
/// </summary>
public DateTime StartTime { get; set; }
/// <summary>
/// The time the cron job stopped.
/// </summary>
public DateTime StopTime { get; set; }
/// <summary>
/// The configuration file.
/// </summary>
public IConfigurations Configs { get; private set; }
/// <summary>
/// The title of the cronix application.
/// </summary>
public string Identity { get; set; } = "Cron app";
/// <summary>
/// The current process Id.
/// </summary>
public int PID { get; protected set; } = -1;
/// <summary>
/// Constructs the Cron class.
/// </summary>
/// <param name="configs">The coniguration to use.</param>
public Cron(IConfigurations configs, string identity = "Cron app")
{
Configs = configs ?? new Configurations();
Identity = identity;
}
/// <summary>
/// Starts the cron app with the message.
/// </summary>
/// <param name="message">The message to use for the start of the log. Default is "Starting App."</param>
public void Start()
{
PID = Environment.ProcessId;
// Start the clock.
StartTime = DateTime.Now;
Log("Starting " + Identity + ".");
if (Configs.HealthChecksUri != null)
{
LogInfo("Sending healthchecks start.");
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("rid", PID.ToString()),
});
var result = client.PostAsync(Configs.HealthChecksUri.ToString().TrimEnd('/') + "/start", content).Result;
}
// First we check if all the directories are set.
// Check to see if log path exists. If not, we create it.
if (!Directory.Exists(Configs.LogPath))
{
LogInfo("The log directory does not exist, attempting to create it.");
try
{
Directory.CreateDirectory(Configs.LogPath);
LogInfo("Log directory created successfully.");
} catch (Exception ex)
{
Debug("Could not create the log path ('" + Configs.LogPath + "') with message: '" + ex.Message + "'");
Log("Could not create the log path.", Level.Err);
Fail(exitCode: 2);
}
}
// Check PID directory.
if (!Directory.Exists(Path.GetDirectoryName(Configs.PID)))
{
LogInfo("The PID directory doesn't exists, attempting to create it.");
// It doesn't exist, lets make it.
var dir = Path.GetDirectoryName(Configs.PID);
if (dir != null)
{
try
{
Directory.CreateDirectory(dir);
LogInfo("PID created successfully.");
} catch (Exception ex)
{
Debug("Could not create the pid path ('" + dir + "') with message: '" + ex.Message + "'");
Debug("You might need to create pid directory and ensure it has write permissions for user running application.");
Log("Could not create pid path.", Level.Err);
Fail(exitCode: 3);
}
}
}
// Lets see if the process is running.
if (File.Exists(Configs.PID))
{
// The file exists, it may still be running.
var storedPid = File.ReadAllText(Configs.PID);
if (ProcessIsRunning(storedPid))
{
Log("The process is currently running. Exiting.", level: Level.Warning);
Environment.Exit(5);
}
else
{
Log("The PID is assumed to be orphaned or not running.");
LogInfo("Attempting to remove pid file.");
try
{
File.Delete(Configs.PID);
LogInfo("The PID file was deleted successfully.");
}
catch (Exception ex)
{
Debug("Could not delete PID file ('" + Configs.PID + "') with message: '" + ex.Message + "'");
Log("Exiting with error. Could not remove PID file.", Level.Err);
Fail(exitCode: 4);
}
}
}
// Creating the pid file.
Log("Process id is '" + PID + "'.");
Log("Creating PID file.");
try
{
File.WriteAllText(Configs.PID, PID.ToString());
LogInfo("PID file created successfuly.");
}
catch (Exception ex)
{
Debug("Could not create PID file at ('" + Configs.PID + "') with message: '" + ex.Message + "'");
Log("Exiting with error. Could not write PID file.", Level.Err);
Fail(exitCode: 4);
}
// Starting the log.
if (File.Exists(Configs.Log))
{
// Log file exists. Checking size.
var info = new FileInfo(Configs.Log);
if (info.Length > Configs.LogMaxSize * 1024 * 1024 || info.CreationTime > DateTime.Now.AddDays(Configs.LogMaxAge))
{
// Log file is too large or older than the specified time.
Log("Log file set for rotation.");
LogInfo("Rotating log file.");
var newName = Configs.LogPath + Configs.LogName + ".old." + DateTime.Now.ToString("s");
Debug("Attempting to move log file to '" + newName + "'.");
try
{
File.Move(Configs.Log, Configs.LogPath + "/" + Configs.LogName + ".old." + DateTime.Now.ToString("s"));
Debug("File moved successfully.");
}
catch (Exception ex)
{
Debug("Could not move log file from '" + Configs.Log + "' to '" + newName + "' with message: '" + ex.Message + "'.");
Log("Exiting with error. Could not move log file.", Level.Err);
Fail(exitCode: 1);
}
LogInfo("Creating new log file.");
CreateLogFile();
}
}
// Getting list of log files
var oldLogs = new List<string>();
try
{
oldLogs = Directory.GetFiles(Configs.LogPath, Configs.LogName + ".old.*").Select(p => Path.GetFileName(p)).ToList();
Debug("Obtained log files (" + oldLogs.Count + ") from '" + Configs.LogPath + "'.");
}
catch (Exception ex)
{
Debug("Could not get file list from the log path ('" + Configs.LogPath + "') with message: '" + ex.Message + "'");
Log("Could not get directory list of the log path.", Level.Err);
Environment.Exit(2);
}
if (oldLogs.Count > Configs.LogMaxCount)
{
LogInfo("The log limit is set at " + Configs.LogMaxCount + " and there are " + oldLogs.Count + ". Deleting excess.");
// We got too many logs, we will delete oldest to newest until we have five.
var orderedList = new SortedList<DateTime, string>();
foreach (var fileName in oldLogs)
{
var match = long.Parse(Regex.Match(fileName, Configs.LogName + @"\.old\.(.+)").Groups[1].Value);
var date = new DateTime(match);
orderedList.Add(date, fileName);
}
foreach (var entry in orderedList.Take(orderedList.Count - 5))
{
try
{
Debug("Attempting to delete file " + entry.Value + ".");
File.Delete(Configs.LogPath + entry.Value);
}
catch (Exception ex)
{
Debug("Could not delete log file at '" + entry.Value + "' with message: '" + ex.Message + "'.");
Log("Exiting with error. Could not create log file.", Level.Err);
Fail(exitCode: 2);
}
}
}
}
/// <summary>
/// Fails the cron app and writes the fail message.
/// </summary>
/// <param name="message">The message to write to the logs. Default is "Ending cron app in a failed state."</param>
public void Fail(string message = "Ending cron app in a failed state.", int exitCode = 1)
{
Log(message, Level.Err);
File.WriteAllText(Configs.PID, "failed");
RunTime();
if (Configs.HealthChecksUri != null)
{
LogInfo("Sending healthchecks fail.");
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("rid", PID.ToString()),
});
_ = client.PostAsync(Configs.HealthChecksUri.ToString().TrimEnd('/') + "/fail", content).Result;
}
Environment.Exit(exitCode);
}
/// <summary>
/// Marks the job as complete
/// </summary>
/// <param name="message"></param>
public void Complete(string message = "Processs complete.")
{
Log(message);
Debug("Attempting to remove the pid file.");
try
{
File.Delete(Configs.PID);
LogInfo("Pid file removed.");
}
catch (Exception ex)
{
Debug("Failed to remove the pid file at '" + Configs.PID + "' with message: '" + ex.Message + "'.");
Log("Failed to remove the pid file.", Level.Err);
Fail(exitCode: 2);
}
RunTime();
if (Configs.HealthChecksUri != null)
{
LogInfo("Sending healthchecks start.");
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("rid", PID.ToString()),
});
_ = client.PostAsync(Configs.HealthChecksUri, content).Result;
}
}
/// <summary>
/// Logs the runtime for the cron application.
/// </summary>
public void RunTime()
{
StopTime = DateTime.Now;
var runTime = StopTime - StartTime;
LogInfo("Total Run time: " + runTime.ToReadableString() + ".");
}
/// <summary>
/// Logs a message into the cron application.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="level">The priority level of the log. Default is <seealso cref="Level.Info"/></param>
/// <param name="identity">The identity of the application logging. Default is "cron_app".</param>
/// <param name="system">True if this should be logged to the system journal. Default is "true".</param>
public void Log(string message, Level level = Level.Info, bool system = true)
{
if (system)
Syslog.Write(message, level, Identity);
File.AppendAllText(Configs.Log, DateTime.Now.ToString("s") + "[" + level + "]: " + message + "\n");
}
/// <summary>
/// Logs a message as info just to the cron apps log file and not the system journal.
/// </summary>
/// <param name="message">The message to log.</param>
public void LogInfo(string message)
{
Log(message, level: Level.Info, system: false);
}
/// <summary>
/// Logs a message as debug. If debug is set to false (it is by default) it will log nothing.
/// </summary>
/// <param name="message">The message to log.</param>
public void Debug(string message)
{
if (Configs.Debug)
Log(message, Level.Debug, system: false);
}
protected bool ProcessIsRunning(int pid)
{
try
{
System.Diagnostics.Process.GetProcessById(pid);
return true;
} catch(ArgumentException)
{
return false;
}
}
protected bool ProcessIsRunning(string pid)
{
try
{
return ProcessIsRunning(int.Parse(pid));
} catch (ArgumentNullException)
{
Debug("The pid supplied was null.");
} catch (FormatException)
{
Debug("The pid supplied wasn't a number.");
} catch (OverflowException)
{
Debug("The pid provided was to large.");
}
return false;
}
protected void CreateLogFile()
{
try
{
File.AppendAllText(Configs.Log, DateTime.Now.ToString("s") + ": Log file created.");
Debug("Log file created successfuly.");
}
catch (Exception ex)
{
Debug("Could not create log file at '" + Configs.Log + "' with message: '" + ex.Message + "'.");
Log("Exiting with error. Could not create log file.", Level.Err);
Environment.Exit(2);
}
}
}
}
// Exit Codes
// 1: Log directory permission errors.
// 2: Log file permission errors.
// 3: PID directory permission errors.
// 4: PID file permission errors.
// 5: Process already running.