Created
May 15, 2018 20:17
-
-
Save achvaicer/fb0eb0bf794607e73f30c27a6c8aab05 to your computer and use it in GitHub Desktop.
RunnableBase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Leste.Robot.Base | |
{ | |
public enum Environment | |
{ | |
Development = 0x01, | |
Homologation = 0x02, | |
Production = 0x04, | |
/// <summary> | |
/// Includes Development | Homologation | Production | |
/// </summary> | |
All = 0x07 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Net.Mail; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Leste.Robot.Base | |
{ | |
internal class Mailer | |
{ | |
object _lock = new object(); | |
private readonly SmtpClient _smtpClient = new SmtpClient(ConfigurationManager.AppSettings["Email:SmtpServer"], int.Parse(ConfigurationManager.AppSettings["Email:Port"])); | |
private readonly string _from = ConfigurationManager.AppSettings["Email:From"]; | |
public Mailer() | |
{ | |
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; | |
_smtpClient.EnableSsl = true; | |
_smtpClient.Credentials = new System.Net.NetworkCredential(string.Format("{0}@leste.com", ConfigurationManager.AppSettings["Leste.Robot:User"]), ConfigurationManager.AppSettings["Leste.Robot:Password"]); | |
} | |
public void Send(IList<string> to, string subject, string body, bool isHtml = false, string[] attachments = null) | |
{ | |
var message = new MailMessage(_from, string.Join(",", to), subject, body); | |
message.IsBodyHtml = isHtml; | |
if (attachments != null && attachments.Any()) | |
foreach (var attach in attachments) | |
message.Attachments.Add(new Attachment(attach)); | |
lock (_lock) | |
_smtpClient.Send(message); | |
message.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using NLog; | |
namespace Leste.Robot.Base | |
{ | |
/// <summary> | |
/// Robô abstrato para servir de base para outros robôs herdarem | |
/// </summary> | |
[RunnableAt(Base.Environment.Production | Base.Environment.Development)] | |
public abstract class RobotRunnableBase : IRobotRunnable, IDisposable | |
{ | |
private readonly Logger _logger = LogManager.GetCurrentClassLogger(); | |
private readonly Mailer _mailer = new Mailer(); | |
private readonly string _robotName; | |
private readonly Type _robotType; | |
protected IList<string> EmailTos = ConfigurationManager.AppSettings["Email:To"].Split(new[] { ';' }).ToList(); | |
private string _logFilePattern = ConfigurationManager.AppSettings["Log:File"]; | |
protected string Environment = ConfigurationManager.AppSettings["Environment"]; | |
private string LogFile { get { return string.Format(_logFilePattern, DateTime.Today); } } | |
private string PrependClassName(string message) | |
{ | |
return string.Format("[{0}] {1}", _robotName, message); | |
} | |
public RobotRunnableBase() | |
{ | |
_robotType = this.GetType(); | |
_robotName = _robotType.Name; | |
_logger.Info("Iniciando robô {0}", _robotName); | |
} | |
public void RunIfEnabled() | |
{ | |
if (IsValidEnvironment()) | |
{ | |
Log("Robô será exacutado por estar em ambiente habilidado"); | |
Run(); | |
} | |
else | |
Log("Robô não será exacutado por não estar em ambiente habilidado"); | |
} | |
public virtual void Run() | |
{ | |
} | |
private bool IsValidEnvironment() | |
{ | |
var environments = _robotType.GetCustomAttributes(typeof(RunnableAtAttribute), true).OfType<RunnableAtAttribute>().FirstOrDefault()?.Environments; | |
if (!environments.HasValue) return false; | |
return environments.Value.HasFlag((Environment)Enum.Parse(typeof(Environment), this.Environment)); | |
} | |
public virtual void Log(string message, params object[] args) | |
{ | |
_logger.Info(PrependClassName(message), args); | |
} | |
public virtual void Error(string message, params object[] args) | |
{ | |
_logger.Error(PrependClassName(message), args); | |
} | |
public virtual void Error(string message, Exception exception) | |
{ | |
_logger.Error(exception, PrependClassName(message)); | |
} | |
public bool IsWeekend() | |
{ | |
return IsWeekend(DateTime.Today); | |
} | |
public bool IsWeekend(DateTime date) | |
{ | |
return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday; | |
} | |
protected void SendMail(string subject, string body, IList<string> To = null, bool isHtml = false, string[] attachments = null) | |
{ | |
var s = IsProduction() ? string.Format("[{0}] {1}", _robotName, subject) : string.Format("[{0}] [{1}] {2}", Environment, _robotName, subject); | |
_mailer.Send(IsDevelopment() ? new[] { string.Format("{0}@leste.com", System.Environment.UserName) } : To ?? EmailTos, s, body, isHtml, attachments); | |
} | |
public void Dispose() | |
{ | |
_logger.Info("Terminando robô {0}", _robotName); | |
} | |
public bool IsDevelopment() | |
{ | |
return Environment == "Development"; | |
} | |
public bool IsHomologation() | |
{ | |
return Environment == "Homologation"; | |
} | |
public bool IsProduction() | |
{ | |
return Environment == "Production"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Leste.Robot.Base | |
{ | |
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | |
public class RunnableAtAttribute : Attribute | |
{ | |
public Environment Environments { get; private set; } | |
public RunnableAtAttribute(Environment environments) | |
{ | |
Environments = environments; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment