2using System.Collections.Generic;
5using System.Threading.Tasks;
7using Microsoft.EntityFrameworkCore;
8using Microsoft.Extensions.Hosting;
9using Microsoft.Extensions.Logging;
10using Microsoft.Extensions.Options;
106 readonly ILogger<InstanceManager>
logger;
111 readonly Dictionary<long, ReferenceCountingContainer<IInstance, InstanceWrapper>>
instances;
194 IOptions<GeneralConfiguration> generalConfigurationOptions,
195 IOptions<SwarmConfiguration> swarmConfigurationOptions,
196 ILogger<InstanceManager>
logger)
208 this.console =
console ??
throw new ArgumentNullException(nameof(
console));
210 generalConfiguration = generalConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(generalConfigurationOptions));
211 swarmConfiguration = swarmConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(swarmConfigurationOptions));
212 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
216 instances =
new Dictionary<long, ReferenceCountingContainer<IInstance, InstanceWrapper>>();
218 readyTcs =
new TaskCompletionSource();
235 await instanceKvp.Value.Instance.DisposeAsync();
241 logger.LogInformation(
"Server shutdown");
247 ArgumentNullException.ThrowIfNull(metadata);
251 if (!
instances.TryGetValue(metadata.Require(x => x.Id), out var instance))
254 return instance.AddReference();
259 public async ValueTask
MoveInstance(Models.Instance instance,
string oldPath, CancellationToken cancellationToken)
261 ArgumentNullException.ThrowIfNull(oldPath);
265 if (instanceReferenceCheck !=
null)
266 throw new InvalidOperationException(
"Cannot move an online instance!");
267 var newPath = instance.Path!;
280 "Error moving instance {instanceId}!",
284 logger.LogDebug(
"Reverting instance {instanceId}'s path to {oldPath} in the DB...", instance.Id, oldPath);
293 db.Instances.Attach(targetInstance);
294 targetInstance.Path = oldPath;
295 return db.Save(CancellationToken.None);
302 "Error reverting database after failing to move instance {instanceId}! Attempting to detach...",
311 CancellationToken.None);
317 "Okay, what gamma radiation are you under? Failed to write instance attach file!");
319 throw new AggregateException(tripleEx, innerEx, ex);
322 throw new AggregateException(ex, innerEx);
330 public async ValueTask
OfflineInstance(Models.Instance metadata,
User user, CancellationToken cancellationToken)
332 ArgumentNullException.ThrowIfNull(metadata);
337 var instanceId = metadata.Require(x => x.Id);
340 if (!
instances.TryGetValue(instanceId, out container))
342 logger.LogDebug(
"Not offlining removed instance {instanceId}", metadata.Id);
349 logger.LogInformation(
"Offlining instance ID {instanceId}", metadata.Id);
356 ValueTask<Job?[]> groupedTask =
default;
363 .Where(x => x.Instance!.Id == metadata.Id && !x.StoppedAt.HasValue)
364 .Select(x =>
new Job(x.Id!.Value))
365 .ToListAsync(cancellationToken);
390 await container.
Instance.DisposeAsync();
396 public async ValueTask
OnlineInstance(Models.Instance metadata, CancellationToken cancellationToken)
398 ArgumentNullException.ThrowIfNull(metadata);
400 var instanceId = metadata.Require(x => x.Id);
405 logger.LogDebug(
"Aborting instance creation due to it seemingly already being online");
409 logger.LogInformation(
"Onlining instance ID {instanceId} ({instanceName}) at {instancePath}", metadata.Id, metadata.Name, metadata.Path);
413 await instance.StartAsync(cancellationToken);
424 logger.LogError(
"Unable to commit onlined instance {instanceId} into service, offlining!", metadata.Id);
428 await instance.StopAsync(CancellationToken.None);
432 throw new AggregateException(innerEx, ex);
440 await instance.DisposeAsync();
450 return Task.CompletedTask;
454 public async Task
StopAsync(CancellationToken cancellationToken)
463 logger.LogWarning(
"InstanceManager was never started!");
467 logger.LogDebug(
"Stopping instance manager...");
471 logger.LogTrace(
"Interrupting startup task...");
476 var instanceFactoryStopTask =
instanceFactory.StopAsync(cancellationToken);
477 await
jobService.StopAsync(cancellationToken);
479 async ValueTask OfflineInstanceImmediate(
IInstance instance, CancellationToken cancellationToken)
483 await instance.StopAsync(cancellationToken);
487 logger.LogError(ex,
"Instance shutdown exception!");
492 await instanceFactoryStopTask;
504 logger.LogCritical(ex,
"Instance manager stop exception!");
511 ArgumentNullException.ThrowIfNull(parameters);
514 if (accessIdentifier ==
null)
516 logger.LogWarning(
"Received invalid bridge request with null access identifier!");
521 var loggedDelay =
false;
522 for (var i = 0; bridgeHandler ==
null && i < 30; ++i)
526 Task delayTask = Task.CompletedTask;
528 if (!
bridgeHandlers.TryGetValue(accessIdentifier, out bridgeHandler))
532 logger.LogTrace(
"Received bridge request with unregistered access identifier \"{aid}\". Waiting up to 3 seconds for it to be registered...", accessIdentifier);
536 delayTask =
asyncDelayer.
Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
542 if (bridgeHandler ==
null)
544 if (!
bridgeHandlers.TryGetValue(accessIdentifier, out bridgeHandler))
546 logger.LogWarning(
"Received invalid bridge request with access identifier: {accessIdentifier}", accessIdentifier);
556 ArgumentNullException.ThrowIfNull(bridgeHandler);
558 var accessIdentifier = bridgeHandler.DMApiParameters.AccessIdentifier
559 ??
throw new InvalidOperationException(
"Attempted bridge registration with null AccessIdentifier!");
563 logger.LogTrace(
"Registered bridge handler: {accessIdentifier}", accessIdentifier);
571 logger.LogTrace(
"Unregistered bridge handler: {accessIdentifier}", accessIdentifier);
581 instances.TryGetValue(metadata.Require(x => x.Id), out var container);
582 return container?.Instance;
605 List<Models.Instance>? dbInstances =
null;
608 => dbInstances = await databaseContext
612 .Include(x => x.RepositorySettings)
613 .Include(x => x.ChatSettings)
614 .ThenInclude(x => x.Channels)
615 .Include(x => x.DreamDaemonSettings)
616 .ToListAsync(cancellationToken);
621 var jobManagerStartup =
jobService.StartAsync(cancellationToken);
623 await Task.WhenAll(instanceEnumeration.AsTask(), factoryStartup, jobManagerStartup);
625 var instanceOnliningTasks = dbInstances!.Select(
634 logger.LogError(ex,
"Failed to online instance {instanceId}!", metadata.Id);
638 await Task.WhenAll(instanceOnliningTasks);
640 logger.LogInformation(
"Server ready!");
646 catch (OperationCanceledException ex)
648 logger.LogInformation(ex,
"Cancelled instance manager initialization!");
652 logger.LogCritical(e,
"Instance manager startup error!");
660 logger.LogCritical(e2,
"Failed to kill server!");
670 logger.LogDebug(
"Running as user: {username}", Environment.UserName);
676 if (!systemIdentity.CanCreateSymlinks)
677 throw new InvalidOperationException($
"The user running {Constants.CanonicalPackageName} cannot create symlinks! Please try running as an administrative user!");
698 throw new InvalidOperationException(
"Swarm private key does not match the swarm controller's!");
701 throw new InvalidOperationException(
"Swarm controller's TGS version does not match our own!");
virtual ? long Id
The ID of the entity.
string? Identifier
The server's identifier.
Extension methods for the ValueTask and ValueTask<TResult> classes.
static async ValueTask WhenAll(IEnumerable< ValueTask > tasks)
Fully await a given list of tasks .
Task Ready
Task that completes when the IInstanceManager finishes initializing.
readonly TaskCompletionSource readyTcs
The TaskCompletionSource for Ready.
readonly Dictionary< long, ReferenceCountingContainer< IInstance, InstanceWrapper > > instances
Map of instance EntityId.Ids to the respective ReferenceCountingContainer<TWrapped,...
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the InstanceManager.
void CheckSystemCompatibility()
Check we have a valid system and configuration.
readonly IJobService jobService
The IJobService for the InstanceManager.
async ValueTask OnlineInstance(Models.Instance metadata, CancellationToken cancellationToken)
Online an IInstance. A ValueTask representing the running operation.
InstanceManager(IInstanceFactory instanceFactory, IIOManager ioManager, IDatabaseContextFactory databaseContextFactory, IAssemblyInformationProvider assemblyInformationProvider, IJobService jobService, IServerControl serverControl, ISystemIdentityFactory systemIdentityFactory, IAsyncDelayer asyncDelayer, IServerPortProvider serverPortProvider, ISwarmServiceController swarmServiceController, IConsole console, IPlatformIdentifier platformIdentifier, IOptions< GeneralConfiguration > generalConfigurationOptions, IOptions< SwarmConfiguration > swarmConfigurationOptions, ILogger< InstanceManager > logger)
Initializes a new instance of the InstanceManager class.
readonly IIOManager ioManager
The IIOManager for the InstanceManager.
bool disposed
If the InstanceManager has been DisposeAsync'd.
readonly ISwarmServiceController swarmServiceController
The ISwarmServiceController for the InstanceManager.
async ValueTask DisposeAsync()
readonly CancellationTokenSource startupCancellationTokenSource
The CancellationTokenSource for Initialize(CancellationToken).
IBridgeRegistration RegisterHandler(IBridgeHandler bridgeHandler)
Register a given bridgeHandler . A representative IBridgeRegistration.
Task? startupTask
The Task returned by Initialize(CancellationToken).
readonly ISystemIdentityFactory systemIdentityFactory
The ISystemIdentityFactory for the InstanceManager.
readonly ILogger< InstanceManager > logger
The ILogger for the InstanceManager.
readonly SwarmConfiguration swarmConfiguration
The SwarmConfiguration for the InstanceManager.
Task StartAsync(CancellationToken cancellationToken)
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the InstanceManager.
async Task Initialize(CancellationToken cancellationToken)
Initializes the InstanceManager.
async Task StopAsync(CancellationToken cancellationToken)
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the InstanceManager.
readonly IDatabaseContextFactory databaseContextFactory
The IDatabaseContextFactory for the InstanceManager.
readonly SemaphoreSlim instanceStateChangeSemaphore
SemaphoreSlim used to guard calls to OnlineInstance(Models.Instance, CancellationToken) and OfflineIn...
readonly IServerControl serverControl
The IServerControl for the InstanceManager.
IInstanceReference? GetInstanceReference(Api.Models.Instance metadata)
Get the IInstanceReference associated with given metadata . The IInstance associated with the given m...
async ValueTask MoveInstance(Models.Instance instance, string oldPath, CancellationToken cancellationToken)
Move an IInstance. A ValueTask representing the running operation.
async ValueTask< BridgeResponse?> ProcessBridgeRequest(BridgeParameters parameters, CancellationToken cancellationToken)
Handle a set of bridge parameters . A ValueTask<TResult> resulting in the BridgeResponse for the requ...
readonly IServerPortProvider serverPortProvider
The IServerPortProvider for the InstanceManager.
readonly IConsole console
The IConsole for the InstanceManager.
readonly? string originalConsoleTitle
The original IConsole.Title of console.
readonly IAsyncDelayer asyncDelayer
The IAsyncDelayer for the InstanceManager.
IInstanceCore? GetInstance(Models.Instance metadata)
Get the IInstanceCore for a given instance if it's online. The IInstanceCore if it is online,...
readonly IInstanceFactory instanceFactory
The IInstanceFactory for the InstanceManager.
async ValueTask InitializeSwarm(CancellationToken cancellationToken)
Initializes the connection to the TGS swarm.
readonly CancellationTokenSource shutdownCancellationTokenSource
The CancellationTokenSource linked with the token given to StopAsync(CancellationToken).
async ValueTask OfflineInstance(Models.Instance metadata, User user, CancellationToken cancellationToken)
Offline an IInstance. A ValueTask representing the running operation.
readonly Dictionary< string, IBridgeHandler > bridgeHandlers
Map of DMApiParameters.AccessIdentifiers to their respective IBridgeHandlers.
Parameters for a bridge request.
string AccessIdentifier
Used to identify and authenticate the DreamDaemon instance.
General configuration options.
void CheckCompatibility(ILogger logger)
Validates the current ConfigVersion's compatibility and provides migration instructions.
Configuration for the server swarm system.
ApiController for managing Components.Instances.
const string InstanceAttachFileName
File name to allow attaching instances.
Extension methods for the Socket class.
static void BindTest(IPlatformIdentifier platformIdentifier, ushort port, bool includeIPv6, bool udp)
Attempt to exclusively bind to a given port .
IIOManager that resolves paths to Environment.CurrentDirectory.
const string CurrentDirectory
Path to the current working directory for the IIOManager.
Represents an Api.Models.Instance in the database.
Wrapper for managing some TWrapped .
TWrapped Instance
The TWrapped .
Task OnZeroReferences
A Task that completes when there are no TReference s active for the Instance.
Async lock context helper.
static async ValueTask< SemaphoreSlimContext > Lock(SemaphoreSlim semaphore, CancellationToken cancellationToken)
Asyncronously locks a semaphore .
For interacting with the instance services.
Provider for IInstanceCores.
Factory for creating IInstances.
IIOManager CreateGameIOManager(Models.Instance metadata)
Create an IIOManager that resolves to the "Game" directory of the Models.Instance defined by metadata...
ValueTask< IInstance > CreateInstance(IBridgeRegistrar bridgeRegistrar, Models.Instance metadata)
Create an IInstance.
Component version of IInstanceCore.
Controller version of IInstanceCore.
ValueTask< BridgeResponse?> ProcessBridgeRequest(BridgeParameters parameters, CancellationToken cancellationToken)
Handle a set of bridge parameters .
Registers IBridgeHandlers.
Represents a registration of an interop session.
Represents a service that may take an updated Host assembly and run it, stopping the current assembly...
ValueTask Die(Exception? exception)
Kill the server with a fatal exception.
Provides access to the server's HttpApiPort.
ushort HttpApiPort
The port the server listens on.
Factory for scoping usage of IDatabaseContexts. Meant for use by Components.
ValueTask UseContextTaskReturn(Func< IDatabaseContext, Task > operation)
Run an operation in the scope of an IDatabaseContext.
ValueTask UseContext(Func< IDatabaseContext, ValueTask > operation)
Run an operation in the scope of an IDatabaseContext.
IDatabaseCollection< Instance > Instances
The Instances in the IDatabaseContext.
Abstraction for global::System.Console.
string? Title
Gets or sets the IConsole window's title. Can return null if getting the console title is not support...
void SetTitle(string newTitle)
Sets a newTitle console window.
Interface for using filesystems.
string ConcatPath(params string[] paths)
Combines an array of strings into a path.
ValueTask WriteAllBytes(string path, byte[] contents, CancellationToken cancellationToken)
Writes some contents to a file at path overwriting previous content.
Task DeleteDirectory(string path, CancellationToken cancellationToken)
Recursively delete a directory, removes and does not enter any symlinks encounterd.
Task MoveDirectory(string source, string destination, CancellationToken cancellationToken)
Moves a directory at source to destination .
ValueTask< Job?> CancelJob(Job job, User? user, bool blocking, CancellationToken cancellationToken)
Cancels a give job .
The service that manages everything to do with jobs.
void Activate(IInstanceCoreProvider instanceCoreProvider)
Activate the IJobManager.
Factory for ISystemIdentitys.
ISystemIdentity GetCurrent()
Retrieves a ISystemIdentity representing the user executing tgstation-server.
Start and stop controllers for a swarm service.
ValueTask Shutdown(CancellationToken cancellationToken)
Deregister with the swarm controller or put clients into querying state.
ValueTask< SwarmRegistrationResult > Initialize(CancellationToken cancellationToken)
Attempt to register with the swarm controller if not one, sets up the database otherwise.
For waiting asynchronously.
Task Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan .
SwarmRegistrationResult
Result of attempting to register with a swarm controller.