-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMonitoring.cs
More file actions
1228 lines (1046 loc) · 45.7 KB
/
Copy pathMonitoring.cs
File metadata and controls
1228 lines (1046 loc) · 45.7 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Cronos;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.Json;
namespace RunCommandsService
{
#region Options & DTOs
public class MonitoringOptions
{
public bool EnableHttpEndpoint { get; set; } = true;
public List<string> HttpPrefixes { get; set; } = new() { "http://localhost:5058/" };
/// <summary>Maximum JSON request size accepted by write and cron-preview APIs.</summary>
public int MaxRequestBodyBytes { get; set; } = 64 * 1024;
public AlertThresholds AlertOn { get; set; } = new();
public NotifiersOptions Notifiers { get; set; } = new();
public DashboardOptions Dashboard { get; set; } = new();
public ExecutionHistoryOptions ExecutionHistory { get; set; } = new();
// Admin key for administrative APIs
public string? AdminKey { get; set; }
}
public class DashboardOptions
{
public bool Enabled { get; set; } = true;
public string Title { get; set; } = "Scheduled Command Executor";
public int AutoRefreshSeconds { get; set; } = 5;
public bool ShowRawJsonToggle { get; set; } = true;
public string HtmlPath { get; set; } = "dashboard.html"; // external HTML file
}
public class AlertThresholds
{
public int ConsecutiveFailures { get; set; } = 2;
public int SlowRunMs { get; set; } = 120000;
// Backcompat with older config key name
public int ExecutionTimeMsThreshold
{
get => SlowRunMs;
set => SlowRunMs = value;
}
public bool EmailOnFail { get; set; } = true;
public bool EmailOnConsecutiveFailures { get; set; } = true;
}
public class NotifiersOptions
{
public EmailOptions Email { get; set; } = new();
public WebhookOptions Webhook { get; set; } = new();
}
public class EmailOptions
{
public bool Enabled { get; set; } = false;
public string From { get; set; } = string.Empty;
public List<string> To { get; set; } = new();
public string SmtpHost { get; set; } = string.Empty;
public int SmtpPort { get; set; } = 587;
public bool UseSsl { get; set; } = true;
public string User { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string SubjectTemplate
{
get;
set;
} = "[${AlertType}] ${CommandId} (${ConsecutiveFailures}x) — ${DurationMs}ms";
public string BodyTemplate
{
get;
set;
} =
@"Command: ${Command}
Started: ${StartUtc:o}
Ended: ${EndUtc:o}
ExitCode: ${ExitCode}
Duration: ${DurationMs}ms
Error: ${Error}
Attempts: ${AttemptCount}/${MaxAttempts}
Message: ${CustomMessage}";
}
public class ExecutionEvent
{
public string CommandId { get; set; } = string.Empty;
public string Command { get; set; } = string.Empty;
public DateTime StartUtc { get; set; }
public DateTime EndUtc { get; set; }
public int? ExitCode { get; set; }
public bool Success { get; set; }
public bool SkippedDueToConflict { get; set; }
public string? Error { get; set; }
public int DurationMs { get; set; }
public int AttemptCount { get; set; } = 1;
public int MaxAttempts { get; set; } = 1;
public bool RetryExhausted { get; set; }
public bool TimedOut { get; set; }
public string TriggerSource { get; set; } = "scheduled";
}
public class ScheduledCommandView
{
public string Id { get; set; } = string.Empty;
public string Command { get; set; } = string.Empty;
public string CronExpression { get; set; } = string.Empty;
public string TimeZone { get; set; } = "UTC";
public bool Enabled { get; set; }
public bool AllowParallelRuns { get; set; }
public string ConcurrencyKey { get; set; } = string.Empty;
public int? MaxRuntimeMinutes { get; set; }
public RetryOptions Retry { get; set; } = new();
public string? NextRunUtc { get; set; }
public string? CustomAlertMessage { get; set; }
}
public record CronPreviewReq(string Cron, string TimeZone);
public class ScheduledCommandPayload
{
public string Id { get; set; } = string.Empty;
public string Command { get; set; } = string.Empty;
public string CronExpression { get; set; } = string.Empty;
public string TimeZone { get; set; } = "UTC";
public bool Enabled { get; set; }
public bool AllowParallelRuns { get; set; }
public string? ConcurrencyKey { get; set; }
public int? MaxRuntimeMinutes { get; set; }
public RetryOptions Retry { get; set; } = new();
public string? NextRunUtc { get; set; }
public string? NextRunLocal { get; set; }
public string? CustomAlertMessage { get; set; }
}
public sealed class ConfigurationImportRequest
{
public string Mode { get; set; } = "replace";
public List<ScheduledCommand> ScheduledCommands { get; set; } = new();
}
#endregion
#region Notifiers
public interface IAlertNotifier
{
Task NotifyAsync(string subject, string message, CancellationToken ct = default);
}
public class CompositeNotifier : IAlertNotifier
{
private readonly List<IAlertNotifier> _notifiers = new();
public CompositeNotifier(IEnumerable<IAlertNotifier> notifiers) => _notifiers.AddRange(notifiers);
public Task NotifyAsync(string subject, string message, CancellationToken ct = default) => Task.WhenAll(
_notifiers.Select(n => SafeNotify(n, subject, message, ct)));
private static async Task SafeNotify(IAlertNotifier n, string s, string m, CancellationToken ct)
{
try
{
await n.NotifyAsync(s, m, ct);
} catch
{
}
}
}
public class EmailNotifier : IAlertNotifier
{
private readonly EmailOptions _opt;
public EmailNotifier(EmailOptions opt) { _opt = opt; }
public Task NotifyAsync(string subject, string message, CancellationToken ct = default)
{
if(!_opt.Enabled || string.IsNullOrWhiteSpace(_opt.SmtpHost) || _opt.To == null || _opt.To.Count == 0)
return Task.CompletedTask;
using var client = new SmtpClient(_opt.SmtpHost, _opt.SmtpPort) { EnableSsl = _opt.UseSsl };
if(!string.IsNullOrEmpty(_opt.User))
client.Credentials = new NetworkCredential(_opt.User, _opt.Password);
foreach(var to in _opt.To)
{
using var mail = new MailMessage(_opt.From, to, subject, message);
client.Send(mail);
}
return Task.CompletedTask;
}
}
#endregion
#region Execution Monitor
public class ExecutionMonitor
{
private readonly MonitoringOptions _options = new();
private readonly IAlertNotifier _notifier;
private readonly ILogger<ExecutionMonitor> _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly ExecutionHistoryStore _historyStore;
private readonly ConcurrentQueue<ExecutionEvent> _events = new();
private readonly ConcurrentDictionary<string, int> _consecutiveFailures = new();
private readonly object _scheduleLock = new();
private List<ScheduledCommandView> _scheduleSnapshot = new();
private Func<object>? _schedulerHealthProvider;
public ExecutionMonitor(
IOptions<MonitoringOptions> options,
ILogger<ExecutionMonitor> logger,
ILoggerFactory loggerFactory,
ExecutionHistoryStore historyStore)
{
_options = options.Value ?? new MonitoringOptions();
_logger = logger;
_loggerFactory = loggerFactory;
_historyStore = historyStore;
var notifiers = new List<IAlertNotifier> { new EmailNotifier(_options.Notifiers.Email) };
try
{
if(_options?.Notifiers?.Webhook != null)
{
var whLogger = _loggerFactory.CreateLogger<WebhookNotifier>();
notifiers.Add(new WebhookNotifier(Microsoft.Extensions.Options.Options.Create(_options.Notifiers.Webhook), whLogger));
}
}
catch { /* ignore wiring errors */ }
_notifier = new CompositeNotifier(notifiers);
foreach (var execution in _historyStore.GetRecent(5000).Reverse())
{
_events.Enqueue(execution);
ApplyConsecutiveFailure(execution);
}
}
public void UpdateScheduleSnapshot(IEnumerable<ScheduledCommandView> snapshot)
{
lock(_scheduleLock)
{
_scheduleSnapshot = snapshot?.ToList() ?? new List<ScheduledCommandView>();
}
}
public void SetSchedulerHealthProvider(Func<object> healthProvider)
{
_schedulerHealthProvider = healthProvider;
}
public void Record(ExecutionEvent ev)
{
ev.DurationMs = (int)(ev.EndUtc - ev.StartUtc).TotalMilliseconds;
_events.Enqueue(ev);
while(_events.Count > 5000 && _events.TryDequeue(out _))
{
} // cap memory
_historyStore.Append(ev);
// update consecutive failures
var failureCount = ApplyConsecutiveFailure(ev);
if (ev.SkippedDueToConflict)
return;
if (!ev.Success)
{
if(_options.AlertOn.EmailOnFail)
FireAlert("Failure", ev, failureCount);
if(_options.AlertOn.EmailOnConsecutiveFailures && failureCount >= _options.AlertOn.ConsecutiveFailures)
FireAlert($"Consecutive failures ({failureCount})", ev, failureCount);
}
// slow run?
if(ev.DurationMs >= _options.AlertOn.SlowRunMs && _options.AlertOn.SlowRunMs > 0)
{
FireAlert("Slow run", ev, _consecutiveFailures.GetValueOrDefault(ev.CommandId, 0));
}
}
private int ApplyConsecutiveFailure(ExecutionEvent ev)
{
if (ev.SkippedDueToConflict)
return _consecutiveFailures.GetValueOrDefault(ev.CommandId, 0);
if (ev.Success)
{
_consecutiveFailures[ev.CommandId] = 0;
return 0;
}
return _consecutiveFailures.AddOrUpdate(ev.CommandId, 1, (_, value) => value + 1);
}
private void FireAlert(string alertType, ExecutionEvent ev, int consecutiveFailCount)
{
try
{
var schedule = _scheduleSnapshot.FirstOrDefault(s => s.Id == ev.CommandId);
string subject = _options.Notifiers.Email.SubjectTemplate
.Replace("${AlertType}", alertType)
.Replace("${CommandId}", ev.CommandId)
.Replace("${ConsecutiveFailures}", consecutiveFailCount.ToString())
.Replace("${DurationMs}", ev.DurationMs.ToString());
string body = _options.Notifiers.Email.BodyTemplate
.Replace("${Command}", ev.Command ?? string.Empty)
.Replace("${StartUtc}", ev.StartUtc.ToString("o"))
.Replace("${EndUtc}", ev.EndUtc.ToString("o"))
.Replace("${ExitCode}", ev.ExitCode?.ToString() ?? "null")
.Replace("${DurationMs}", ev.DurationMs.ToString())
.Replace("${AttemptCount}", ev.AttemptCount.ToString())
.Replace("${MaxAttempts}", ev.MaxAttempts.ToString())
.Replace("${Error}", ev.Error ?? string.Empty)
.Replace("${CustomMessage}", schedule?.CustomAlertMessage ?? string.Empty);
_ = _notifier.NotifyAsync(subject, body);
} catch(Exception ex)
{
_logger.LogError(ex, "Error while sending alert");
}
}
public object GetHealthPayload()
{
var recent = _events.Reverse().Take(100).ToList(); // newest first
List<ScheduledCommandView> schedule;
lock(_scheduleLock)
schedule = _scheduleSnapshot.ToList();
// Build a concrete, serializer-friendly list
var scheduledForPayload = new List<ScheduledCommandPayload>(schedule.Count);
foreach(var s in schedule)
{
string? nextLocal = null;
try
{
if(!string.IsNullOrWhiteSpace(s.NextRunUtc) &&
DateTime.TryParse(
s.NextRunUtc,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
out var nextUtc))
{
var tzId = string.IsNullOrWhiteSpace(s.TimeZone) ? "UTC" : s.TimeZone;
var tz = TimeZoneHelper.FindTimeZone(tzId);
var local = TimeZoneInfo.ConvertTimeFromUtc(DateTime.SpecifyKind(nextUtc, DateTimeKind.Utc), tz);
nextLocal = $"{local:yyyy-MM-dd HH:mm:ss} ({tz.Id})";
}
} catch
{
// leave nextLocal = null on any failure
}
scheduledForPayload.Add(
new ScheduledCommandPayload
{
Id = s.Id,
Command = s.Command,
CronExpression = s.CronExpression,
TimeZone = s.TimeZone,
Enabled = s.Enabled,
AllowParallelRuns = s.AllowParallelRuns,
ConcurrencyKey = s.ConcurrencyKey,
MaxRuntimeMinutes = s.MaxRuntimeMinutes,
Retry = s.Retry,
NextRunUtc = s.NextRunUtc,
NextRunLocal = nextLocal,
CustomAlertMessage = s.CustomAlertMessage
});
}
return new
{
version = GetProductVersion(),
nowUtc = DateTime.UtcNow.ToString("o"),
recentCount = recent.Count,
recent,
metrics = _historyStore.GetMetrics(),
history = new { enabled = _historyStore.Enabled },
consecutiveFailures = _consecutiveFailures.ToDictionary(kv => kv.Key, kv => kv.Value),
scheduled = scheduledForPayload,
// Scheduler health status
schedulerHealth = _schedulerHealthProvider?.Invoke(),
// NEW: simple UI hints for the dashboard
ui = new
{
showRawJsonToggle = _options?.Dashboard?.ShowRawJsonToggle == true
}
};
}
private static string GetProductVersion()
{
try
{
var asm = typeof(Monitoring).Assembly;
var info = asm.GetCustomAttributes(
typeof(System.Reflection.AssemblyInformationalVersionAttribute),
false)
.OfType<System.Reflection.AssemblyInformationalVersionAttribute>()
.FirstOrDefault()?.InformationalVersion;
if(!string.IsNullOrWhiteSpace(info))
return info;
// Fall back to AssemblyVersion
return asm.GetName().Version?.ToString() ?? "unknown";
} catch
{
return "unknown";
}
}
}
#endregion
#region HTTP Monitoring Host
public class Monitoring : IHostedService
{
private readonly IConfiguration _configuration;
private readonly IOptions<MonitoringOptions> _options;
private readonly ExecutionMonitor _monitor;
private readonly ILogger<Monitoring> _logger;
private readonly IManualJobRunner _manualJobRunner;
private readonly ExecutionHistoryStore _historyStore;
private HttpListener? _listener;
private string _dashboardPath = string.Empty;
private FileSystemWatcher? _htmlWatcher;
private static readonly object ConfigWriteLock = new();
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
public Monitoring(
IConfiguration configuration,
IOptions<MonitoringOptions> options,
ExecutionMonitor monitor,
IManualJobRunner manualJobRunner,
ExecutionHistoryStore historyStore,
ILogger<Monitoring> logger)
{
_configuration = configuration;
_options = options;
_monitor = monitor;
_manualJobRunner = manualJobRunner;
_historyStore = historyStore;
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
if (!_options.Value.EnableHttpEndpoint)
return Task.CompletedTask;
_listener = new HttpListener();
var prefixes = (_options.Value.HttpPrefixes ?? new List<string>())
.Where(p => !string.IsNullOrWhiteSpace(p))
.Select(p => p.Trim())
.Select(p => p.EndsWith("/") ? p : p + "/")
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
if (prefixes.Count == 0)
prefixes.Add("http://localhost:5058/");
// ✅ Use the normalized list
foreach (var p in prefixes)
_listener.Prefixes.Add(p);
try
{
_listener.Start();
_ = AcceptLoop(cancellationToken);
_logger.LogInformation("Health HTTP endpoint listening on {Prefixes}", string.Join(", ", prefixes));
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to start HTTP endpoint");
}
_dashboardPath = ResolveHtmlPath(_options.Value.Dashboard.HtmlPath);
SetupHtmlWatcher(_dashboardPath);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
try
{
_htmlWatcher?.Dispose();
_listener?.Stop();
_listener?.Close();
} catch
{
}
return Task.CompletedTask;
}
private async Task AcceptLoop(CancellationToken ct)
{
try
{
while(!ct.IsCancellationRequested && _listener != null && _listener.IsListening)
{
var ctx = await _listener.GetContextAsync();
_ = Task.Run(() => Handle(ctx), ct);
}
} catch(Exception ex)
{
if(!ct.IsCancellationRequested)
_logger.LogError(ex, "HTTP accept loop error");
}
}
// -------------- SYNCHRONOUS HANDLER (NO await HERE) ----------------
private void Handle(HttpListenerContext ctx)
{
try
{
ApplySecurityHeaders(ctx.Response);
string path = ctx.Request.Url?.AbsolutePath ?? string.Empty;
var pathLower = path.ToLowerInvariant();
if(path == "/" || path == "/dashboard")
{
ServeDashboard(ctx);
} else if(pathLower == "/api/health")
{
var payload = _monitor.GetHealthPayload();
WriteJson(ctx, payload, 200);
} else if(pathLower == "/api/logs")
{
ServeLogsTail(ctx);
} else if(pathLower == "/api/history" && ctx.Request.HttpMethod == "GET")
{
ServeHistory(ctx);
} else if(pathLower == "/api/config/export" && ctx.Request.HttpMethod == "GET")
{
if (!RequireAuthorization(ctx))
return;
ExportConfiguration(ctx);
} else if(pathLower == "/api/config/import" && ctx.Request.HttpMethod == "POST")
{
if (!RequireAuthorization(ctx))
return;
var body = ReadBody(ctx);
var request = JsonSerializer.Deserialize<ConfigurationImportRequest>(body, JsonOptions)
?? throw new JsonException("Body must contain an import object.");
ImportConfiguration(ctx, request);
} else if(TryGetManualRunJobId(path, out var manualJobId) && ctx.Request.HttpMethod == "POST")
{
if (!RequireAuthorization(ctx))
return;
QueueManualRun(ctx, manualJobId);
} else if(pathLower == "/api/jobs" && ctx.Request.HttpMethod == "GET")
{
var jobs = ReadJobsRaw();
WriteJson(ctx, new { ok = true, jobs }, 200);
} else if((pathLower == "/api/jobs/validatecron" || pathLower == "/api/jobs/validatecron/" || pathLower == "/api/jobs/validatecron") && ctx.Request.HttpMethod == "POST"
|| (path == "/api/jobs/validateCron" && ctx.Request.HttpMethod == "POST"))
{
var body = ReadBody(ctx);
var dto = JsonSerializer.Deserialize<CronPreviewReq>(body)
?? throw new JsonException("Body must contain cron and timeZone.");
ValidateCron(ctx, dto);
} else if(pathLower == "/api/jobs" && ctx.Request.HttpMethod == "POST")
{
if(!RequireAuthorization(ctx))
return;
var body = ReadBody(ctx);
var job = JsonSerializer.Deserialize<Dictionary<string, object?>>(body)
?? throw new JsonException("Body must contain a job object.");
CreateJob(ctx, job);
} else if(pathLower.StartsWith("/api/jobs/") &&
(ctx.Request.HttpMethod == "PUT" || ctx.Request.HttpMethod == "DELETE"))
{
if(!RequireAuthorization(ctx))
return;
var id = path.Split('/').Last();
if(ctx.Request.HttpMethod == "DELETE")
{
DeleteJob(ctx, id);
} else
{
var body = ReadBody(ctx);
var job = JsonSerializer.Deserialize<Dictionary<string, object?>>(body)
?? throw new JsonException("Body must contain a job object.");
UpdateJob(ctx, id, job);
}
} else
{
ctx.Response.StatusCode = 404;
ctx.Response.Close();
}
} catch(RequestBodyTooLargeException ex)
{
WriteJson(ctx, new { ok = false, error = ex.Message }, 413);
} catch(UnsupportedMediaTypeException ex)
{
WriteJson(ctx, new { ok = false, error = ex.Message }, 415);
} catch(JsonException ex)
{
WriteJson(ctx, new { ok = false, error = $"Invalid JSON: {ex.Message}" }, 400);
} catch(Exception ex)
{
try
{
_logger.LogError(ex, "HTTP handler error");
ctx.Response.StatusCode = 500;
var b = Encoding.UTF8.GetBytes("Internal error");
ctx.Response.OutputStream.Write(b, 0, b.Length);
ctx.Response.Close();
} catch
{
}
}
}
private static void ApplySecurityHeaders(HttpListenerResponse response)
{
response.Headers["X-Content-Type-Options"] = "nosniff";
response.Headers["X-Frame-Options"] = "DENY";
response.Headers["Referrer-Policy"] = "no-referrer";
response.Headers["Cache-Control"] = "no-store";
response.Headers["Content-Security-Policy"] =
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; " +
"img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'";
}
#endregion
#region Dashboard & Logs (sync)
private void ServeDashboard(HttpListenerContext ctx)
{
try
{
if (!_options.Value.Dashboard.Enabled)
{
ctx.Response.StatusCode = 404;
ctx.Response.Close();
return;
}
string html;
try
{
if (File.Exists(_dashboardPath))
html = File.ReadAllText(_dashboardPath, Encoding.UTF8);
else
html = "<!doctype html><meta charset=\"utf-8\"><title>Dashboard</title><h1>Dashboard file not found</h1>";
}
catch
{
html = "<!doctype html><meta charset=\"utf-8\"><title>Dashboard</title><h1>Error reading dashboard.html</h1>";
}
html = (html ?? string.Empty)
.Replace("{{TITLE}}", _options.Value.Dashboard.Title ?? "Scheduled Command Executor")
.replaceInsensitive("{{AUTO_REFRESH_SECONDS}}", (_options.Value.Dashboard.AutoRefreshSeconds > 0 ? _options.Value.Dashboard.AutoRefreshSeconds : 5).ToString())
.Replace(
"{{RAW_TOGGLE}}",
_options.Value.Dashboard.ShowRawJsonToggle
? "<label class=\"pill\"><input id=\"toggleRaw\" type=\"checkbox\" checked> Show raw JSON</label>"
: string.Empty);
var bytes = Encoding.UTF8.GetBytes(html);
ctx.Response.ContentType = "text/html; charset=utf-8";
ctx.Response.StatusCode = 200;
ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
ctx.Response.Close();
}
catch (Exception ex)
{
try
{
_logger.LogError(ex, "Error serving dashboard");
ctx.Response.StatusCode = 500;
var b = Encoding.UTF8.GetBytes("<h1>Internal error</h1>");
ctx.Response.ContentType = "text/html; charset=utf-8";
ctx.Response.OutputStream.Write(b, 0, b.Length);
ctx.Response.Close();
}
catch { /* swallow */ }
}
}
private void ServeLogsTail(HttpListenerContext ctx)
{
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
var candidates = new List<string>();
// Daily FileLogger (log_*.txt) in base
candidates.AddRange(SafeGetFiles(baseDir, "log_*.txt"));
var logDir = Path.Combine(baseDir, "log");
var logsDir = Path.Combine(baseDir, "logs");
var LogsDir = Path.Combine(baseDir, "Logs");
if(Directory.Exists(logDir))
candidates.AddRange(SafeGetFiles(logDir, "*.txt"));
if(Directory.Exists(logsDir))
candidates.AddRange(SafeGetFiles(logsDir, "*.txt"));
if(Directory.Exists(LogsDir))
candidates.AddRange(SafeGetFiles(LogsDir, "*.txt"));
if(candidates.Count == 0)
{
var msg = Encoding.UTF8.GetBytes("(no logs found in base/log/logs)");
ctx.Response.ContentType = "text/plain; charset=utf-8";
ctx.Response.StatusCode = 200;
ctx.Response.OutputStream.Write(msg, 0, msg.Length);
ctx.Response.Close();
return;
}
var latest = candidates.Select(f => new FileInfo(f)).OrderByDescending(fi => fi.LastWriteTimeUtc).First()
.FullName;
int tailKb = 128;
int.TryParse(ctx.Request.QueryString["tailKb"], out tailKb);
if(tailKb <= 0 || tailKb > 4096)
tailKb = 128;
byte[] buf;
using(var fs = new FileStream(latest, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
long read = Math.Min(fs.Length, tailKb * 1024L);
buf = new byte[read];
fs.Seek(-read, SeekOrigin.End);
fs.ReadExactly(buf);
}
ctx.Response.ContentType = "text/plain; charset=utf-8";
ctx.Response.StatusCode = 200;
ctx.Response.OutputStream.Write(buf, 0, buf.Length);
ctx.Response.Close();
}
#endregion
#region Job Builder helpers (sync)
private void ValidateCron(HttpListenerContext ctx, CronPreviewReq dto)
{
try
{
if(dto == null || string.IsNullOrWhiteSpace(dto.Cron))
{
WriteJson(ctx, new { ok = false, error = "Missing 'cron' field." }, 400);
return;
}
CronExpression cron;
try
{
cron = CronExpression.Parse(dto.Cron);
} catch(Exception ex)
{
WriteJson(ctx, new { ok = false, error = $"Invalid cron: {ex.Message}" }, 400);
return;
}
var tzId = string.IsNullOrWhiteSpace(dto.TimeZone) ? "UTC" : dto.TimeZone;
TimeZoneInfo tz = TimeZoneHelper.FindTimeZone(tzId);
var results = new List<string>(5);
var cursorUtc = DateTime.UtcNow;
for(int i = 0; i < 5; i++)
{
// Cronos expects UTC base time when a time zone is provided
var nextUtc = cron.GetNextOccurrence(DateTime.SpecifyKind(cursorUtc, DateTimeKind.Utc), tz);
if(nextUtc == null)
break;
results.Add(nextUtc.Value.ToString("o"));
cursorUtc = nextUtc.Value.AddSeconds(1);
}
WriteJson(ctx, new { ok = true, timeZone = tz.Id, next = results }, 200);
} catch(Exception ex)
{
WriteJson(ctx, new { ok = false, error = ex.Message }, 400);
}
}
private static DateTime ConvertLocalToUtc(DateTime localUnspec, TimeZoneInfo tz)
{
if(tz.IsInvalidTime(localUnspec))
localUnspec = localUnspec.AddHours(1);
return TimeZoneInfo.ConvertTimeToUtc(localUnspec, tz);
}
private bool IsAuthorized(HttpListenerContext ctx)
{
var expected = _options.Value.AdminKey ?? _configuration["Monitoring:AdminKey"];
if (SecretMasker.IsDefaultSecret(expected))
{
_logger.LogWarning("API administrative action rejected: Monitoring:AdminKey is using a default or unconfigured secret value. Please set a strong random key in configuration.");
return false;
}
var got = ctx.Request.Headers["X-Admin-Key"];
return SecretMasker.FixedTimeEquals(expected, got);
}
private bool RequireAuthorization(HttpListenerContext ctx)
{
if (IsAuthorized(ctx))
return true;
WriteJson(ctx, new { ok = false, error = "Unauthorized" }, 401);
return false;
}
private static bool TryGetManualRunJobId(string path, out string jobId)
{
var segments = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
if (segments.Length == 4 &&
string.Equals(segments[0], "api", StringComparison.OrdinalIgnoreCase) &&
string.Equals(segments[1], "jobs", StringComparison.OrdinalIgnoreCase) &&
string.Equals(segments[3], "run", StringComparison.OrdinalIgnoreCase))
{
jobId = Uri.UnescapeDataString(segments[2]);
return !string.IsNullOrWhiteSpace(jobId);
}
jobId = string.Empty;
return false;
}
private void QueueManualRun(HttpListenerContext ctx, string jobId)
{
var result = _manualJobRunner.QueueManualRun(jobId);
if (!result.Accepted)
{
WriteJson(ctx, new { ok = false, error = result.Error },
string.Equals(result.Error, "Job not found.", StringComparison.Ordinal) ? 404 : 409);
return;
}
WriteJson(ctx, new { ok = true, accepted = true, jobId, trigger = "manual" }, 202);
}
private void ServeHistory(HttpListenerContext ctx)
{
var jobId = ctx.Request.QueryString["jobId"];
var requestedLimit = int.TryParse(ctx.Request.QueryString["limit"], out var parsed) ? parsed : 100;
var limit = Math.Clamp(requestedLimit, 1, 5000);
var events = _historyStore.GetRecent(limit, string.IsNullOrWhiteSpace(jobId) ? null : jobId);
WriteJson(ctx, new { ok = true, count = events.Count, events }, 200);
}
private void ExportConfiguration(HttpListenerContext ctx)
{
var jobs = ReadJobsRaw();
WriteJson(ctx, new
{
schemaVersion = 1,
exportedAtUtc = DateTime.UtcNow.ToString("O"),
scheduledCommands = jobs
}, 200);
}
private void ImportConfiguration(HttpListenerContext ctx, ConfigurationImportRequest request)
{
var mode = request.Mode?.Trim().ToLowerInvariant() ?? "replace";
if (mode is not ("replace" or "merge"))
{
WriteJson(ctx, new { ok = false, error = "Mode must be 'replace' or 'merge'." }, 400);
return;
}
var incoming = request.ScheduledCommands ?? new List<ScheduledCommand>();
if (incoming.Any(job => job == null))
{
WriteJson(ctx, new { ok = false, error = "ScheduledCommands cannot contain null entries." }, 400);
return;
}
List<ScheduledCommand> finalJobs;
lock (ConfigWriteLock)
{
if (mode == "merge")
{
finalJobs = ReadJobsRaw()
.Select(job => JsonSerializer.Deserialize<ScheduledCommand>(JsonSerializer.Serialize(job), JsonOptions))
.Where(job => job != null)
.Cast<ScheduledCommand>()
.ToList();
foreach (var job in incoming)
{
var index = finalJobs.FindIndex(existing =>
string.Equals(existing.Id, job.Id, StringComparison.OrdinalIgnoreCase));
if (index >= 0)
finalJobs[index] = job;
else
finalJobs.Add(job);
}
}
else
{
finalJobs = incoming;
}
var defaultTimeZone = _configuration["Scheduler:DefaultTimeZone"] ?? "UTC";
var report = ConfigValidator.Validate(finalJobs, defaultTimeZone);
if (!report.AllValid)
{
WriteJson(ctx, new
{
ok = false,
error = "Imported jobs failed validation.",
problems = report.Jobs.Where(job => !job.IsValid)
}, 400);
return;
}
var rawJobs = finalJobs.Select(job =>
JsonSerializer.Deserialize<Dictionary<string, object?>>(JsonSerializer.Serialize(job))
?? throw new JsonException("Unable to serialize imported job."))
.ToList();
WriteJobsRaw(rawJobs);
}
WriteJson(ctx, new { ok = true, mode, imported = incoming.Count, total = finalJobs.Count }, 200);
}
private static string ConfigPath() => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
private static List<Dictionary<string, object?>> ReadJobsRaw()
{
var json = File.ReadAllText(ConfigPath(), Encoding.UTF8);
using var doc = JsonDocument.Parse(json);