85 lines
2.8 KiB
C#
85 lines
2.8 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.Threading;
|
|
|
|
namespace Microsoft.AspNetCore.Hosting.Internal
|
|
{
|
|
/// <summary>
|
|
/// Allows consumers to perform cleanup during a graceful shutdown.
|
|
/// </summary>
|
|
public class ApplicationLifetime : IApplicationLifetime
|
|
{
|
|
private readonly CancellationTokenSource _startedSource = new CancellationTokenSource();
|
|
private readonly CancellationTokenSource _stoppingSource = new CancellationTokenSource();
|
|
private readonly CancellationTokenSource _stoppedSource = new CancellationTokenSource();
|
|
|
|
/// <summary>
|
|
/// Triggered when the application host has fully started and is about to wait
|
|
/// for a graceful shutdown.
|
|
/// </summary>
|
|
public CancellationToken ApplicationStarted => _startedSource.Token;
|
|
|
|
/// <summary>
|
|
/// Triggered when the application host is performing a graceful shutdown.
|
|
/// Request may still be in flight. Shutdown will block until this event completes.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public CancellationToken ApplicationStopping => _stoppingSource.Token;
|
|
|
|
/// <summary>
|
|
/// Triggered when the application host is performing a graceful shutdown.
|
|
/// All requests should be complete at this point. Shutdown will block
|
|
/// until this event completes.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public CancellationToken ApplicationStopped => _stoppedSource.Token;
|
|
|
|
/// <summary>
|
|
/// Signals the ApplicationStopping event and blocks until it completes.
|
|
/// </summary>
|
|
public void StopApplication()
|
|
{
|
|
try
|
|
{
|
|
_stoppingSource.Cancel(throwOnFirstException: false);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// TODO: LOG
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Signals the ApplicationStarted event and blocks until it completes.
|
|
/// </summary>
|
|
public void NotifyStarted()
|
|
{
|
|
try
|
|
{
|
|
_startedSource.Cancel(throwOnFirstException: false);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// TODO: LOG
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Signals the ApplicationStopped event and blocks until it completes.
|
|
/// </summary>
|
|
public void NotifyStopped()
|
|
{
|
|
try
|
|
{
|
|
_stoppedSource.Cancel(throwOnFirstException: false);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// TODO: LOG
|
|
}
|
|
}
|
|
}
|
|
}
|