2using System.Globalization;
5using System.Threading.Tasks;
7using Microsoft.Extensions.Logging;
71 ILogger<BasicWatchdog> logger,
73 Api.Models.Instance instance,
77 sessionControllerFactory,
87 initialLaunchParameters,
97 return base.ResetRebootState(cancellationToken);
99 return Restart(
true, cancellationToken);
103 public sealed
override Task
InstanceRenamed(
string newInstanceName, CancellationToken cancellationToken)
104 =>
Server?.InstanceRenamed(newInstanceName, cancellationToken) ?? Task.CompletedTask;
112 var eventType = Server.TerminationWasRequested
113 ? EventType.WorldEndProcess
115 await
HandleEventImpl(eventType, Enumerable.Empty<
string>(),
false, cancellationToken);
117 var exitWord = Server.TerminationWasRequested ?
"exited" :
"crashed";
118 if (
Server.RebootState == Session.RebootState.Shutdown)
123 CultureInfo.InvariantCulture,
124 "Server {0}! Shutting down due to graceful termination request...",
131 CultureInfo.InvariantCulture,
132 "Server {0}! Rebooting...",
136 var rebootState =
Server.RebootState;
139 Logger.LogError(
"Watchdog reached normal reboot state with gracefulRebootRequired set!");
140 rebootState = Session.RebootState.Restart;
144 Server.ResetRebootState();
151 case Session.RebootState.Normal:
153 case Session.RebootState.Restart:
155 case Session.RebootState.Shutdown:
158 "Active server rebooted! Shutting down due to graceful termination request...");
161 throw new InvalidOperationException($
"Invalid reboot state: {rebootState}");
170 await
Server.SetRebootState(Session.RebootState.Restart, cancellationToken);
183 throw new InvalidOperationException($
"Invalid activation reason: {reason}");
192 var disposeTask =
Server?.DisposeAsync();
194 if (!disposeTask.HasValue)
197 await disposeTask.Value;
208 CancellationToken cancellationToken)
211 var reattachInProgress = reattachInfo !=
null;
220 Task<ISessionController> serverLaunchTask;
221 if (!reattachInProgress)
223 Logger.LogTrace(
"Initializing controller with CompileJob {compileJobId}...", dmbToUse.CompileJob.Id);
242 Server = await serverLaunchTask;
252 if (!reattachInProgress)
257 Server.EnableCustomChatCommands();
261 Logger.LogTrace(ex,
"Controller initialization failure!");
264 bool serverWasActive =
Server !=
null;
270 if (dmbToUse !=
null && !serverWasActive)
295 var result = settingsUpdatePending ? MonitorAction.Restart :
MonitorAction.Continue;
296 return Task.FromResult(result);
307 if (
Server.CompileJob.DMApiVersion ==
null)
310 "A new deployment has been made but cannot be applied automatically as the currently running server has no DMAPI. Please manually reboot the server to apply the update.");
314 await
Server.SetRebootState(Session.RebootState.Restart, cancellationToken);
Launch settings for DreamDaemon.
IDmbProvider LockNextDmb(int lockCount)
Gets the next IDmbProvider. A new IDmbProvider.
async Task< ISessionController > LaunchNew(IDmbProvider dmbProvider, IByondExecutableLock currentByondLock, DreamDaemonLaunchParameters launchParameters, bool apiValidate, CancellationToken cancellationToken)
Create a ISessionController from a freshly launch DreamDaemon instance. A Task<TResult> resulting in ...
async Task< ISessionController > Reattach(ReattachInformation reattachInformation, CancellationToken cancellationToken)
Create a ISessionController from an existing DreamDaemon instance. A Task<TResult> resulting in a new...
Task Save(ReattachInformation reattachInformation, CancellationToken cancellationToken)
Save some reattachInformation . A Task representing the running operation.
A IWatchdog that manages one server.
virtual Task< IDmbProvider > PrepServerForLaunch(IDmbProvider dmbToUse, CancellationToken cancellationToken)
Prepare the server to launch a new instance with the WatchdogBase.ActiveLaunchParameters and a given ...
override async Task DisposeAndNullControllersImpl()
Call IDisposable.Dispose and null the fields for all ISessionControllers. A Task representing the run...
virtual Task SessionStartupPersist(CancellationToken cancellationToken)
Called to save the current Server into the WatchdogBase.SessionPersistor when initially launched.
override Task ResetRebootState(CancellationToken cancellationToken)
Cancels pending graceful actions. A Task representing the running operation.
BasicWatchdog(IChatManager chat, ISessionControllerFactory sessionControllerFactory, IDmbFactory dmbFactory, ISessionPersistor sessionPersistor, IJobManager jobManager, IServerControl serverControl, IAsyncDelayer asyncDelayer, IIOManager diagnosticsIOManager, IEventConsumer eventConsumer, IRemoteDeploymentManagerFactory remoteDeploymentManagerFactory, ILogger< BasicWatchdog > logger, DreamDaemonLaunchParameters initialLaunchParameters, Api.Models.Instance instance, bool autoStart)
Initializes a new instance of the BasicWatchdog class.
virtual async Task HandleNewDmbAvailable(CancellationToken cancellationToken)
Handler for MonitorActivationReason.NewDmbAvailable.
override async Task< MonitorAction > HandleMonitorWakeup(MonitorActivationReason reason, CancellationToken cancellationToken)
Handles the actions to take when the monitor has to "wake up". A Task<TResult> resulting in the Monit...
ISessionController Server
The single ISessionController.
sealed override ISessionController GetActiveController()
Get the active ISessionController. The active ISessionController.
virtual Task< MonitorAction > HandleNormalReboot(CancellationToken cancellationToken)
Handler for MonitorActivationReason.ActiveServerRebooted when the RebootState is RebootState....
override async Task InitController(Task eventTask, ReattachInformation reattachInfo, CancellationToken cancellationToken)
Starts all ISessionControllers. A Task representing the running operation.
bool gracefulRebootRequired
If the server is set to gracefully reboot due to a pending dmb or settings change.
sealed override bool AlphaIsActive
If the alpha server is the active server.
sealed override Task InstanceRenamed(string newInstanceName, CancellationToken cancellationToken)
Called when the owning Instance is renamed. A Task representing the running operation.
Base class for IWatchdogs.
readonly IJobManager jobManager
The IJobManager for the WatchdogBase.
ILogger< WatchdogBase > Logger
The ILogger for the WatchdogBase.
async Task ReattachFailure(CancellationToken cancellationToken)
Call from InitController(Task, ReattachInformation, CancellationToken) when a reattach operation fail...
async Task Restart(bool graceful, CancellationToken cancellationToken)
Restarts the watchdog. A Task representing the running operation.
readonly bool autoStart
If the WatchdogBase should LaunchNoLock(bool, bool, bool, ReattachInformation, CancellationToken) in ...
readonly IEventConsumer eventConsumer
The IEventConsumer that is not the WatchdogBase.
async Task HandleEventImpl(EventType eventType, IEnumerable< string > parameters, bool relayToSession, CancellationToken cancellationToken)
Handle a given eventType without re-throwing errors.
DreamDaemonLaunchParameters LastLaunchParameters
The DreamDaemonLaunchParameters the active server is using.
readonly IRemoteDeploymentManagerFactory remoteDeploymentManagerFactory
The IRemoteDeploymentManagerFactory for the WatchdogBase.
async Task DisposeAndNullControllers(CancellationToken cancellationToken)
Wrapper for DisposeAndNullControllersImpl under a locked context.
readonly IIOManager diagnosticsIOManager
The IIOManager pointing to the Diagnostics directory.
async Task CheckLaunchResult(ISessionController controller, string serverName, CancellationToken cancellationToken)
Check the LaunchResult of a given controller for errors and throw a JobException if any are detected...
DreamDaemonLaunchParameters ActiveLaunchParameters
The DreamDaemonLaunchParameters to be applied.
async Task BeforeApplyDmb(Models.CompileJob newCompileJob, CancellationToken cancellationToken)
To be called before a given newCompileJob goes live.
IChatManager Chat
The IChatManager for the WatchdogBase.
For managing connected chat services.
void QueueWatchdogMessage(string message)
Queue a chat message to configured watchdog channels.
Factory for IDmbProviders.
Provides absolute paths to the latest compiled .dmbs.
Factory for creating IRemoteDeploymentManagers.
Consumes EventTypes and takes the appropriate actions.
Factory for ISessionControllers.
Handles communication with a DreamDaemon IProcess.
Handles saving and loading ReattachInformation.
Represents a service that may take an updated Host assembly and run it, stopping the current assembly...
Interface for using filesystems.
Manages the runtime of Jobs.
For waiting asynchronously.
EventType
Types of events. Mirror in tgs.dm.
RebootState
Represents the action to take when /world/Reboot() is called.
MonitorAction
The action for the monitor loop to take when control is returned to it.
MonitorActivationReason
Reasons for the monitor to wake up.