< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.ScheduledTasksController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/ScheduledTasksController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 26
Coverable lines: 26
Total lines: 161
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetTask(...)0%620%
StartTask(...)0%620%
StopTask(...)0%620%
UpdateTask(...)0%620%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/ScheduledTasksController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel.DataAnnotations;
 4using System.Linq;
 5using Jellyfin.Api.Constants;
 6using MediaBrowser.Common.Api;
 7using MediaBrowser.Model.Tasks;
 8using Microsoft.AspNetCore.Authorization;
 9using Microsoft.AspNetCore.Http;
 10using Microsoft.AspNetCore.Mvc;
 11
 12namespace Jellyfin.Api.Controllers;
 13
 14/// <summary>
 15/// Scheduled Tasks Controller.
 16/// </summary>
 17[Authorize(Policy = Policies.RequiresElevation)]
 18public class ScheduledTasksController : BaseJellyfinApiController
 19{
 20    private readonly ITaskManager _taskManager;
 21
 22    /// <summary>
 23    /// Initializes a new instance of the <see cref="ScheduledTasksController"/> class.
 24    /// </summary>
 25    /// <param name="taskManager">Instance of the <see cref="ITaskManager"/> interface.</param>
 026    public ScheduledTasksController(ITaskManager taskManager)
 27    {
 028        _taskManager = taskManager;
 029    }
 30
 31    /// <summary>
 32    /// Get tasks.
 33    /// </summary>
 34    /// <param name="isHidden">Optional filter tasks that are hidden, or not.</param>
 35    /// <param name="isEnabled">Optional filter tasks that are enabled, or not.</param>
 36    /// <response code="200">Scheduled tasks retrieved.</response>
 37    /// <returns>The list of scheduled tasks.</returns>
 38    [HttpGet]
 39    [ProducesResponseType(StatusCodes.Status200OK)]
 40    public IEnumerable<TaskInfo> GetTasks(
 41        [FromQuery] bool? isHidden,
 42        [FromQuery] bool? isEnabled)
 43    {
 44        IEnumerable<IScheduledTaskWorker> tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name);
 45
 46        foreach (var task in tasks)
 47        {
 48            if (task.ScheduledTask is IConfigurableScheduledTask scheduledTask)
 49            {
 50                if (isHidden.HasValue && isHidden.Value != scheduledTask.IsHidden)
 51                {
 52                    continue;
 53                }
 54
 55                if (isEnabled.HasValue && isEnabled.Value != scheduledTask.IsEnabled)
 56                {
 57                    continue;
 58                }
 59            }
 60
 61            yield return ScheduledTaskHelpers.GetTaskInfo(task);
 62        }
 63    }
 64
 65    /// <summary>
 66    /// Get task by id.
 67    /// </summary>
 68    /// <param name="taskId">Task Id.</param>
 69    /// <response code="200">Task retrieved.</response>
 70    /// <response code="404">Task not found.</response>
 71    /// <returns>An <see cref="OkResult"/> containing the task on success, or a <see cref="NotFoundResult"/> if the task
 72    [HttpGet("{taskId}")]
 73    [ProducesResponseType(StatusCodes.Status200OK)]
 74    [ProducesResponseType(StatusCodes.Status404NotFound)]
 75    public ActionResult<TaskInfo> GetTask([FromRoute, Required] string taskId)
 76    {
 077        var task = _taskManager.ScheduledTasks.FirstOrDefault(i =>
 078            string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase));
 79
 080        if (task is null)
 81        {
 082            return NotFound();
 83        }
 84
 085        return ScheduledTaskHelpers.GetTaskInfo(task);
 86    }
 87
 88    /// <summary>
 89    /// Start specified task.
 90    /// </summary>
 91    /// <param name="taskId">Task Id.</param>
 92    /// <response code="204">Task started.</response>
 93    /// <response code="404">Task not found.</response>
 94    /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be
 95    [HttpPost("Running/{taskId}")]
 96    [ProducesResponseType(StatusCodes.Status204NoContent)]
 97    [ProducesResponseType(StatusCodes.Status404NotFound)]
 98    public ActionResult StartTask([FromRoute, Required] string taskId)
 99    {
 0100        var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
 0101            o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
 102
 0103        if (task is null)
 104        {
 0105            return NotFound();
 106        }
 107
 0108        _taskManager.Execute(task, new TaskOptions());
 0109        return NoContent();
 110    }
 111
 112    /// <summary>
 113    /// Stop specified task.
 114    /// </summary>
 115    /// <param name="taskId">Task Id.</param>
 116    /// <response code="204">Task stopped.</response>
 117    /// <response code="404">Task not found.</response>
 118    /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found.
 119    [HttpDelete("Running/{taskId}")]
 120    [ProducesResponseType(StatusCodes.Status204NoContent)]
 121    [ProducesResponseType(StatusCodes.Status404NotFound)]
 122    public ActionResult StopTask([FromRoute, Required] string taskId)
 123    {
 0124        var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
 0125            o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
 126
 0127        if (task is null)
 128        {
 0129            return NotFound();
 130        }
 131
 0132        _taskManager.Cancel(task);
 0133        return NoContent();
 134    }
 135
 136    /// <summary>
 137    /// Update specified task triggers.
 138    /// </summary>
 139    /// <param name="taskId">Task Id.</param>
 140    /// <param name="triggerInfos">Triggers.</param>
 141    /// <response code="204">Task triggers updated.</response>
 142    /// <response code="404">Task not found.</response>
 143    /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found.
 144    [HttpPost("{taskId}/Triggers")]
 145    [ProducesResponseType(StatusCodes.Status204NoContent)]
 146    [ProducesResponseType(StatusCodes.Status404NotFound)]
 147    public ActionResult UpdateTask(
 148        [FromRoute, Required] string taskId,
 149        [FromBody, Required] TaskTriggerInfo[] triggerInfos)
 150    {
 0151        var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
 0152            o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
 0153        if (task is null)
 154        {
 0155            return NotFound();
 156        }
 157
 0158        task.Triggers = triggerInfos;
 0159        return NoContent();
 160    }
 161}