// 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.
#nullable enable
using System;
using System.Collections.Generic;
using Duplicati.Library.Interface;
using Duplicati.Server.Serialization.Interface;
namespace Duplicati.WebserverCore.Abstractions;
///
/// A cached task result
///
/// The task ID
/// The backup ID
/// The time the task started
/// The time the task finished
/// The exception that was thrown
public sealed record CachedTaskResult(long TaskID, string? BackupId, DateTime? TaskStarted, DateTime? TaskFinished, Exception? Exception);
///
/// Class to encapsulate a thread that runs a list of queued operations
///
/// The type to operate on
public interface IQueueRunnerService
{
///
/// Returns a copy of the current tasks in the queue
///
/// A list of queued tasks
List GetCurrentTasks();
///
/// Gets a flag indicating if the queue is currently executing a task
///
/// True if the queue is executing a task, false otherwise
bool GetIsActive();
///
/// Returns the currently executing task in the queue
///
/// The currently executing task, or null if no task is executing
IQueuedTask? GetCurrentTask();
///
/// Gets the cached task results for a given task ID
///
/// The task ID
/// The cached task result
CachedTaskResult? GetCachedTaskResults(long taskID);
///
/// Adds a task to the queue
///
/// The task to add
long AddTask(IQueuedTask task);
///
/// Adds a task to the queue, optionally skipping the queue
///
/// The task to add
/// Whether to skip the queue
long AddTask(IQueuedTask task, bool skipQueue);
///
/// Removes a task from the queue
///
/// Whether to wait for the task to finish
void Terminate(bool wait);
///
/// Resumes processing items in the queue
///
void Resume();
///
/// Pauses processing items in the queue
///
void Pause();
///
/// Gets the IDs of the tasks in the worker queue
///
/// A list of tuples containing the task ID and backup ID
IList> GetQueueWithIds();
///
/// Runs a task immediately, bypassing the queue.
/// Note that the task will run concurrently with the queue tasks and may cause database lock issues.
///
/// The task to run
IBasicResults? RunImmediately(IQueuedTask task);
}