Skip to content

Commit

Permalink
#645 Add Solr service type
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryKolinchuk committed Nov 26, 2021
1 parent a5a92ea commit e98a507
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
13 changes: 11 additions & 2 deletions src/SIM.Base/SolrState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ public class SolrState
[RenderInDataGreed]
public string Url { get; set; }

[RenderInDataGreed]
public CurrentType Type { get; set; }

public enum CurrentState
{
Running,
Stopped,
ServiceNotExist
Stopped
}

public enum CurrentType
{
Local,
Service,
Unknown
}
}
}
34 changes: 15 additions & 19 deletions src/SIM.Base/SolrStateResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ namespace SIM
{
public class SolrStateResolver
{
public virtual SolrState.CurrentState GetServiceState(string solrServiceName)
public virtual SolrState.CurrentState GetServiceState(ServiceControllerWrapper service)
{
ServiceControllerWrapper service = GetService(solrServiceName);

if (service == null)
if (service != null)
{
return SolrState.CurrentState.ServiceNotExist;
if (service.Status == ServiceControllerStatus.Running)
{
return SolrState.CurrentState.Running;
}
}

if (service.Status != ServiceControllerStatus.Running)
return SolrState.CurrentState.Stopped;
}

public virtual ServiceControllerWrapper GetService(string solrServiceName)
{
ServiceController serviceController = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == solrServiceName);
if (serviceController == null)
{
return SolrState.CurrentState.Stopped;
return null;
}

return SolrState.CurrentState.Running;
return new ServiceControllerWrapper(serviceController);
}

public virtual SolrState.CurrentState GetUrlState(string solrUrl)
Expand Down Expand Up @@ -68,17 +75,6 @@ public virtual string GetVersion(string solrUrl)

return string.Empty;
}

public virtual ServiceControllerWrapper GetService(string serviceName)
{
ServiceController service = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == serviceName);
if (service == null)
{
return null;
}

return new ServiceControllerWrapper(service);
}
}

public class ServiceControllerWrapper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using NSubstitute;
using SIM.Sitecore9Installer.Tasks;
using SIM.Sitecore9Installer.Validation;
Expand All @@ -21,7 +22,10 @@ public void ServiceIsStopped(IEnumerable<Task> tasks)

SolrServiceValidator val = Substitute.ForPartsOf<SolrServiceValidator>();
SolrStateResolver solrStateResolver = Substitute.ForPartsOf<SolrStateResolver>();
solrStateResolver.GetServiceState(Arg.Any<string>()).ReturnsForAnyArgs(SolrState.CurrentState.Stopped);
ServiceController serviceController = Substitute.For<ServiceController>();
ServiceControllerWrapper serviceControllerWrapper = Substitute.For<ServiceControllerWrapper>(serviceController);
solrStateResolver.GetService(Arg.Any<string>()).ReturnsForAnyArgs(serviceControllerWrapper);
solrStateResolver.GetServiceState(Arg.Any<ServiceControllerWrapper>()).ReturnsForAnyArgs(SolrState.CurrentState.Stopped);
val.SolrStateResolver.Returns(solrStateResolver);
IEnumerable<ValidationResult> res = val.Evaluate(tasks);
Assert.Equal(2, res.Count(r => r.State == ValidatorState.Error));
Expand All @@ -38,7 +42,10 @@ public void ServiceIsRunning(IEnumerable<Task> tasks)

SolrServiceValidator val = Substitute.ForPartsOf<SolrServiceValidator>();
SolrStateResolver solrStateResolver = Substitute.ForPartsOf<SolrStateResolver>();
solrStateResolver.GetServiceState(Arg.Any<string>()).ReturnsForAnyArgs(SolrState.CurrentState.Running);
ServiceController serviceController = Substitute.For<ServiceController>();
ServiceControllerWrapper serviceControllerWrapper = Substitute.For<ServiceControllerWrapper>(serviceController);
solrStateResolver.GetService(Arg.Any<string>()).ReturnsForAnyArgs(serviceControllerWrapper);
solrStateResolver.GetServiceState(Arg.Any<ServiceControllerWrapper>()).ReturnsForAnyArgs(SolrState.CurrentState.Running);
val.SolrStateResolver.Returns(solrStateResolver);
IEnumerable<ValidationResult> res = val.Evaluate(tasks);
Assert.Equal(0, res.Count(r => r.State == ValidatorState.Error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ public class SolrServiceValidator : BaseValidator
{
foreach (InstallParam param in paramsToValidate)
{
if (SolrStateResolver.GetServiceState(param.Value) == SolrState.CurrentState.Stopped)
ServiceControllerWrapper serviceControllerWrapper = SolrStateResolver.GetService(param.Value);
if (serviceControllerWrapper == null)
{
yield return new ValidationResult(ValidatorState.Success,
$"The '{param.Value}' service defined in the '{task.Name}' installation task does not exist, so the validation has been skipped.", null);

}
else if (SolrStateResolver.GetServiceState(serviceControllerWrapper) == SolrState.CurrentState.Stopped)
{
yield return new ValidationResult(ValidatorState.Error,
$"The '{param.Value}' service required for the '{task.Name}' installation task is not running.", null);
Expand Down
23 changes: 16 additions & 7 deletions src/SIM.Tool.Windows/Dialogs/GridEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,16 @@ private List<SolrState> GetSolrStates()
SolrState solrState = new SolrState();
solrState.Name = solrDefinition.Name;
solrState.Url = solrDefinition.Url;
solrState.State = solrStateResolver.GetServiceState(solrDefinition.Service);
solrState.State = solrStateResolver.GetServiceState(solrStateResolver.GetService(solrDefinition.Service));
if (solrState.State == SolrState.CurrentState.Running)
{
solrState.Version = solrStateResolver.GetVersion(solrDefinition.Url);
solrState.Type = SolrState.CurrentType.Service;
}
else
{
solrState.Version = "N/A";
solrState.Type = SolrState.CurrentType.Unknown;
}
solrStates.Add(solrState);
}
Expand All @@ -189,6 +191,7 @@ private List<SolrState> GetSolrStates()
if (solrState.State == SolrState.CurrentState.Running)
{
solrState.Version = solrStateResolver.GetVersion(solrState.Url);
solrState.Type = SolrState.CurrentType.Local;
}
}
}
Expand Down Expand Up @@ -222,13 +225,19 @@ private void UpdateDataGridRowColor()
if (row != null)
{
SolrState.CurrentState state = (this.DataGrid.Items[i] as SolrState).State;
if (state == SolrState.CurrentState.Running)
switch (state)
{
row.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#ccffcc");
}
else if (state == SolrState.CurrentState.Stopped || state == SolrState.CurrentState.ServiceNotExist)
{
row.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#f2f2f2");
case SolrState.CurrentState.Running:
{
row.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#ccffcc");
break;
}
case SolrState.CurrentState.Stopped:
{
row.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#f2f2f2");
break;
}
default: break;
}
}
}
Expand Down

0 comments on commit e98a507

Please sign in to comment.