// Copyright (C) 2025, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Duplicati.Library.Main.Operation.Common
{
///
/// Interface for async access to the current control state
///
public interface ITaskReader
{
///
/// A cancellation token that can be used to monitor progress abort
///
CancellationToken ProgressToken { get; }
///
/// Gets the progress state, waiting if the state is paused, throws if terminated
///
/// true if the progress should continue, false if it should stop
Task ProgressRendevouz();
///
/// A cancellation token that can be used to monitor transfer abort
///
CancellationToken TransferToken { get; }
///
/// Gets the transfer state, waiting if the state is paused, throws if terminated
///
/// true if the transfer should continue, false if it should stop
Task TransferRendevouz();
#if DEBUG
///
/// Callback for testing the task control
///
/// The path being processed
Action TestMethodCallback { get; set; }
#endif
}
///
/// Interface for controlling the progress
///
public interface ITaskCommander
{
///
/// Requests that progress should be paused
///
/// If true, the transfer should also be paused
void Pause(bool alsoTransfer);
///
/// Resumes running a paused process
///
void Resume();
///
/// Requests that the progress should be stopped in a controlled way.
/// This will finalize the current file and transfer.
///
void Stop();
///
/// Terminates the progress without allowing a flush
///
void Terminate();
}
///
/// Interface for the task control
///
public interface ITaskControl : ITaskReader, ITaskCommander
{
}
///
/// Implementation of the task control
///
public class TaskControl : ITaskControl, IDisposable
{
///
/// State tracking to avoid invalid requests
///
private enum State
{
///
/// The task is running
///
Active,
///
/// The task is paused
///
Paused,
///
/// The task is stopped
///
Stopped,
///
/// The task is terminated
///
Terminated
}
///
/// Internal control state for progress event
///
private TaskCompletionSource m_progress = new();
///
/// Internal control state for transfer event
///
private TaskCompletionSource m_transfer = new();
///
/// The progress task completion source, cancelled if the operation is terminated
///
private readonly CancellationTokenSource m_progressTcs = new();
///
/// The transfer task completion source, cancelled if the operation is terminated
///
private readonly CancellationTokenSource m_transferTcs = new();
///
/// The control lock instance
///
private readonly object m_lock = new object();
///
/// The current progress state
///
private State m_progressstate = State.Paused;
///
/// The current transfer state
///
private State m_transferstate = State.Paused;
///
/// A cancellation token that is cancelled if the operation is aborted
///
public CancellationToken ProgressToken => m_progressTcs.Token;
///
/// A cancellation token that is cancelled if the transfers are aborted
///
public CancellationToken TransferToken => m_transferTcs.Token;
///
/// Initializes a new instance of the class.
///
public TaskControl()
{
Resume();
}
///
/// Gets the progress state, waiting if the state is paused
///
/// true if the progress should continue, false if it should stop
public async Task ProgressRendevouz()
{
var res = await m_progress.Task.ConfigureAwait(false);
lock (m_lock)
m_progressTcs.Token.ThrowIfCancellationRequested();
return res;
}
///
/// Gets the transfer state, waiting if the state is paused
///
/// true if the transfer should continue, false if it should stop
public async Task TransferRendevouz()
{
var res = await m_transfer.Task.ConfigureAwait(false);
lock (m_lock)
m_transferTcs.Token.ThrowIfCancellationRequested();
return res;
}
///
/// Resumes running a paused process
///
public void Resume()
{
lock (m_lock)
{
if (m_progressstate == State.Paused)
{
m_progress.SetResult(true);
m_progressstate = State.Active;
}
if (m_transferstate == State.Paused)
{
m_transfer.SetResult(true);
m_transferstate = State.Active;
}
}
}
///
/// Requests that progress should be paused
///
public void Pause(bool alsoTransfer)
{
lock (m_lock)
{
if (m_progressstate == State.Active)
{
m_progress = new TaskCompletionSource();
m_progressstate = State.Paused;
}
if (alsoTransfer && m_transferstate == State.Active)
{
m_transfer = new TaskCompletionSource();
m_transferstate = State.Paused;
}
}
}
///
/// Requests that the progress should be stopped in an orderly manner,
/// which allows current transfers to be completed.
///
public void Stop()
{
lock (m_lock)
{
if (m_progressstate == State.Active || m_progressstate == State.Paused)
{
if (m_progressstate != State.Paused)
m_progress = new TaskCompletionSource();
m_progress.SetResult(false);
m_progressstate = State.Stopped;
}
// For symmetry, we also mark the transfer as stopped
// but the transfer logic does not stop transfers
// unless they are aborted
if (m_transferstate == State.Active || m_transferstate == State.Paused)
{
if (m_transferstate != State.Paused)
m_transfer = new TaskCompletionSource();
m_transfer.SetResult(false);
m_transferstate = State.Stopped;
}
}
}
///
/// Terminates the progress without allowing a flush
///
public void Terminate()
{
lock (m_lock)
{
if (m_progressstate != State.Terminated)
{
if (m_progressstate != State.Paused)
m_progress = new TaskCompletionSource();
m_progress.SetCanceled();
m_progressstate = State.Terminated;
m_progressTcs.Cancel();
}
if (m_transferstate != State.Terminated)
{
if (m_transferstate != State.Paused)
m_transfer = new TaskCompletionSource();
m_transfer.SetCanceled();
m_transferstate = State.Terminated;
m_transferTcs.Cancel();
}
}
}
///
public void Dispose()
{
Terminate();
}
#if DEBUG
///
public Action TestMethodCallback { get; set; }
#endif
}
}