Remotely/Server/Services/DesktopHubSessionCleanup.cs
Jared Goodwin 3ef4cdf81a
Extract remote control functionality into separate library. (#539)
* Convert server to new single-file startup model.

* Add remote control implementations.

* Implement IViewerAuthorizer.

* Update hub endpoints.

* Implement HubEventHandler.

* Implement ViewerHubDataProvider.

* Implement page data provider.

* Implement RCL and refactor.

* Update submodule.

* Replace submodule with NuGet.

* Update copy URL.

* Update NuGet.

* Remove deprecated WebRTC.

* Remove deprecated WebRTC.

* Update Immense.RemoteControl

* Building out desktop projects.

* Bring more services into submodule.

* Update submodule.

* Update submodule.

* Refactoring for module.

* Update submodule.

* Update submodule

* Got Windows desktop app running.

* Refactor for submodule changes.

* FIx unattended session start.

* Switch desktop app out of console mode.

* Fix tests.

* Update publishing.

* Remove ClickOnce middleware.

* Remove ClickOnce remnants.

* Update submodule

* Add some logging.

* Update Linux path.

* Update submodule.

* Add cleanup service for unattended sessions that failed to start.

* Update submodule.

* Fix chat.

* Add ValidateExecutableReferencesMatchSelfContained property.

* Add other submodule projects.  Align checkbox.

* Update submodule.  Reduce deserialization in the browser, resulting in faster renders.

* Update submodule.

* Update submodule.

* Update submodule.

* Update submodule.

* Add orgId back for branding.

* Get branding loading in desktop apps.

* Update submodule.

* Create log dir.

* Refactor version check on config page.

* Update submodule.

* Update submodule.

* Change submodule URL.

* Correct namespace.

* Update submodule.

* Checkout submodules recursively.
2022-12-23 06:39:12 -08:00

62 lines
2.1 KiB
C#

using Immense.RemoteControl.Server.Services;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Linq;
using Timer = System.Timers.Timer;
using Immense.RemoteControl.Shared.Services;
using Microsoft.Build.Framework;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Collections.Generic;
using Immense.RemoteControl.Server.Models;
namespace Remotely.Server.Services
{
public class DesktopHubSessionCleanup : BackgroundService
{
private readonly IDesktopHubSessionCache _sessionCache;
private readonly ISystemTime _systemTime;
private readonly ILogger<DesktopHubSessionCleanup> _logger;
private Task _cleanupTask;
private CancellationToken _stoppingToken;
public DesktopHubSessionCleanup(
IDesktopHubSessionCache sessionCache,
ISystemTime systemTime,
ILogger<DesktopHubSessionCleanup> logger)
{
_sessionCache = sessionCache;
_systemTime = systemTime;
_logger = logger;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
_stoppingToken = stoppingToken;
_cleanupTask = Task.Run(CleanupSessions, stoppingToken);
return Task.CompletedTask;
}
private async Task CleanupSessions()
{
while (!_stoppingToken.IsCancellationRequested)
{
foreach (var session in _sessionCache.Sessions)
{
if (session.Value.Mode == RemoteControlMode.Unattended &&
!session.Value.ViewerList.Any() &&
session.Value.Created < _systemTime.Now.AddMinutes(-1))
{
_logger.LogWarning("Removing expired session: {session}", JsonSerializer.Serialize(session.Value));
_sessionCache.Sessions.Remove(session.Key, out _);
}
}
await Task.Delay(30_000, _stoppingToken);
}
}
}
}