ProcessBase2
Defines an abstract object that performs a runnable process.
using Relativity.DataExchange.Io;
using Relativity.DataExchange.Logger;
using Relativity.Logging;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace Relativity.DataExchange.Process
{
public abstract class ProcessBase2 : IRunnable, IDisposable
{
private readonly ProcessErrorWriter processErrorWriter;
private readonly ProcessEventWriter processEventWriter;
private bool disposed;
public ProcessContext Context { get; }
public Guid ProcessId { get; set; }
protected IAppSettings AppSettings { get; }
protected CancellationTokenSource CancellationTokenSource { get; }
protected IFileSystem FileSystem { get; }
protected ILog Logger { get; }
[Obsolete("This constructor is marked for deprecation. Please use the constructor that requires a logger instance.")]
protected ProcessBase2()
: this(RelativityLogger.Instance)
{
}
protected ProcessBase2(ILog logger)
: this(logger, null)
{
}
protected ProcessBase2(ILog logger, CancellationTokenSource tokenSource)
: this(Relativity.DataExchange.Io.FileSystem.Instance, Relativity.DataExchange.AppSettings.Instance, logger, tokenSource)
{
}
protected ProcessBase2(IFileSystem fileSystem, IAppSettings settings, ILog logger, CancellationTokenSource tokenSource)
{
AppSettings = settings.ThrowIfNull("settings");
CancellationTokenSource = (tokenSource ?? new CancellationTokenSource());
FileSystem = fileSystem.ThrowIfNull("fileSystem");
Logger = (((object)logger) ?? ((object)new NullLogger()));
processErrorWriter = new ProcessErrorWriter(fileSystem, logger);
processEventWriter = new ProcessEventWriter(fileSystem);
Context = new ProcessContext(processEventWriter, processErrorWriter, settings, logger);
ProcessId = Guid.NewGuid();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This was done for backwards compatibility reasons.")]
public void Start()
{
try {
Logger.LogInformation("The runnable process {ProcessType}-{ProcessId} has started.", new object[2] {
GetType(),
ProcessId
});
OnExecute();
Logger.LogInformation("The runnable process {ProcessType}-{ProcessId} successfully completed.", new object[2] {
GetType(),
ProcessId
});
} catch (Exception ex) {
Logger.LogError(ex, "The runnable process {ProcessType}-{ProcessId} experienced a fatal exception.", new object[2] {
GetType(),
ProcessId
});
Context.PublishFatalException(ex);
}
}
protected IIoReporter CreateIoReporter(IoReporterContext context)
{
return new IoReporter(context, Logger, CancellationTokenSource.Token);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed) {
if (disposing) {
if (processErrorWriter != null)
processErrorWriter.Dispose();
if (processEventWriter != null)
processEventWriter.Dispose();
}
disposed = true;
}
}
protected abstract void OnExecute();
}
}