using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Kyoo.Models.Attributes; namespace Kyoo.Controllers { /// /// A single task parameter. This struct contains metadata to display and utility functions to get them in the taks. /// /// This struct will be used to generate the swagger documentation of the task. public record TaskParameter { /// /// The name of this parameter. /// public string Name { get; init; } /// /// The description of this parameter. /// public string Description { get; init; } /// /// The type of this parameter. /// public Type Type { get; init; } /// /// Is this parameter required or can it be ignored? /// public bool IsRequired { get; init; } /// /// The default value of this object. /// public object DefaultValue { get; init; } /// /// The value of the parameter. /// private object Value { get; init; } /// /// Create a new task parameter. /// /// The name of the parameter /// The description of the parameter /// The type of the parameter. /// A new task parameter. public static TaskParameter Create(string name, string description) { return new() { Name = name, Description = description, Type = typeof(T) }; } /// /// Create a parameter's value to give to a task. /// /// The name of the parameter /// The value of the parameter. It's type will be used as parameter's type. /// The type of the parameter /// A TaskParameter that can be used as value. public static TaskParameter CreateValue(string name, T value) { return new() { Name = name, Type = typeof(T), Value = value }; } /// /// Create a parameter's value for the current parameter. /// /// The value to use /// A new parameter's value for this current parameter public TaskParameter CreateValue(object value) { return this with {Value = value}; } /// /// Get the value of this parameter. If the value is of the wrong type, it will be converted. /// /// The type of this parameter /// The value of this parameter. public T As() { return (T)Convert.ChangeType(Value, typeof(T)); } } /// /// A parameters container implementing an indexer to allow simple usage of parameters. /// public class TaskParameters : List { /// /// An indexer that return the parameter with the specified name. /// /// The name of the task (case sensitive) public TaskParameter this[string name] => this.FirstOrDefault(x => x.Name == name); /// /// Create a new, empty, /// public TaskParameters() {} /// /// Create a with an initial parameters content /// /// The list of parameters public TaskParameters(IEnumerable parameters) { AddRange(parameters); } } /// /// A common interface that tasks should implement. /// public interface ITask { /// /// The slug of the task, used to start it. /// public string Slug { get; } /// /// The name of the task that will be displayed to the user. /// public string Name { get; } /// /// A quick description of what this task will do. /// public string Description { get; } /// /// An optional message to display to help the user. /// public string HelpMessage { get; } /// /// Should this task be automatically runned at app startup? /// public bool RunOnStartup { get; } /// /// The priority of this task. Only used if is true. /// It allow one to specify witch task will be started first as tasked are run on a Priority's descending order. /// public int Priority { get; } /// /// Start this task. /// /// The list of parameters. /// A token to request the task's cancelation. /// If this task is not cancelled quickly, it might be killed by the runner. /// /// Your task can have any service as a public field and use the , /// they will be set to an available service from the service container before calling this method. /// public Task Run(TaskParameters arguments, CancellationToken cancellationToken); /// /// The list of parameters /// /// All parameters that this task as. Every one of them will be given to the run function with a value. public TaskParameters GetParameters(); /// /// If this task is running, return the percentage of completion of this task or null if no percentage can be given. /// /// The percentage of completion of the task. public int? Progress(); } }