Skip to content

Commit

Permalink
#50
Browse files Browse the repository at this point in the history
*Wrote  implementation of maintanance mode
* made print status on webpage
  • Loading branch information
Yaroslav Husynin committed Mar 23, 2020
1 parent 5981fe2 commit 295daee
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
using Elfo.Wardein.Core.ServiceManager;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Elfo.Wardein.APIs
Expand Down Expand Up @@ -35,8 +32,22 @@ public async Task<string> GetServiceStatus(HttpContext context)
{
string serviceName = context.GetRouteValue("name").ToString();

var result = $"{serviceName} is not running";

var status = await GetServiceStatus(serviceName);
return status; // TODO: print on webpage
if (status == "Running")
{
result = $"{serviceName} is running";
}
return context.Response.WriteAsync(result).ToString();
}

public Task GetMaintenanceModeStatus(HttpContext context)
{
var result = "Wardein is not in maintenance mode";
if (ServicesContainer.WardeinConfigurationManager().IsInMaintenanceMode)
result = "Wardein is in maintenance mode";
return context.Response.WriteAsync(result);
}

public Task StartService(HttpContext context)
Expand Down Expand Up @@ -75,9 +86,15 @@ public Task StopPool(HttpContext context)
public async Task<string> GetPoolStatus(HttpContext context)
{
string applicationPoolName = context.GetRouteValue("name").ToString();


var result = $"{applicationPoolName} is not running";

var status = await GetIISPoolStatus(applicationPoolName);
return status; // TODO: print in webpage
if (status == "Started")
{
result = $"{applicationPoolName} is running";
}
return context.Response.WriteAsync(result).ToString();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,134 @@ public class OracleWardeinConfigurationManager : IAmWardeinConfigurationManager
private readonly OracleConnectionConfiguration configuration;
private readonly IOracleHelper oracleHelper;
private readonly string hostname;
private WardeinConfig cachedWardeinConfig;

public OracleWardeinConfigurationManager(IOracleHelper oracleHelper, string hostname)
{
this.oracleHelper = oracleHelper;
this.hostname = hostname;
}

public bool IsInMaintenanceMode => throw new NotImplementedException();

public WardeinConfig GetConfiguration()
public bool IsInMaintenanceMode
{
// TODO: Add dynamic Appl hostname and check if it's possible ot remove deplendency towards managed data access
var parameters = new Dictionary<string, object>
get
{
["APPL_HOSTNAME"] = new OracleParameter("APPL_HOSTNAME", OracleDbType.Varchar2).Value = hostname
};
var query = @"SELECT * FROM V_WRD_WATCHERS WHERE ""ApplicationHostname"" = :APPL_HOSTNAME";
var waredinWatcherConfigs = this.oracleHelper.Query<WardeinConfigurationModel>(query, parameters);
if (!GetMaintenanceModeValue())
return false;

// TODO: Test to see if it works.. code will probably need refactoring
var wardeinConfig = JObject.Parse(waredinWatcherConfigs.FirstOrDefault().WardeinConfig);
foreach (var wardeinWatcherConfig in waredinWatcherConfigs)
{
var watcherTypeConfig = JObject.Parse((string)wardeinWatcherConfig.WatcherTypeJsonConfig);
var watcherConfig = JObject.Parse((string)wardeinWatcherConfig.WatcherJsonConfig);
watcherConfig.AddDefaultProps(wardeinWatcherConfig.WatcherType, wardeinWatcherConfig.WatcherConfigurationId, wardeinWatcherConfig.ApplicationId);
watcherTypeConfig.Merge(watcherConfig);
wardeinConfig.Merge(watcherTypeConfig);
if (IsMaintenanceModeTimeoutExpired())
StopMaintenaceMode();

return GetMaintenanceModeValue();

#region Local Functions

bool GetMaintenanceModeValue() => GetConfiguration().MaintenanceModeStatus?.IsInMaintenanceMode ?? false;

bool IsMaintenanceModeTimeoutExpired()
{
return GetExpirationDate() <= DateTime.UtcNow;

#region Local Functions

DateTime GetExpirationDate() =>
GetConfiguration().MaintenanceModeStatus.MaintenanceModeStartDateInUTC.AddSeconds(GetConfiguration().MaintenanceModeStatus.DurationInSeconds);

#endregion
}

#endregion
}
}

// TODO: Cahe the config
return wardeinConfig.ToObject<WardeinConfig>();
public WardeinConfig GetConfiguration()
{
if (cachedWardeinConfig == null)
{
// TODO: Add dynamic Appl hostname and check if it's possible ot remove deplendency towards managed data access
var parameters = new Dictionary<string, object>
{
["APPL_HOSTNAME"] = new OracleParameter("APPL_HOSTNAME", OracleDbType.Varchar2).Value = hostname
};
var query = @"SELECT * FROM V_WRD_WATCHERS WHERE ""ApplicationHostname"" = :APPL_HOSTNAME";
var waredinWatcherConfigs = this.oracleHelper.Query<WardeinConfigurationModel>(query, parameters);

// TODO: Test to see if it works.. code will probably need refactoring
var wardeinConfig = JObject.Parse(waredinWatcherConfigs.FirstOrDefault().WardeinConfig);
foreach (var wardeinWatcherConfig in waredinWatcherConfigs)
{
var watcherTypeConfig = JObject.Parse((string)wardeinWatcherConfig.WatcherTypeJsonConfig);
var watcherConfig = JObject.Parse((string)wardeinWatcherConfig.WatcherJsonConfig);
watcherConfig.AddDefaultProps(wardeinWatcherConfig.WatcherType, wardeinWatcherConfig.WatcherConfigurationId, wardeinWatcherConfig.ApplicationId);
watcherTypeConfig.Merge(watcherConfig);
wardeinConfig.Merge(watcherTypeConfig);
}

// TODO: Cahe the config
return cachedWardeinConfig = wardeinConfig.ToObject<WardeinConfig>();
}

return cachedWardeinConfig;
}

public void InvalidateCache()
{
throw new NotImplementedException();
cachedWardeinConfig = null;
}

public void StartMaintenanceMode(double durationInSeconds)
{
throw new NotImplementedException();
ToggleAndPersistMaintenanceModeStatus(startmaintenanceMode: true, durationInSeconds: durationInSeconds);
}

public void StopMaintenaceMode()
{
throw new NotImplementedException();
var parameters = new Dictionary<string, object>
{
["DT_MNTNC_END"] = new OracleParameter("DT_MNTNC_END", OracleDbType.Date).Value = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(1)),
["APPL_HOSTNAME"] = new OracleParameter("APPL_HOSTNAME", OracleDbType.Varchar2).Value = hostname
};
var query = @"UPDATE WRD_CNFG SET DT_MNTNC_END = :DT_MNTNC_END WHERE APPL_HOSTNAME =:APPL_HOSTNAME";
oracleHelper.ExecuteAsync(query, parameters);
ToggleAndPersistMaintenanceModeStatus(startmaintenanceMode: false);
}

private void ToggleAndPersistMaintenanceModeStatus(bool startmaintenanceMode, double? durationInSeconds = null)
{
if (GetConfiguration().MaintenanceModeStatus == null)
GetConfiguration().MaintenanceModeStatus = new MaintenanceModeStatus();

SetStatus();
SetDurationInSecondIfNeccessary();
SetStartDateIfNeccessary();

#region Local Functions

void SetStatus()
{
GetConfiguration().MaintenanceModeStatus.IsInMaintenanceMode = startmaintenanceMode;
}

void SetDurationInSecondIfNeccessary()
{
if (durationInSeconds.HasValue)
GetConfiguration().MaintenanceModeStatus.DurationInSeconds = durationInSeconds.Value;
}

void SetStartDateIfNeccessary()
{
if (startmaintenanceMode)
{
var parameters = new Dictionary<string, object>
{
["DT_MNTNC_END"] = new OracleParameter("DT_MNTNC_END", OracleDbType.Date).Value = DateTime.UtcNow.AddSeconds((double)durationInSeconds),
["APPL_HOSTNAME"] = new OracleParameter("APPL_HOSTNAME", OracleDbType.Varchar2).Value = hostname
};
var query = @"UPDATE WRD_CNFG SET DT_MNTNC_END = :DT_MNTNC_END WHERE APPL_HOSTNAME =:APPL_HOSTNAME";
oracleHelper.ExecuteAsync(query, parameters);
}
}
#endregion
}
}

Expand Down

0 comments on commit 295daee

Please sign in to comment.