update shutdown

This commit is contained in:
Luke Pulverenti 2016-04-22 12:12:20 -04:00
parent f3c3c74d84
commit c94706d6ee
3 changed files with 39 additions and 2 deletions

View file

@ -179,6 +179,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer
private void OnWebSocketConnecting(WebSocketConnectingEventArgs args) private void OnWebSocketConnecting(WebSocketConnectingEventArgs args)
{ {
if (_disposed)
{
return;
}
if (WebSocketConnecting != null) if (WebSocketConnecting != null)
{ {
WebSocketConnecting(this, args); WebSocketConnecting(this, args);
@ -187,6 +192,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer
private void OnWebSocketConnected(WebSocketConnectEventArgs args) private void OnWebSocketConnected(WebSocketConnectEventArgs args)
{ {
if (_disposed)
{
return;
}
if (WebSocketConnected != null) if (WebSocketConnected != null)
{ {
WebSocketConnected(this, args); WebSocketConnected(this, args);
@ -331,6 +341,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
var httpRes = httpReq.Response; var httpRes = httpReq.Response;
if (_disposed)
{
httpRes.StatusCode = 503;
httpRes.Close();
return Task.FromResult(true);
}
var operationName = httpReq.OperationName; var operationName = httpReq.OperationName;
var localPath = url.LocalPath; var localPath = url.LocalPath;

View file

@ -71,6 +71,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// <value>The web socket listeners.</value> /// <value>The web socket listeners.</value>
private readonly List<IWebSocketListener> _webSocketListeners = new List<IWebSocketListener>(); private readonly List<IWebSocketListener> _webSocketListeners = new List<IWebSocketListener>();
private bool _disposed;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ServerManager" /> class. /// Initializes a new instance of the <see cref="ServerManager" /> class.
/// </summary> /// </summary>
@ -143,6 +145,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// <param name="e">The <see cref="WebSocketConnectEventArgs" /> instance containing the event data.</param> /// <param name="e">The <see cref="WebSocketConnectEventArgs" /> instance containing the event data.</param>
void HttpServer_WebSocketConnected(object sender, WebSocketConnectEventArgs e) void HttpServer_WebSocketConnected(object sender, WebSocketConnectEventArgs e)
{ {
if (_disposed)
{
return;
}
var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, _jsonSerializer, _logger) var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, _jsonSerializer, _logger)
{ {
OnReceive = ProcessWebSocketMessageReceived, OnReceive = ProcessWebSocketMessageReceived,
@ -164,6 +171,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// <param name="result">The result.</param> /// <param name="result">The result.</param>
private async void ProcessWebSocketMessageReceived(WebSocketMessageInfo result) private async void ProcessWebSocketMessageReceived(WebSocketMessageInfo result)
{ {
if (_disposed)
{
return;
}
//_logger.Debug("Websocket message received: {0}", result.MessageType); //_logger.Debug("Websocket message received: {0}", result.MessageType);
var tasks = _webSocketListeners.Select(i => Task.Run(async () => var tasks = _webSocketListeners.Select(i => Task.Run(async () =>
@ -244,6 +256,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager
throw new ArgumentNullException("dataFunction"); throw new ArgumentNullException("dataFunction");
} }
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var connectionsList = connections.Where(s => s.State == WebSocketState.Open).ToList(); var connectionsList = connections.Where(s => s.State == WebSocketState.Open).ToList();
@ -301,6 +318,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
_disposed = true;
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }

View file

@ -555,9 +555,10 @@ namespace MediaBrowser.ServerApplication
private static void ShutdownWindowsApplication() private static void ShutdownWindowsApplication()
{ {
_logger.Info("Calling Application.Exit"); //_logger.Info("Calling Application.Exit");
Application.Exit(); //Application.Exit();
_logger.Info("Calling Environment.Exit");
Environment.Exit(0); Environment.Exit(0);
_logger.Info("Calling ApplicationTaskCompletionSource.SetResult"); _logger.Info("Calling ApplicationTaskCompletionSource.SetResult");