jellyfin/Jellyfin.Api/Controllers/ScheduledTasksController.cs

155 lines
5.0 KiB
C#
Raw Normal View History

2020-04-19 15:26:20 -07:00
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
2020-04-19 15:31:09 -07:00
using MediaBrowser.Controller.Net;
2020-04-19 15:26:20 -07:00
using MediaBrowser.Model.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Scheduled Tasks Controller.
/// </summary>
2020-04-21 13:23:08 -07:00
// [Authenticated]
2020-04-19 15:26:20 -07: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>
/// <returns>Task list.</returns>
[HttpGet]
2020-04-21 13:23:08 -07:00
[ProducesResponseType(StatusCodes.Status200OK)]
public IEnumerable<IScheduledTaskWorker> GetTasks(
2020-04-19 15:26:20 -07:00
[FromQuery] bool? isHidden = false,
[FromQuery] bool? isEnabled = false)
{
2020-04-21 07:02:07 -07:00
IEnumerable<IScheduledTaskWorker> tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name);
2020-04-19 15:26:20 -07:00
2020-04-21 13:23:08 -07:00
foreach (var task in tasks)
2020-04-21 07:02:07 -07:00
{
2020-04-21 13:23:08 -07:00
if (task.ScheduledTask is IConfigurableScheduledTask scheduledTask)
2020-04-19 15:26:20 -07:00
{
2020-04-21 13:23:08 -07:00
if (isHidden.HasValue && isHidden.Value != scheduledTask.IsHidden)
2020-04-19 15:26:20 -07:00
{
2020-04-21 13:23:08 -07:00
continue;
2020-04-21 07:02:07 -07:00
}
2020-04-19 15:26:20 -07:00
2020-04-21 13:23:08 -07:00
if (isEnabled.HasValue && isEnabled.Value != scheduledTask.IsEnabled)
2020-04-19 15:26:20 -07:00
{
2020-04-21 13:23:08 -07:00
continue;
2020-04-21 07:02:07 -07:00
}
2020-04-21 13:23:08 -07:00
}
2020-04-19 15:26:20 -07:00
2020-04-21 13:23:08 -07:00
yield return task;
2020-04-21 07:02:07 -07:00
}
2020-04-19 15:26:20 -07:00
}
/// <summary>
/// Get task by id.
/// </summary>
/// <param name="taskId">Task Id.</param>
/// <returns>Task Info.</returns>
[HttpGet("{TaskID}")]
2020-04-21 13:23:08 -07:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-19 15:26:20 -07:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 13:23:08 -07:00
public ActionResult<TaskInfo> GetTask([FromRoute] string taskId)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(i =>
string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase));
2020-04-19 15:26:20 -07:00
2020-04-21 07:02:07 -07:00
if (task == null)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
return NotFound();
2020-04-19 15:26:20 -07:00
}
2020-04-21 07:02:07 -07:00
var result = ScheduledTaskHelpers.GetTaskInfo(task);
return Ok(result);
2020-04-19 15:26:20 -07:00
}
/// <summary>
/// Start specified task.
/// </summary>
/// <param name="taskId">Task Id.</param>
/// <returns>Status.</returns>
[HttpPost("Running/{TaskID}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 13:23:08 -07:00
public ActionResult StartTask([FromRoute] string taskId)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
2020-04-19 15:26:20 -07:00
2020-04-21 07:02:07 -07:00
if (task == null)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
return NotFound();
2020-04-19 15:26:20 -07:00
}
2020-04-21 07:02:07 -07:00
_taskManager.Execute(task, new TaskOptions());
return Ok();
2020-04-19 15:26:20 -07:00
}
/// <summary>
/// Stop specified task.
/// </summary>
/// <param name="taskId">Task Id.</param>
/// <returns>Status.</returns>
[HttpDelete("Running/{TaskID}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 13:23:08 -07:00
public ActionResult StopTask([FromRoute] string taskId)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
2020-04-19 15:26:20 -07:00
2020-04-21 07:02:07 -07:00
if (task == null)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
return NotFound();
2020-04-19 15:26:20 -07:00
}
2020-04-21 07:02:07 -07:00
_taskManager.Cancel(task);
return Ok();
2020-04-19 15:26:20 -07:00
}
/// <summary>
/// Update specified task triggers.
/// </summary>
/// <param name="taskId">Task Id.</param>
/// <param name="triggerInfos">Triggers.</param>
/// <returns>Status.</returns>
[HttpPost("{TaskID}/Triggers")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 13:23:08 -07:00
public ActionResult UpdateTask(
2020-04-21 07:02:07 -07:00
[FromRoute] string taskId,
[FromBody, BindRequired] TaskTriggerInfo[] triggerInfos)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
if (task == null)
2020-04-19 15:26:20 -07:00
{
2020-04-21 07:02:07 -07:00
return NotFound();
2020-04-19 15:26:20 -07:00
}
2020-04-21 07:02:07 -07:00
task.Triggers = triggerInfos;
return Ok();
2020-04-19 15:26:20 -07:00
}
}
}