NTP Analyzer  0.8.2
Analyze the operation of time servers
NotifyJob.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Globalization;
25 using System.Net;
26 using System.Net.Mail;
27 using System.Text;
28 using Ntp.Analyzer.Config.Node;
30 using Ntp.Common.Log;
31 using Ntp.Common.Process;
32 
33 namespace Ntp.Analyzer.Process.Description
34 {
35  public sealed class NotifyJob : JobDescription, IDisposable
36  {
37  public NotifyJob(
38  NotifyConfiguration config,
39  LogBase log,
40  int pid,
41  string configFile
42  )
43  : base(config, log)
44  {
45  this.config = config;
46  this.pid = pid;
47  this.configFile = configFile;
48  jobs = new List<Job>();
49 
50  client = new SmtpClient(config.SmptServer, config.SmtpPort);
51 
52  if (config.SmtpUser != null)
53  {
54  client.Credentials = new NetworkCredential(config.SmtpUser, config.SmtpPass ?? string.Empty);
55  }
56 
57  client.EnableSsl = config.EnableSsl;
58  }
59 
60  private readonly SmtpClient client;
61  private readonly NotifyConfiguration config;
62  private readonly string configFile;
63  private readonly List<Job> jobs;
64  private readonly int pid;
65 
66  public override ThreadType ThreadType => ThreadType.MultiThreaded;
67 
68  public override string JobType => "Notification mail";
69 
70  public override int Priority => 99;
71 
72  protected override void InternalExecute()
73  {
74  var message = new MailMessage(
75  config.Sender,
76  config.Mail,
77  config.Subject,
78  BuildContent()
79  );
80 
81  client.Send(message);
82  }
83 
88  internal void SetJobList(IEnumerable<Job> items)
89  {
90  jobs.AddRange(items);
91  }
92 
97  private string BuildContent()
98  {
99  var builder = new StringBuilder();
100  builder.AppendLine("List of running items in NTP Analyzer");
101  builder.Append(Billboard.Jobs(jobs));
102  builder.Append("Using configuration: ");
103  builder.AppendLine(configFile);
104  builder.Append("Running with PID: ");
105  builder.AppendLine(pid.ToString(CultureInfo.InvariantCulture));
106  return builder.ToString();
107  }
108 
109  #region IDisposable Support
110 
111  private void Dispose(bool disposing)
112  {
113  if (disposing)
114  {
115  client.Dispose();
116  }
117  }
118 
120  {
121  Dispose(false);
122  }
123 
124  public void Dispose()
125  {
126  Dispose(true);
127  GC.SuppressFinalize(this);
128  }
129 
130  #endregion
131  }
132 }
readonly NotifyConfiguration config
Definition: NotifyJob.cs:61
string BuildContent()
Builds the message content.
Definition: NotifyJob.cs:97
static string Jobs(IEnumerable< Job > jobs)
Definition: Billboard.cs:33
NotifyJob(NotifyConfiguration config, LogBase log, int pid, string configFile)
Definition: NotifyJob.cs:37
Base class for jobs following the GoF Command Pattern.
override void InternalExecute()
Implementing method for descendants.
Definition: NotifyJob.cs:72
void SetJobList(IEnumerable< Job > items)
Sets the job list to be monitored.
Definition: NotifyJob.cs:88