Warden

Piotr Gankiewicz edited this page Sep 18, 2016 · 16 revisions

Warden instance is responsible for invoking its specific hooks and all of the configured watchers in an iteration - a single cycle during which all of the watchers are being executed, defined hooks will be invoked etc. Warden is defined via the following interface:

public interface IWarden
{
    string Name { get; }
    Task StartAsync();
    Task PauseAsync();
    Task StopAsync();
}
  • Name - allows to identify which Warden has performed specific tasks. Might be useful for collecting the custom data or when using the Web Panel. The default name has the following format: @Warden {HOSTNAME}.
  • StartAsync() - starts the Warden which includes e.g. running iterations in a loop (infinite by default, but it can be configured - both the interval which is 5 seconds by default and the total number of iterations).
  • PauseAsync() - pauses the Warden, until it is resumed again by StartAsync(). Does not reset the current iteration number (ordinal).
  • StopAsync() - stops the Warden, until it is resumed again by StartAsync(). Does reset the current iteration number (ordinal) back to 1.

Installation:

Available as a NuGet package.

Install-Package Warden

Configuration:

Warden can be configured to accept the hooks (callbacks) both for the Warden and watchers events (such as completed iteration or when the particular watcher returns an error). To learn more about hooks, please navigate to the hooks page. The WardenConfiguration exposes the following methods:

  • AddWatcher() - add watcher instance that will be executed once the Warden is runing.
  • SetHooks() - configure the hooks specific for the Warden.
  • SetGlobalWatcherHooks() - configure the common hooks specific for the watchers.
  • WithInterval() - set the interval of TimeSpan type between the watcher checks within the iteration.
  • WithMinimalInterval() - set the minimal TimeSpan value for interval which is 1 ms.
  • SetIterationsCount() - if set to the specific number N, it will run only the N number iterations.
  • RunOnlyOnce() - run only single iteration.
  • WithConsoleLogger() - use the built-in Console Logger.
  • SetLogger() - allows to provide a custom Logger.
  • SetIterationProcessor() - allows to provide a custom IIterationProcessor.
  • SetCustomDateTimeProvider() - allows to configure a custom provider for the DateTime (UTC by default).

Similar to the watchers, Warden can be configured by passing the WardenConfiguration instance or using the lambda expression.

The example configuration that makes use of two watchers (Web and MSSQL) and has set a lot of the useful hooks is presented below:

var wardenConfiguration = WardenConfiguration
    .Create()
    .SetHooks(hooks =>
    {
        hooks.OnError(exception => Logger.Error(exception));
        hooks.OnIterationCompleted(iteration => OnIterationCompleted(iteration));
    })
    .AddWatcher(mssqlWatcher)
    .AddWatcher(websiteWatcher, hooks =>
    {
        hooks.OnStartAsync(check => WebsiteHookOnStartAsync(check));
        hooks.OnFailureAsync(result => WebsiteHookOnFailureAsync(result));
        hooks.OnSuccessAsync(result => WebsiteHookOnSuccessAsync(result));
        hooks.OnCompletedAsync(result => WebsiteHookOnCompletedAsync(result));
    }, interval: TimeSpan.FromSeconds(3))
    .SetGlobalWatcherHooks(hooks =>
    {
        hooks.OnStart(check => GlobalHookOnStart(check));
        hooks.OnFailure(result => GlobalHookOnFailure(result));
        hooks.OnSuccess(result => GlobalHookOnSuccess(result));
        hooks.OnCompleted(result => GlobalHookOnCompleted(result));
        hooks.OnError(exception => Logger.Error(exception));
    })
    .WithInterval(TimeSpan.FromMinutes(1))
    .WithConsoleLogger(minLevel: WardenLoggerLevel.Info, useColors: true)
    .Build();   

And the configuration via the lambda expression may look like this:

var warden = WardenInstance.Create(cfg =>
{
    cfg.AddWatcher(websiteWatcher)
       .AddWatcher(mssqlWatcher);
});

Once the Warden has been configured, just start it, and it will do the rest.

await warden.StartAsync();

Please note that the Warden will never throw an exception. In order to get notified about the Warden specific exceptions, use the OnError() hook.

Iteration processor:

public interface IIterationProcessor
{
    Task<IWardenIteration> ExecuteAsync(long ordinal);
}

IIterationProcessor is responsible for executing all of the configured watchers and theirs hooks in a cycle called the iteration. It is possible to provide the custom IIterationProcessor by passing its instance to the SetIterationProcessor() method during the Warden configuration. The IWardenIteration has been described in the hooks page at the very bottom under the Consuming the Warden hooks data section.