jellyfin/Jellyfin.Api/Controllers/ScheduledTasksController.cs

162 lines
6.1 KiB
C#
Raw Normal View History

2020-04-19 18:26:20 -04:00
using System;
using System.Collections.Generic;
2020-08-06 10:17:45 -04:00
using System.ComponentModel.DataAnnotations;
2020-04-19 18:26:20 -04:00
using System.Linq;
2020-05-19 11:06:37 -04:00
using Jellyfin.Api.Constants;
2020-04-19 18:26:20 -04:00
using MediaBrowser.Model.Tasks;
2020-05-19 11:06:37 -04:00
using Microsoft.AspNetCore.Authorization;
2020-04-19 18:26:20 -04:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Scheduled Tasks Controller.
/// </summary>
2020-05-19 11:06:37 -04:00
[Authorize(Policy = Policies.RequiresElevation)]
2020-04-19 18:26:20 -04:00
public class ScheduledTasksController : BaseJellyfinApiController
{
private readonly ITaskManager _taskManager;
/// <summary>
/// Initializes a new instance of the <see cref="ScheduledTasksController"/> class.
/// </summary>
/// <param name="taskManager">Instance of the <see cref="ITaskManager"/> interface.</param>
public ScheduledTasksController(ITaskManager taskManager)
{
_taskManager = taskManager;
}
/// <summary>
/// Get tasks.
/// </summary>
/// <param name="isHidden">Optional filter tasks that are hidden, or not.</param>
/// <param name="isEnabled">Optional filter tasks that are enabled, or not.</param>
2020-05-02 19:10:59 -04:00
/// <response code="200">Scheduled tasks retrieved.</response>
/// <returns>The list of scheduled tasks.</returns>
2020-04-19 18:26:20 -04:00
[HttpGet]
2020-04-21 16:23:08 -04:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-10-26 06:21:32 -04:00
public IEnumerable<TaskInfo> GetTasks(
2020-05-19 14:56:57 -04:00
[FromQuery] bool? isHidden,
[FromQuery] bool? isEnabled)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
IEnumerable<IScheduledTaskWorker> tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name);
2020-04-19 18:26:20 -04:00
2020-04-21 16:23:08 -04:00
foreach (var task in tasks)
2020-04-21 10:02:07 -04:00
{
2020-04-21 16:23:08 -04:00
if (task.ScheduledTask is IConfigurableScheduledTask scheduledTask)
2020-04-19 18:26:20 -04:00
{
2020-04-21 16:23:08 -04:00
if (isHidden.HasValue && isHidden.Value != scheduledTask.IsHidden)
2020-04-19 18:26:20 -04:00
{
2020-04-21 16:23:08 -04:00
continue;
2020-04-21 10:02:07 -04:00
}
2020-04-19 18:26:20 -04:00
2020-04-21 16:23:08 -04:00
if (isEnabled.HasValue && isEnabled.Value != scheduledTask.IsEnabled)
2020-04-19 18:26:20 -04:00
{
2020-04-21 16:23:08 -04:00
continue;
2020-04-21 10:02:07 -04:00
}
2020-04-21 16:23:08 -04:00
}
2020-04-19 18:26:20 -04:00
2020-10-26 06:21:32 -04:00
yield return ScheduledTaskHelpers.GetTaskInfo(task);
2020-04-21 10:02:07 -04:00
}
2020-04-19 18:26:20 -04:00
}
/// <summary>
/// Get task by id.
/// </summary>
/// <param name="taskId">Task Id.</param>
2020-05-02 19:10:59 -04:00
/// <response code="200">Task retrieved.</response>
/// <response code="404">Task not found.</response>
/// <returns>An <see cref="OkResult"/> containing the task on success, or a <see cref="NotFoundResult"/> if the task could not be found.</returns>
2020-06-04 17:17:05 -04:00
[HttpGet("{taskId}")]
2020-04-21 16:23:08 -04:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-19 18:26:20 -04:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-09-07 20:45:06 -04:00
public ActionResult<TaskInfo> GetTask([FromRoute, Required] string taskId)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(i =>
string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase));
2020-04-19 18:26:20 -04:00
2020-04-21 10:02:07 -04:00
if (task == null)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
return NotFound();
2020-04-19 18:26:20 -04:00
}
2020-04-21 10:02:07 -04:00
2020-05-19 11:06:37 -04:00
return ScheduledTaskHelpers.GetTaskInfo(task);
2020-04-19 18:26:20 -04:00
}
/// <summary>
/// Start specified task.
/// </summary>
/// <param name="taskId">Task Id.</param>
2020-06-20 17:11:10 -04:00
/// <response code="204">Task started.</response>
2020-05-02 19:10:59 -04:00
/// <response code="404">Task not found.</response>
2020-06-20 17:11:10 -04:00
/// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found.</returns>
2020-06-04 17:17:05 -04:00
[HttpPost("Running/{taskId}")]
2020-06-20 17:16:30 -04:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-04-19 18:26:20 -04:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-09-07 20:45:06 -04:00
public ActionResult StartTask([FromRoute, Required] string taskId)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
2020-04-19 18:26:20 -04:00
2020-04-21 10:02:07 -04:00
if (task == null)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
return NotFound();
2020-04-19 18:26:20 -04:00
}
2020-04-21 10:02:07 -04:00
_taskManager.Execute(task, new TaskOptions());
2020-06-20 17:11:10 -04:00
return NoContent();
2020-04-19 18:26:20 -04:00
}
/// <summary>
/// Stop specified task.
/// </summary>
/// <param name="taskId">Task Id.</param>
2020-06-20 17:11:10 -04:00
/// <response code="204">Task stopped.</response>
2020-05-02 19:10:59 -04:00
/// <response code="404">Task not found.</response>
/// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found.</returns>
2020-06-04 17:17:05 -04:00
[HttpDelete("Running/{taskId}")]
2020-06-20 17:16:30 -04:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-04-19 18:26:20 -04:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-09-07 20:45:06 -04:00
public ActionResult StopTask([FromRoute, Required] string taskId)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
2020-04-19 18:26:20 -04:00
2020-04-21 10:02:07 -04:00
if (task == null)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
return NotFound();
2020-04-19 18:26:20 -04:00
}
2020-04-21 10:02:07 -04:00
_taskManager.Cancel(task);
2020-06-20 17:11:10 -04:00
return NoContent();
2020-04-19 18:26:20 -04:00
}
/// <summary>
/// Update specified task triggers.
/// </summary>
/// <param name="taskId">Task Id.</param>
/// <param name="triggerInfos">Triggers.</param>
2020-06-20 17:11:10 -04:00
/// <response code="204">Task triggers updated.</response>
2020-05-02 19:10:59 -04:00
/// <response code="404">Task not found.</response>
/// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found.</returns>
2020-06-04 17:17:05 -04:00
[HttpPost("{taskId}/Triggers")]
2020-06-20 17:16:30 -04:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-04-19 18:26:20 -04:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 16:23:08 -04:00
public ActionResult UpdateTask(
2020-09-07 20:45:06 -04:00
[FromRoute, Required] string taskId,
2020-08-06 10:17:45 -04:00
[FromBody, Required] TaskTriggerInfo[] triggerInfos)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
if (task == null)
2020-04-19 18:26:20 -04:00
{
2020-04-21 10:02:07 -04:00
return NotFound();
2020-04-19 18:26:20 -04:00
}
2020-04-21 10:02:07 -04:00
task.Triggers = triggerInfos;
2020-06-20 17:11:10 -04:00
return NoContent();
2020-04-19 18:26:20 -04:00
}
}
}