1 using Microsoft.Extensions.Logging;
5 using System.Threading.Tasks;
24 public sealed
override bool AlphaIsActive =>
true;
27 public sealed
override Models.CompileJob ActiveCompileJob =>
Server?.Dmb.CompileJob;
70 ILogger<BasicWatchdog> logger,
72 Api.Models.Instance instance,
76 sessionControllerFactory,
79 databaseContextFactory,
86 initialLaunchParameters,
97 string exitWord =
Server.TerminationWasRequested ?
"exited" :
"crashed";
98 if (
Server.RebootState == Session.RebootState.Shutdown)
101 await Chat.SendWatchdogMessage(
103 CultureInfo.InvariantCulture,
104 "Server {0}! Shutting down due to graceful termination request...",
108 .ConfigureAwait(
false);
112 await Chat.SendWatchdogMessage(
114 CultureInfo.InvariantCulture,
115 "Server {0}! Rebooting...",
119 .ConfigureAwait(
false);
122 var rebootState =
Server.RebootState;
123 if (gracefulRebootRequired && rebootState == Session.RebootState.Normal)
125 Logger.LogError(
"Watchdog reached normal reboot state with gracefulRebootRequired set!");
126 rebootState = Session.RebootState.
Restart;
129 gracefulRebootRequired =
false;
130 Server.ResetRebootState();
134 case Session.RebootState.Normal:
135 return HandleNormalReboot();
136 case Session.RebootState.Restart:
138 case Session.RebootState.Shutdown:
140 await Chat.SendWatchdogMessage(
141 "Active server rebooted! Shutting down due to graceful termination request...",
144 .ConfigureAwait(
false);
147 throw new InvalidOperationException($
"Invalid reboot state: {rebootState}");
151 await
Server.SetRebootState(Session.RebootState.Restart, cancellationToken).ConfigureAwait(
false);
152 gracefulRebootRequired =
true;
155 await HandleNewDmbAvailable(cancellationToken).ConfigureAwait(
false);
159 throw new InvalidOperationException($
"Invalid activation reason: {reason}");
170 gracefulRebootRequired =
false;
180 CancellationToken cancellationToken)
183 var reattachInProgress = reattachInfo != null;
192 Task<ISessionController> serverLaunchTask;
193 if (!reattachInProgress)
195 dmbToUse = await PrepServerForLaunch(dmbToUse, cancellationToken).ConfigureAwait(
false);
199 ActiveLaunchParameters,
207 Server = await serverLaunchTask.ConfigureAwait(
false);
210 Server?.SetHighPriority();
215 await ReattachFailure(
218 .ConfigureAwait(
false);
222 await CheckLaunchResult(
Server,
"Server", cancellationToken).ConfigureAwait(
false);
224 Server.EnableCustomChatCommands();
229 bool serverWasActive =
Server != null;
230 DisposeAndNullControllers();
233 if (dmbToUse != null && !serverWasActive)
246 bool dmbUpdatePending = ActiveLaunchParameters != LastLaunchParameters;
257 gracefulRebootRequired =
true;
258 if (
Server.Dmb.CompileJob.DMApiVersion == null)
259 return Chat.SendWatchdogMessage(
260 "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.",
263 return Server.SetRebootState(Session.RebootState.Restart, cancellationToken);
272 protected virtual Task<IDmbProvider> PrepServerForLaunch(
IDmbProvider dmbToUse, CancellationToken cancellationToken) => Task.FromResult(dmbToUse);
277 if (!gracefulRebootRequired)
278 return base.ResetRebootState(cancellationToken);
280 return Restart(
true, cancellationToken);
284 public sealed
override Task InstanceRenamed(
string newInstanceName, CancellationToken cancellationToken)
285 =>
Server?.InstanceRenamed(newInstanceName, cancellationToken) ?? Task.CompletedTask;
async Task< ISessionController > Reattach(ReattachInformation reattachInformation, CancellationToken cancellationToken)
Create a ISessionController from an existing DreamDaemon instance
Handles saving and loading ReattachInformation.
bool gracefulRebootRequired
If the server is set to gracefully reboot due to a pending dmb or settings change.
Base class for IWatchdogs.
Factory for scoping usage of IDatabaseContexts. Meant for use by Components
virtual Task HandleNewDmbAvailable(CancellationToken cancellationToken)
Handler for MonitorActivationReason.NewDmbAvailable.
Launch settings for DreamDaemon
virtual MonitorAction HandleNormalReboot()
Handler for MonitorActivationReason.ActiveServerRebooted when the RebootState is RebootState.Normal.
For waiting asynchronously
override void DisposeAndNullControllersImpl()
Call IDisposable.Dispose and null the fields for all ISessionControllers.
override async Task InitControllers(Task chatTask, ReattachInformation reattachInfo, CancellationToken cancellationToken)
Starts all ISessionControllers.
For managing connected chat services
RebootState
Represents the action to take when /world/Reboot() is called
A IWatchdog that manages one server.
Factory for ISessionControllers
MonitorActivationReason
Reasons for the monitor to wake up
Factory for IDmbProviders
Consumes EventTypes and takes the appropriate actions
BasicWatchdog(IChatManager chat, ISessionControllerFactory sessionControllerFactory, IDmbFactory dmbFactory, ISessionPersistor sessionPersistor, IDatabaseContextFactory databaseContextFactory, IJobManager jobManager, IServerControl serverControl, IAsyncDelayer asyncDelayer, IIOManager diagnosticsIOManager, IEventConsumer eventConsumer, ILogger< BasicWatchdog > logger, DreamDaemonLaunchParameters initialLaunchParameters, Api.Models.Instance instance, bool autoStart)
Initializes a new instance of the BasicWatchdog .
Task Restart()
Restarts the Host
Handles communication with a DreamDaemon IProcess
override async Task< MonitorAction > HandleMonitorWakeup(MonitorActivationReason reason, CancellationToken cancellationToken)
Handles the actions to take when the monitor has to "wake up"
Manages the runtime of Jobs
async Task< ISessionController > LaunchNew(IDmbProvider dmbProvider, IByondExecutableLock currentByondLock, DreamDaemonLaunchParameters launchParameters, bool apiValidate, CancellationToken cancellationToken)
Create a ISessionController from a freshly launch DreamDaemon instance
Provides absolute paths to the latest compiled .dmbs
Interface for using filesystems
MonitorAction
The action for the monitor loop to take when control is returned to it
override Task ResetRebootState(CancellationToken cancellationToken)
Cancels pending graceful actions
IDmbProvider LockNextDmb(int lockCount)
Gets the next IDmbProvider
Represents a service that may take an updated Host assembly and run it, stopping the current assembly...