Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
Aggregated watcher hooks json config #11
Browse files Browse the repository at this point in the history
  • Loading branch information
spetz committed Sep 1, 2016
1 parent de8cf0a commit d2f5a82
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Examples/Warden.Spawn.Examples.Console/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,16 @@
"message": "Monitoring error(s) global hook async."
}
}
],
"aggregatedWatcherHooks": [
{
"type": "onCompletedAsync",
"condition": "invalidCheckResult",
"use": "sendGrid",
"configuration": {
"subject": "Monitoring async aggregated hook",
"message": "Monitoring error(s) aggregated hook async."
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public IWardenSpawnConfiguration Read(string configuration)
var hooks = ResolveHooks(wardenName, spawnConfiguration.hooks, integrations);
var globalWatcherHooks = ResolveGlobalWatcherHooks(wardenName, spawnConfiguration.globalWatcherHooks,
integrations);
var aggregatedWatcherHooks = ResolveAggregatedWatcherHooks(wardenName, spawnConfiguration.aggregatedWatcherHooks,
integrations);

return new WardenSpawnConfiguration(wardenName, watchers, integrations, hooks, globalWatcherHooks);
return new WardenSpawnConfiguration(wardenName, watchers, integrations, hooks,
globalWatcherHooks, aggregatedWatcherHooks);
}

private IEnumerable<IWatcherSpawnWithHooksConfiguration> ResolveWatchers(string wardenName,
Expand Down Expand Up @@ -112,6 +115,25 @@ private IEnumerable<IWatcherHookSpawnConfiguration> ResolveGlobalWatcherHooks(st
}
}

private IEnumerable<IWatcherHookSpawnConfiguration> ResolveAggregatedWatcherHooks(string wardenName,
dynamic hooks, IEnumerable<ISpawnIntegration> integrations)
{
if (hooks == null)
yield break;

var hooksText = JsonConvert.SerializeObject(hooks);
var hooksConfigurations =
JsonConvert.DeserializeObject<IEnumerable<WatcherHookSpawnConfiguration>>(hooksText)
as IEnumerable<WatcherHookSpawnConfiguration>;

foreach (var hookConfig in hooksConfigurations)
{
SetWatcherHookConfiguration(wardenName, hookConfig, integrations, hookType: "aggregatedWatcherHooks");

yield return hookConfig;
}
}

private void SetWatcherHookConfiguration(string wardenName,
IWatcherHookSpawnConfiguration hookConfig,
IEnumerable<ISpawnIntegration> integrations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ public class ConsoleSpawnIntegration : ISpawnIntegration
public string Name => "Console";
public IWatcherHooksResolver WatcherHooksResolver { get; }
public IWardenHooksResolver WardenHooksResolver { get; }
public IAggregatedWatcherHooksResolver AggregatedWatcherHooksResolver { get; }
public ISpawnIntegrationConfiguration Configuration => _configuration;

public ConsoleSpawnIntegration(ConsoleSpawnIntegrationConfiguration configuration)
{
_configuration = configuration;
WatcherHooksResolver = new ConsoleSpawnIntegrationWatcherHooksResolver(configuration);
WardenHooksResolver = new ConsoleSpawnIntegrationWardenHooksResolver(configuration);
AggregatedWatcherHooksResolver = new ConsoleSpawnIntegrationAggregatedWatcherHooksResolver(configuration);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Warden.Spawn.Hooks;
using Warden.Watchers;

namespace Warden.Spawn.Integrations.Console
{
public class ConsoleSpawnIntegrationAggregatedWatcherHooksResolver : IAggregatedWatcherHooksResolver
{
private readonly ConsoleSpawnIntegrationConfiguration _integrationConfiguration;

public ConsoleSpawnIntegrationAggregatedWatcherHooksResolver(ConsoleSpawnIntegrationConfiguration integrationConfiguration)
{
_integrationConfiguration = integrationConfiguration;
}

public Expression<Action<IEnumerable<IWatcherCheck>>> OnStart()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWatcherCheck>, Task>> OnStartAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnSuccess()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnSuccessAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFirstSuccess()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFirstSuccessAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFailure()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFailureAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnCompleted(object configuration)
{
var config = configuration as ConsoleSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();

var text = string.IsNullOrWhiteSpace(config.Text) ? _integrationConfiguration.DefaultText : config.Text;

return x => System.Console.WriteLine(text);
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnCompletedAsync(object configuration)
{
var config = configuration as ConsoleSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();

var text = string.IsNullOrWhiteSpace(config.Text) ? _integrationConfiguration.DefaultText : config.Text;

return x => Task.Factory.StartNew(() => System.Console.WriteLine(text));
}

public Expression<Action<IEnumerable<Exception>>> OnError()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<Exception>, Task>> OnErrorAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<Exception>>> OnFirstError()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<Exception>, Task>> OnFirstErrorAsync()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Warden.Integrations.SendGrid;
using Warden.Spawn.Hooks;
using Warden.Watchers;

namespace Warden.Spawn.Integrations.SendGrid
{
public class SendGridIntegrationAggregatedWatcherHooksResolver : IAggregatedWatcherHooksResolver
{
private readonly SendGridIntegration _integration;
private readonly SendGridSpawnIntegrationConfiguration _integrationConfiguration;

public SendGridIntegrationAggregatedWatcherHooksResolver(SendGridIntegration integration,
SendGridSpawnIntegrationConfiguration integrationConfiguration)
{
_integration = integration;
_integrationConfiguration = integrationConfiguration;
}

public Expression<Action<IEnumerable<IWatcherCheck>>> OnStart()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWatcherCheck>, Task>> OnStartAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnSuccess()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnSuccessAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFirstSuccess()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFirstSuccessAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnFailure()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnFailureAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<IWardenCheckResult>>> OnCompleted(object configuration)
{
var config = configuration as SendGridSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();

var subject = string.IsNullOrWhiteSpace(config.Subject)
? _integrationConfiguration.DefaultSubject
: config.Subject;
var message = string.IsNullOrWhiteSpace(config.Message)
? _integrationConfiguration.DefaultMessage
: config.Message;
var receivers = config.Receivers == null || !config.Receivers.Any()
? _integrationConfiguration.DefaultReceivers
: config.Receivers;

return x => Task.Factory.StartNew(() => _integration.SendEmailAsync(subject, message, receivers.ToArray()));
}

public Expression<Func<IEnumerable<IWardenCheckResult>, Task>> OnCompletedAsync(object configuration)
{
var config = configuration as SendGridSpawnIntegrationHooksConfiguration;
if (config == null)
throw new InvalidOperationException();

var subject = string.IsNullOrWhiteSpace(config.Subject)
? _integrationConfiguration.DefaultSubject
: config.Subject;
var message = string.IsNullOrWhiteSpace(config.Message)
? _integrationConfiguration.DefaultMessage
: config.Message;
var receivers = config.Receivers == null || !config.Receivers.Any()
? _integrationConfiguration.DefaultReceivers
: config.Receivers;

return x => _integration.SendEmailAsync(subject, message, receivers.ToArray());
}

public Expression<Action<IEnumerable<Exception>>> OnError()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<Exception>, Task>> OnErrorAsync()
{
throw new NotImplementedException();
}

public Expression<Action<IEnumerable<Exception>>> OnFirstError()
{
throw new NotImplementedException();
}

public Expression<Func<IEnumerable<Exception>, Task>> OnFirstErrorAsync()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SendGridSpawnIntegration : ISpawnIntegration
public string Name => "SendGrid";
public IWatcherHooksResolver WatcherHooksResolver { get; protected set; }
public IWardenHooksResolver WardenHooksResolver { get; }
public IAggregatedWatcherHooksResolver AggregatedWatcherHooksResolver { get; }
public ISpawnIntegrationConfiguration Configuration => _configuration;

public SendGridSpawnIntegration(SendGridSpawnIntegrationConfiguration configuration)
Expand All @@ -22,6 +23,8 @@ public SendGridSpawnIntegration(SendGridSpawnIntegrationConfiguration configurat
var integration = SendGridIntegration.Create(sendGridConfiguration);
WatcherHooksResolver = new SendGridIntegrationWatcherHooksResolver(integration, configuration);
WardenHooksResolver = new SendGridIntegrationWardenHooksResolver(integration, configuration);
AggregatedWatcherHooksResolver = new SendGridIntegrationAggregatedWatcherHooksResolver(integration,
configuration);
}
}
}
1 change: 1 addition & 0 deletions src/Warden.Spawn/Configurations/ISpawnIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface ISpawnIntegration
string Name { get; }
IWatcherHooksResolver WatcherHooksResolver { get; }
IWardenHooksResolver WardenHooksResolver { get; }
IAggregatedWatcherHooksResolver AggregatedWatcherHooksResolver { get; }
ISpawnIntegrationConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IWardenSpawnConfiguration : IConfiguration
IEnumerable<ISpawnIntegration> Integrations { get; }
IEnumerable<IWardenHookSpawnConfiguration> Hooks { get; }
IEnumerable<IWatcherHookSpawnConfiguration> GlobalWatcherHooks { get; }
IEnumerable<IWatcherHookSpawnConfiguration> AggregatedWatcherHooks { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface IWardenSpawnConfigurationInstance : IConfiguration
IEnumerable<IIntegration> Integrations { get; }
Action<WardenHooksConfiguration.Builder> Hooks { get; }
Action<WatcherHooksConfiguration.Builder> GlobalWatcherHooks { get; }
Action<AggregatedWatcherHooksConfiguration.Builder> AggregatedWatcherHooks { get; set; }
}
}
6 changes: 5 additions & 1 deletion src/Warden.Spawn/Configurations/WardenSpawnConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ public class WardenSpawnConfiguration : IWardenSpawnConfiguration
public IEnumerable<ISpawnIntegration> Integrations { get; protected set; }
public IEnumerable<IWardenHookSpawnConfiguration> Hooks { get; }
public IEnumerable<IWatcherHookSpawnConfiguration> GlobalWatcherHooks { get; }
public IEnumerable<IWatcherHookSpawnConfiguration> AggregatedWatcherHooks { get; }

public WardenSpawnConfiguration(
string wardenName,
IEnumerable<IWatcherSpawnWithHooksConfiguration> watchers,
IEnumerable<ISpawnIntegration> integrations,
IEnumerable<IWardenHookSpawnConfiguration> hooks,
IEnumerable<IWatcherHookSpawnConfiguration> globalWatcherHooks)
IEnumerable<IWatcherHookSpawnConfiguration> globalWatcherHooks,
IEnumerable<IWatcherHookSpawnConfiguration> aggregatedWatcherHooks)
{
WardenName = wardenName;
AggregatedWatcherHooks = aggregatedWatcherHooks;
Watchers = watchers ?? Enumerable.Empty<IWatcherSpawnWithHooksConfiguration>();
Integrations = integrations ?? Enumerable.Empty<ISpawnIntegration>();
Hooks = hooks ?? Enumerable.Empty<IWardenHookSpawnConfiguration>();
GlobalWatcherHooks = globalWatcherHooks ?? Enumerable.Empty<IWatcherHookSpawnConfiguration>();
AggregatedWatcherHooks = aggregatedWatcherHooks ?? Enumerable.Empty<IWatcherHookSpawnConfiguration>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ public class WardenSpawnConfigurationInstance : IWardenSpawnConfigurationInstanc
public IEnumerable<IIntegration> Integrations { get; protected set; }
public Action<WardenHooksConfiguration.Builder> Hooks { get; }
public Action<WatcherHooksConfiguration.Builder> GlobalWatcherHooks { get; }
public Action<AggregatedWatcherHooksConfiguration.Builder> AggregatedWatcherHooks { get; set; }

public WardenSpawnConfigurationInstance(
string wardenName,
IEnumerable<IWatcherWithHooks> watchers,
IEnumerable<IIntegration> integrations,
Action<WardenHooksConfiguration.Builder> hooks,
Action<WatcherHooksConfiguration.Builder> globalWatcherHooks)
Action<WatcherHooksConfiguration.Builder> globalWatcherHooks,
Action<AggregatedWatcherHooksConfiguration.Builder> aggregatedWatcherHooks)
{
WardenName = wardenName;
Watchers = watchers ?? Enumerable.Empty<IWatcherWithHooks>();
Integrations = integrations ?? Enumerable.Empty<IIntegration>();
Hooks = hooks;
GlobalWatcherHooks = globalWatcherHooks;
AggregatedWatcherHooks = aggregatedWatcherHooks;
}
}
}
Loading

0 comments on commit d2f5a82

Please sign in to comment.