[tarantino] r159 committed - Upgrade logging around BatchJobs to Info, change emailing of errors to...

0 views
Skip to first unread message

codesite...@google.com

unread,
Mar 31, 2010, 6:16:03 PM3/31/10
to tarantino...@googlegroups.com
Revision: 159
Author: ajepst
Date: Wed Mar 31 15:15:37 2010
Log: Upgrade logging around BatchJobs to Info, change emailing of errors to
warn (batch job should still run) if email info not configured
http://code.google.com/p/tarantino/source/detail?r=159

Added:
/trunk/src/BatchJobs.Console/DebugerJobAgent.cs
/trunk/src/BatchJobs.Console/DebugerJobAgentFactory.cs
/trunk/src/BatchJobs.Console/LogFileToEmailSender.cs
Modified:
/trunk/src/BatchJobs.Console/BatchJobs.Console.csproj
/trunk/src/BatchJobs.Console/Program.cs
/trunk/src/BatchJobs.UnitTests/ProgramTester.cs

=======================================
--- /dev/null
+++ /trunk/src/BatchJobs.Console/DebugerJobAgent.cs Wed Mar 31 15:15:37 2010
@@ -0,0 +1,12 @@
+using BatchJobs.Core;
+
+namespace BatchJobs.Console
+{
+ public class DebugerJobAgent : IJobAgent
+ {
+ public void Execute()
+ {
+ System.Console.WriteLine("Executing");
+ }
+ }
+}
=======================================
--- /dev/null
+++ /trunk/src/BatchJobs.Console/DebugerJobAgentFactory.cs Wed Mar 31
15:15:37 2010
@@ -0,0 +1,19 @@
+using System.Collections.Generic;
+using BatchJobs.Core;
+
+namespace BatchJobs.Console
+{
+ public class DebugerJobAgentFactory : IJobAgentFactory
+ {
+ public IJobAgent Create(string name)
+ {
+ System.Console.WriteLine(name);
+ return new DebugerJobAgent();
+ }
+
+ public IEnumerable<string> GetInstanceNames()
+ {
+ return new string[] { "Foo", "Bar" };
+ }
+ }
+}
=======================================
--- /dev/null
+++ /trunk/src/BatchJobs.Console/LogFileToEmailSender.cs Wed Mar 31
15:15:37 2010
@@ -0,0 +1,69 @@
+using System;
+using System.Configuration;
+using System.IO;
+using System.Net.Mail;
+
+namespace BatchJobs.Console
+{
+ public class LogFileToEmailSender
+ {
+ private const string ToEmailKey = "BatchLogFileToEmail";
+ private const string FromEmailKey = "BatchLogFileFromEmail";
+ private const string SmtpHostKey = "BatchLogFileSmtpHost";
+ private const string FileLocationKey = "BatchLogFileLocation";
+
+ public Func<string> GetToEmail = () =>
ConfigurationManager.AppSettings[ToEmailKey];
+ public Func<string> GetFromEmail = () =>
ConfigurationManager.AppSettings[FromEmailKey];
+ public Func<string> GetSmtpHost = () =>
ConfigurationManager.AppSettings[SmtpHostKey];
+ public Func<string> GetFileLocation = () =>
ConfigurationManager.AppSettings[FileLocationKey];
+
+
+ public void Send(string args)
+ {
+ if (!IsConfiguredForEmail())
+ {
+ Logger.Warn(this, "Note: Emailing of errors has not been configured
for this job");
+ return;
+ }
+
+ var message = CreateMessage(args);
+ var client = new SmtpClient {Host = GetSmtpHost()};
+ client.Send(message);
+ }
+
+ private bool IsConfiguredForEmail()
+ {
+ var isConfigured = true;
+ if (string.IsNullOrEmpty(GetToEmail())) isConfigured = false;
+ if (string.IsNullOrEmpty(GetFromEmail())) isConfigured = false;
+ if (string.IsNullOrEmpty(GetSmtpHost())) isConfigured = false;
+ return isConfigured;
+ }
+
+ public MailMessage CreateMessage(string args)
+ {
+ var message = new MailMessage(GetFromEmail(), GetToEmail())
+ {
+ Subject = String.Format("[{0}] Error on Batch Job",
args),
+ Body = GetFileText()
+ };
+ return message;
+ }
+
+ public string GetFileText()
+ {
+ if (!string.IsNullOrEmpty(GetFileLocation()))
+ {
+ using (var fs = new FileStream(GetFileLocation(), FileMode.Open,
FileAccess.Read))
+ {
+ using (var sr = new StreamReader(fs))
+ {
+
+ return sr.ReadToEnd();
+ }
+ }
+ }
+ return "Log File Location Not Configured so Log File could not be
attached, Error on batch occurred";
+ }
+ }
+}
=======================================
--- /trunk/src/BatchJobs.Console/BatchJobs.Console.csproj Fri Feb 26
14:56:10 2010
+++ /trunk/src/BatchJobs.Console/BatchJobs.Console.csproj Wed Mar 31
15:15:37 2010
@@ -65,6 +65,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="DebugerJobAgent.cs" />
+ <Compile Include="DebugerJobAgentFactory.cs" />
+ <Compile Include="LogFileToEmailSender.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
=======================================
--- /trunk/src/BatchJobs.Console/Program.cs Mon Mar 1 14:48:26 2010
+++ /trunk/src/BatchJobs.Console/Program.cs Wed Mar 31 15:15:37 2010
@@ -1,9 +1,7 @@
using System;
-using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
-using System.Net.Mail;
using System.Reflection;
using BatchJobs.Core;

@@ -45,17 +43,17 @@
}
else
{
- Logger.Debug(this, string.Format("Command Line Specified Instance
Name: {0}", args[0]));
+ Logger.Info(this, string.Format("Command Line Specified Instance Name:
{0}", args[0]));
IJobAgent jobAgent = Factory().Create(args[0]);
- Logger.Debug(this, "Executing the Job");
+ Logger.Info(this, "Executing the Job");
try
{
jobAgent.Execute();
- Logger.Debug(this, string.Format("Job Execution Complete: {0}",
args[0]));
+ Logger.Info(this, string.Format("Job Execution Complete: {0}",
args[0]));
}
catch ( Exception e)
{
- Logger.Fatal(this, Logger.SerializeException(e));
+ Logger.Fatal(typeof(Program), string.Format("Failure in Job, bubbled
to Batch Runner: {0}", Logger.SerializeException(e)));
throw;
}
}
@@ -69,7 +67,7 @@

try
{
- Logger.Debug(this, string.Format("Parsing assembly and type names from
string \"{0}\"", unparsedTypename));
+ Logger.Info(this, string.Format("Parsing assembly and type names from
string \"{0}\"", unparsedTypename));
assemblyname = unparsedTypename.Split(',')[1].Trim();
typename = unparsedTypename.Split(',')[0].Trim();
}
@@ -82,7 +80,7 @@
Assembly a = null;
try
{
- Logger.Debug(this, string.Format("Loading Assembly {0}",
assemblyname));
+ Logger.Info(this, string.Format("Loading Assembly {0}", assemblyname));
a = Assembly.Load(assemblyname);
}
catch (FileNotFoundException e)
@@ -91,7 +89,7 @@
System.Console.WriteLine(e.Message);
}
Type classType = a.GetType(typename);
- Logger.Debug(this, string.Format("Creating instance of {0}",
classType));
+ Logger.Info(this, string.Format("Creating instance of {0}", classType));
if (classType.GetConstructors().Any( x => x.GetParameters().Length ==
1))
{
return (IJobAgentFactory)Activator.CreateInstance(classType, new
LoggerProxy());
@@ -100,71 +98,4 @@

}
}
-
- public class DebugerJobAgentFactory : IJobAgentFactory
- {
- public IJobAgent Create(string name)
- {
- System.Console.WriteLine(name);
- return new DebugerJobAgent();
- }
-
- public IEnumerable<string> GetInstanceNames()
- {
- return new string[] { "Foo", "Bar" };
- }
- }
-
- public class DebugerJobAgent : IJobAgent
- {
- public void Execute()
- {
- System.Console.WriteLine("Executing");
- }
- }
-
- public class LogFileToEmailSender
- {
- private const string ToEmailKey = "BatchLogFileToEmail";
- private const string FromEmailKey = "BatchLogFileFromEmail";
- private const string SmtpHostKey = "BatchLogFileSmtpHost";
- private const string FileLocationKey = "BatchLogFileLocation";
-
- public Func<string> GetToEmail = () =>
ConfigurationManager.AppSettings[ToEmailKey];
- public Func<string> GetFromEmail = () =>
ConfigurationManager.AppSettings[FromEmailKey];
- public Func<string> GetSmtpHost = () =>
ConfigurationManager.AppSettings[SmtpHostKey];
- public Func<string> GetFileLocation = () =>
ConfigurationManager.AppSettings[FileLocationKey];
-
-
- public void Send(string args)
- {
- var message = CreateMessage(args);
- var client = new SmtpClient { Host = GetSmtpHost() };
- client.Send(message);
- }
-
- public MailMessage CreateMessage(string args)
- {
- var message = new MailMessage(GetFromEmail(), GetToEmail())
- {
- Subject = String.Format("[{0}] Error on Batch Job",
args),
- Body = GetFileText()
- };
- return message;
- }
-
- public string GetFileText()
- {
- using (FileStream fs = new FileStream(GetFileLocation(), FileMode.Open,
FileAccess.Read))
- {
- using (StreamReader sr = new StreamReader(fs))
- {
-
- return sr.ReadToEnd();
- }
- }
- }
- }
-
-
-}
+}
=======================================
--- /trunk/src/BatchJobs.UnitTests/ProgramTester.cs Mon Mar 1 14:48:26 2010
+++ /trunk/src/BatchJobs.UnitTests/ProgramTester.cs Wed Mar 31 15:15:37 2010
@@ -35,36 +35,71 @@
public void Should_create_a_logging_factory()
{
var program = new Program();
- program.GetFactoryTypeName = () => typeof(FactoryLoggingStub).FullName
+ "," + GetType().Assembly.FullName;
+ program.GetFactoryTypeName = () => typeof(LoggingFactoryStub).FullName
+ "," + GetType().Assembly.FullName;
IJobAgentFactory factory = program.Factory();
- Assert.AreEqual("BatchJobs.UnitTests.FactoryLoggingStub",
factory.GetType().ToString());
+ Assert.AreEqual("BatchJobs.UnitTests.LoggingFactoryStub",
factory.GetType().ToString());

}
- }
-
- public class StubJob : IJobAgent
- {
- public bool Executed { get; set; }
-
-
- public void Execute()
- {
- Executed = true;
- }
+
+ [Test]
+ public void Should_rethrow_and_log_exceptions_from_job()
+ {
+ //Logger.EnsureInitialized(); //view sample error logging in output by
uncommenting
+ var agent = new ExceptionThrowingStubJob();
+ ExceptionThrowingFactoryStub.JobAgent = agent;
+ var program = new Program();
+ program.GetFactoryTypeName = () =>
typeof(ExceptionThrowingFactoryStub).FullName + "," +
GetType().Assembly.FullName;
+
+ try
+ {
+ program.Run(new[] {"foo"});
+ Assert.Fail("Should not have reached here-Program should have thrown");
+ }catch
+ {
+
+ }
+ }
}

- public class StubJobWithContructorParam : IJobAgent
- {
-
- public StubJobWithContructorParam(ILogger logger)
+ public class ExceptionThrowingStubJob : IJobAgent
+ {
+ public ExceptionThrowingStubJob()
+ {
+ }
+
+ public ExceptionThrowingStubJob(ILogger logger)
{
}
+
+ public bool Executed { get; set; }
+

public void Execute()
{
-
+ Executed = true;
+ throw new Exception("fake error occurred");
+ }
+ }
+
+ public class StubJob : IJobAgent
+ {
+ public bool Executed = false;
+ public StubJob()
+ {
+ }
+
+
+ public StubJob(ILogger logger)
+ {
+ }
+
+ public void Execute()
+ {
+ Executed = true;
}
}
+
+

public class FactoryStub : IJobAgentFactory
{
@@ -94,26 +129,45 @@
}
}

- public class FactoryLoggingStub : IJobAgentFactory
- {
- private readonly ILogger _logger;
-
- public FactoryLoggingStub(ILogger logger)
- {
- _logger = logger;
- }
-
- public FactoryLoggingStub()
+ public class LoggingFactoryStub : IJobAgentFactory
+ {
+ protected readonly ILogger _logger;
+
+ public LoggingFactoryStub()
{
if (JobAgent == null)
{
- JobAgent = new StubJobWithContructorParam(_logger);
+ JobAgent = new StubJob(_logger);
}
}

public static IJobAgent JobAgent { get; set; }

public static string Name { get; set; }
+
+
+ public IJobAgent Create(string name)
+ {
+ System.Console.WriteLine(name);
+ Name = name;
+ return JobAgent;
+ }
+
+ public IEnumerable<string> GetInstanceNames()
+ {
+ return new string[0];
+ }
+ }
+
+ public class ExceptionThrowingFactoryStub : IJobAgentFactory
+ {
+ protected readonly ILogger _logger;
+ public static IJobAgent JobAgent { get; set; }
+ public static string Name { get; set; }
+ public ExceptionThrowingFactoryStub()
+ {
+ JobAgent = new ExceptionThrowingStubJob(_logger);
+ }


public IJobAgent Create(string name)
Reply all
Reply to author
Forward
0 new messages