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;
200 IOptions<GeneralConfiguration> generalConfigurationOptions,
201 IOptions<SwarmConfiguration> swarmConfigurationOptions,
202 IOptions<InternalConfiguration> internalConfigurationOptions,
203 ILogger<InstanceManager>
logger)
215 this.console =
console ??
throw new ArgumentNullException(nameof(
console));
217 generalConfiguration = generalConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(generalConfigurationOptions));
218 swarmConfiguration = swarmConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(swarmConfigurationOptions));
219 internalConfiguration = internalConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(internalConfigurationOptions));
220 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
224 instances =
new Dictionary<long, ReferenceCountingContainer<IInstance, InstanceWrapper>>();
226 readyTcs =
new TaskCompletionSource();
243 await instanceKvp.Value.Instance.DisposeAsync();
249 logger.LogInformation(
"Server shutdown");
255 ArgumentNullException.ThrowIfNull(metadata);
259 if (!
instances.TryGetValue(metadata.Require(x => x.Id), out var instance))
262 return instance.AddReference();
267 public async ValueTask
MoveInstance(Models.Instance instance,
string oldPath, CancellationToken cancellationToken)
269 ArgumentNullException.ThrowIfNull(oldPath);
273 if (instanceReferenceCheck !=
null)
274 throw new InvalidOperationException(
"Cannot move an online instance!");
275 var newPath = instance.Path!;
288 "Error moving instance {instanceId}!",
292 logger.LogDebug(
"Reverting instance {instanceId}'s path to {oldPath} in the DB...", instance.Id, oldPath);
301 db.Instances.Attach(targetInstance);
302 targetInstance.Path = oldPath;
303 return db.Save(CancellationToken.None);
310 "Error reverting database after failing to move instance {instanceId}! Attempting to detach...",
319 CancellationToken.None);
325 "Okay, what gamma radiation are you under? Failed to write instance attach file!");
327 throw new AggregateException(tripleEx, innerEx, ex);
330 throw new AggregateException(ex, innerEx);
338 public async ValueTask
OfflineInstance(Models.Instance metadata,
User user, CancellationToken cancellationToken)
340 ArgumentNullException.ThrowIfNull(metadata);
345 var instanceId = metadata.Require(x => x.Id);
348 if (!
instances.TryGetValue(instanceId, out container))
350 logger.LogDebug(
"Not offlining removed instance {instanceId}", metadata.Id);
357 logger.LogInformation(
"Offlining instance ID {instanceId}", metadata.Id);
364 ValueTask<Job?[]> groupedTask =
default;
371 .Where(x => x.Instance!.Id == metadata.Id && !x.StoppedAt.HasValue)
372 .Select(x =>
new Job(x.Id!.Value))
373 .ToListAsync(cancellationToken);
398 await container.
Instance.DisposeAsync();
404 public async ValueTask
OnlineInstance(Models.Instance metadata, CancellationToken cancellationToken)
406 ArgumentNullException.ThrowIfNull(metadata);
408 var instanceId = metadata.Require(x => x.Id);
413 logger.LogDebug(
"Aborting instance creation due to it seemingly already being online");
417 logger.LogInformation(
"Onlining instance ID {instanceId} ({instanceName}) at {instancePath}", metadata.Id, metadata.Name, metadata.Path);
421 await instance.StartAsync(cancellationToken);
432 logger.LogError(
"Unable to commit onlined instance {instanceId} into service, offlining!", metadata.Id);
436 await instance.StopAsync(CancellationToken.None);
440 throw new AggregateException(innerEx, ex);
448 await instance.DisposeAsync();
458 return Task.CompletedTask;
462 public async Task
StopAsync(CancellationToken cancellationToken)
471 logger.LogWarning(
"InstanceManager was never started!");
475 logger.LogDebug(
"Stopping instance manager...");
479 logger.LogTrace(
"Interrupting startup task...");
484 var instanceFactoryStopTask =
instanceFactory.StopAsync(cancellationToken);
485 await
jobService.StopAsync(cancellationToken);
487 async ValueTask OfflineInstanceImmediate(
IInstance instance, CancellationToken cancellationToken)
491 await instance.StopAsync(cancellationToken);
495 logger.LogError(ex,
"Instance shutdown exception!");
500 await instanceFactoryStopTask;
512 logger.LogCritical(ex,
"Instance manager stop exception!");
519 ArgumentNullException.ThrowIfNull(parameters);
522 if (accessIdentifier ==
null)
524 logger.LogWarning(
"Received invalid bridge request with null access identifier!");
529 var loggedDelay =
false;
530 for (var i = 0; bridgeHandler ==
null && i < 30; ++i)
534 Task delayTask = Task.CompletedTask;
536 if (!
bridgeHandlers.TryGetValue(accessIdentifier, out bridgeHandler))
540 logger.LogTrace(
"Received bridge request with unregistered access identifier \"{aid}\". Waiting up to 3 seconds for it to be registered...", accessIdentifier);
544 delayTask =
asyncDelayer.
Delay(TimeSpan.FromMilliseconds(100), cancellationToken).AsTask();
550 if (bridgeHandler ==
null)
552 if (!
bridgeHandlers.TryGetValue(accessIdentifier, out bridgeHandler))
554 logger.LogWarning(
"Received invalid bridge request with access identifier: {accessIdentifier}", accessIdentifier);
564 ArgumentNullException.ThrowIfNull(bridgeHandler);
566 var accessIdentifier = bridgeHandler.DMApiParameters.AccessIdentifier
567 ??
throw new InvalidOperationException(
"Attempted bridge registration with null AccessIdentifier!");
571 logger.LogTrace(
"Registered bridge handler: {accessIdentifier}", accessIdentifier);
579 logger.LogTrace(
"Unregistered bridge handler: {accessIdentifier}", accessIdentifier);
589 instances.TryGetValue(metadata.Require(x => x.Id), out var container);
590 return container?.Instance;
613 List<Models.Instance>? dbInstances =
null;
616 => dbInstances = await databaseContext
620 .Include(x => x.RepositorySettings)
621 .Include(x => x.ChatSettings)
622 .ThenInclude(x => x.Channels)
623 .Include(x => x.DreamDaemonSettings)
624 .ToListAsync(cancellationToken);
629 var jobManagerStartup =
jobService.StartAsync(cancellationToken);
631 await Task.WhenAll(instanceEnumeration.AsTask(), factoryStartup, jobManagerStartup);
633 var instanceOnliningTasks = dbInstances!.Select(
642 logger.LogError(ex,
"Failed to online instance {instanceId}!", metadata.Id);
646 await Task.WhenAll(instanceOnliningTasks);
648 logger.LogInformation(
"Server ready!");
654 catch (OperationCanceledException ex)
656 logger.LogInformation(ex,
"Cancelled instance manager initialization!");
660 logger.LogCritical(e,
"Instance manager startup error!");
668 logger.LogCritical(e2,
"Failed to kill server!");
678 logger.LogDebug(
"Running as user: {username}", Environment.UserName);
684 if (!systemIdentity.CanCreateSymlinks)
685 throw new InvalidOperationException($
"The user running {Constants.CanonicalPackageName} cannot create symlinks! Please try running as an administrative user!");
689 logger.LogWarning(
"TGS is being run as the root account. This is not recommended.");
711 throw new InvalidOperationException(
"Swarm private key does not match the swarm controller's!");
714 throw new InvalidOperationException(
"Swarm controller's TGS version does not match our own!");
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.
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.
readonly InternalConfiguration internalConfiguration
The InternalConfiguration for the InstanceManager.
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.
void PreflightChecks()
Check we have a valid system and configuration.
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 me...
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 reque...
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.
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, IOptions< InternalConfiguration > internalConfigurationOptions, ILogger< InstanceManager > logger)
Initializes a new instance of the InstanceManager class.
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.
Unstable configuration options used internally by TGS.
bool UsingDocker
If the server is running inside of a Docker container.
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, ILogger? logger=null)
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.
ValueTask Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan .
SwarmRegistrationResult
Result of attempting to register with a swarm controller.