Skip to content

Commit

Permalink
Fixes #1313 (#1316)
Browse files Browse the repository at this point in the history
* Fixes #1313

* Fixes #1295
  • Loading branch information
adamdriscoll authored Nov 12, 2019
1 parent 5987af0 commit fac1cec
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/UniversalDashboard.Server/DashboardManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace UniversalDashboard
class DashboardManager
{
private Server _server;
private readonly bool _dontSetExecutionPolicy;

public DashboardManager(bool dontSetExecutionPolicy)
{
_dontSetExecutionPolicy = dontSetExecutionPolicy;
}

public void Start()
{
Expand All @@ -24,7 +30,11 @@ public void Start()
{
var modulePath = Path.Combine(assemblyBasePath, "..\\UniversalDashboard.Community.psd1");

powerShell.AddStatement().AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process");
if (!_dontSetExecutionPolicy)
{
powerShell.AddStatement().AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process");
}

powerShell.AddStatement().AddScript($"Import-Module '{modulePath}'");
powerShell.AddStatement().AddScript($". '{dashboardScript}'");

Expand Down
7 changes: 5 additions & 2 deletions src/UniversalDashboard.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ namespace UniversalDashboard
class Program
{
private const string RunAsServiceFlag = "--run-as-service";
private const string NoExecutionPolicy = "--dont-set-execution-policy";

static void Main(string[] args)
{
var dontSetExecutionPolicy = args.Contains(NoExecutionPolicy);

if (args.Contains(RunAsServiceFlag))
{
var service = new UniversalDashboardService();
var service = new UniversalDashboardService(dontSetExecutionPolicy);
var serviceHost = new Win32ServiceHost(service);
serviceHost.Run();
}
else {
var dashboardManager = new DashboardManager();
var dashboardManager = new DashboardManager(dontSetExecutionPolicy);
dashboardManager.Start();
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/UniversalDashboard.Server/Win32Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ class UniversalDashboardService : IWin32Service
public string ServiceName => "Universal Dashboard";
private DashboardManager _dashboardManager;

private readonly bool _dontSetExecutionPolicy;

public UniversalDashboardService(bool dontSetExecutionPolicy)
{
_dontSetExecutionPolicy = dontSetExecutionPolicy;
}

public void Start(string[] startupArguments, ServiceStoppedCallback serviceStoppedCallback)
{
_dashboardManager = new DashboardManager();
_dashboardManager = new DashboardManager(_dontSetExecutionPolicy);
_dashboardManager.Start();
}

Expand Down
40 changes: 40 additions & 0 deletions src/UniversalDashboard.UITest/Integration/Sessions.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
param([Switch]$Release)

Import-Module "$PSScriptRoot\..\TestFramework.psm1" -Force
$ModulePath = Get-ModulePath -Release:$Release
$BrowserPort = Get-BrowserPort -Release:$Release

Import-Module $ModulePath -Force

Get-UDDashboard | Stop-UDDashboard

$Server = Start-UDDashboard -Port 10001 -Dashboard (New-UDDashboard -Title "Test" -Content {})
$Driver = Start-SeFirefox

Enable-UDLogging
Describe "Sessions" {

Context "Should remove sessions" {
$Dashboard = New-UDDashboard -Title "Test" -Content {

} -IdleTimeout ([TimeSpan]::FromSeconds(5))

$Server.DashboardService.SetDashboard($Dashboard)
Enter-SeUrl -Driver $Driver -Url "http://localhost:$BrowserPort"

Start-sleep 2

It "should timeout session" {

Stop-SeDriver $Driver

Start-Sleep 8

$Dashboard = Get-UDDashboard
$Dashboard.DashboardService.EndpointService.SessionManager.Sessions.Count | Should be 0
}
}
}

Stop-SeDriver $Driver
Stop-UDDashboard -Server $Server
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
var trigger = TriggerBuilder.Create()
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(60)
.WithIntervalInSeconds((int)_dashboardService.Dashboard.IdleTimeout.TotalSeconds)
.RepeatForever())
.Build();

Expand Down
14 changes: 12 additions & 2 deletions src/UniversalDashboard/Services/DashboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Runspaces;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using UniversalDashboard.Cmdlets;
using UniversalDashboard.Execution;
using UniversalDashboard.Interfaces;
Expand Down Expand Up @@ -40,9 +42,7 @@ public DashboardService(DashboardOptions dashboardOptions, string reloadToken) {
public DateTime StartTime { get; private set; }
public Debugger Debugger { get; private set; }
public ServiceProvider ServiceProvider { get; set; }

public IEndpointService EndpointService { get; }

public Dictionary<string, object> Properties { get; }

public void SetDashboard(Dashboard dashboard)
Expand Down Expand Up @@ -72,6 +72,16 @@ public void SetDashboard(Dashboard dashboard)
CmdletExtensions.HostState.EndpointService.Endpoints.Clear();
CmdletExtensions.HostState.EndpointService.RestEndpoints.Clear();
CmdletExtensions.HostState.EndpointService.ScheduledEndpoints.Clear();

if (ServiceProvider != null)
{
var manager = ServiceProvider.GetServices<IHostedService>().First(m => m is ScheduledEndpointManager) as ScheduledEndpointManager;

var source = new CancellationTokenSource();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
manager.StartAsync(source.Token);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
}
}

public void SetRestEndpoints(Endpoint[] endpoints)
Expand Down
3 changes: 3 additions & 0 deletions src/UniversalDashboard/Services/SessionManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NLog;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -17,6 +18,7 @@ public interface ISessionManager

public class SessionManager : ISessionManager
{
private readonly Logger logger = LogManager.GetLogger(nameof(SessionManager));
public ConcurrentDictionary<string, SessionState> Sessions { get; private set; }

public SessionManager()
Expand Down Expand Up @@ -71,6 +73,7 @@ public void ClearTimedOutSessions(TimeSpan timespan)

toRemove.ForEach(x => {
Sessions.TryRemove(x, out SessionState value);
logger.Debug($"Session {x} timed out. Last touched: {value.LastTouched}");
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/UniversalDashboard/Services/SessionTimeoutJob.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System;
using System.Threading.Tasks;
using NLog;
using Quartz;

namespace UniversalDashboard.Services
{
public class SessionTimeoutJob : IJob
{
private readonly Logger logger = LogManager.GetLogger(nameof(SessionTimeoutJob));

public TimeSpan IdleTimeout { get; set; }
public async Task Execute(IJobExecutionContext context)
{
await Task.CompletedTask;

logger.Debug($"Clearing out timed out sessions. IdleTimeout is: {IdleTimeout}");

foreach (var server in Server.Servers)
{
server.DashboardService.EndpointService.SessionManager.ClearTimedOutSessions(IdleTimeout);
Expand Down

0 comments on commit fac1cec

Please sign in to comment.