aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/ScheduledTasksController.cs
blob: da7cfbc3a7bb101a511ffd67a7bf4e6e4415e5d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Controller.Net;
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>
    // [Authenticated]
    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]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IEnumerable<IScheduledTaskWorker> GetTasks(
            [FromQuery] bool? isHidden = false,
            [FromQuery] bool? isEnabled = false)
        {
            IEnumerable<IScheduledTaskWorker> tasks = _taskManager.ScheduledTasks.OrderBy(o => o.Name);

            foreach (var task in tasks)
            {
                if (task.ScheduledTask is IConfigurableScheduledTask scheduledTask)
                {
                    if (isHidden.HasValue && isHidden.Value != scheduledTask.IsHidden)
                    {
                        continue;
                    }

                    if (isEnabled.HasValue && isEnabled.Value != scheduledTask.IsEnabled)
                    {
                        continue;
                    }
                }

                yield return task;
            }
        }

        /// <summary>
        /// Get task by id.
        /// </summary>
        /// <param name="taskId">Task Id.</param>
        /// <returns>Task Info.</returns>
        [HttpGet("{TaskID}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult<TaskInfo> GetTask([FromRoute] string taskId)
        {
            var task = _taskManager.ScheduledTasks.FirstOrDefault(i =>
                string.Equals(i.Id, taskId, StringComparison.OrdinalIgnoreCase));

            if (task == null)
            {
                return NotFound();
            }

            var result = ScheduledTaskHelpers.GetTaskInfo(task);
            return Ok(result);
        }

        /// <summary>
        /// Start specified task.
        /// </summary>
        /// <param name="taskId">Task Id.</param>
        /// <returns>Status.</returns>
        [HttpPost("Running/{TaskID}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult StartTask([FromRoute] string taskId)
        {
            var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
                o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));

            if (task == null)
            {
                return NotFound();
            }

            _taskManager.Execute(task, new TaskOptions());
            return Ok();
        }

        /// <summary>
        /// Stop specified task.
        /// </summary>
        /// <param name="taskId">Task Id.</param>
        /// <returns>Status.</returns>
        [HttpDelete("Running/{TaskID}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public ActionResult StopTask([FromRoute] string taskId)
        {
            var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
                o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));

            if (task == null)
            {
                return NotFound();
            }

            _taskManager.Cancel(task);
            return Ok();
        }

        /// <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)]
        public ActionResult UpdateTask(
            [FromRoute] string taskId,
            [FromBody, BindRequired] TaskTriggerInfo[] triggerInfos)
        {
            var task = _taskManager.ScheduledTasks.FirstOrDefault(o =>
                o.Id.Equals(taskId, StringComparison.OrdinalIgnoreCase));
            if (task == null)
            {
                return NotFound();
            }

            task.Triggers = triggerInfos;
            return Ok();
        }
    }
}