aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.WebDashboard/Html/scripts/ScheduledTasksPage.js
blob: d822db722d0d441c145879dbd21f523a128365bc (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var ScheduledTasksPage = {

    onPageShow: function () {

        Dashboard.showLoadingMsg();

        ScheduledTasksPage.reloadList(true);

        $(document).on("websocketmessage", ScheduledTasksPage.onWebSocketMessage).on("websocketopen", ScheduledTasksPage.onWebSocketConnectionChange).on("websocketerror", ScheduledTasksPage.onWebSocketConnectionChange).on("websocketclose", ScheduledTasksPage.onWebSocketConnectionChange);
    },

    onPageHide: function () {
        $(document).off("websocketmessage", ScheduledTasksPage.onWebSocketMessage).off("websocketopen", ScheduledTasksPage.onWebSocketConnectionChange).off("websocketerror", ScheduledTasksPage.onWebSocketConnectionChange).off("websocketclose", ScheduledTasksPage.onWebSocketConnectionChange);
        ScheduledTasksPage.stopInterval();
    },

    startInterval: function () {

        if (Dashboard.isWebSocketOpen()) {
            Dashboard.sendWebSocketMessage("ScheduledTasksInfoStart", "1500,1500");
        }
    },

    stopInterval: function () {

        if (Dashboard.isWebSocketOpen()) {
            Dashboard.sendWebSocketMessage("ScheduledTasksInfoStop");
        }
    },

    onWebSocketMessage: function (e, msg) {

        if (msg.MessageType == "ScheduledTasksInfo") {
            ScheduledTasksPage.populateList(msg.Data);
        }
    },
    
    onWebSocketConnectionChange: function() {
        ScheduledTasksPage.reloadList(true);
    },

    reloadList: function (updateInterval) {

        if (updateInterval) {
            ScheduledTasksPage.stopInterval();
        }

        ApiClient.getScheduledTasks().done(function (tasks) {
            ScheduledTasksPage.populateList(tasks);
            Dashboard.hideLoadingMsg();

            if (updateInterval) {
                ScheduledTasksPage.startInterval();
            }

        });
    },

    populateList: function (tasks) {

        tasks = tasks.sort(function (a, b) {

            a = a.Category + " " + a.Name;
            b = b.Category + " " + b.Name;

            if (a == b) {
                return 0;
            }

            if (a < b) {
                return -1;
            }

            return 1;
        });

        var page = $($.mobile.activePage);

        var html = "";

        var currentCategory;

        for (var i = 0, length = tasks.length; i < length; i++) {

            var task = tasks[i];

            if (task.Category != currentCategory) {
                currentCategory = task.Category;

                html += "<li data-role='list-divider'>" + currentCategory + "</li>";
            }

            html += "<li>";

            html += "<a href='scheduledTask.html?id=" + task.Id + "'>";

            html += "<h3>" + task.Name + "</h3>";

            if (task.State == "Idle") {

                if (task.LastExecutionResult) {

                    var text = "Last run " + humane_date(task.LastExecutionResult.EndTimeUtc) + ', taking ' + humane_elapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc);

                    if (task.LastExecutionResult.Status == "Failed") {
                        text += " <span style='color:#FF0000;'>(failed)</span>";
                    }
                    else if (task.LastExecutionResult.Status == "Cancelled") {
                        text += " <span style='color:#0026FF;'>(cancelled)</span>";
                    }
                    else if (task.LastExecutionResult.Status == "Aborted") {
                        text += " <span style='color:#FF0000;'>(Aborted by server shutdown)</span>";
                    }

                    html += "<p>" + text + "</p>";
                }

                html += "<a href='#' data-icon='play' onclick='ScheduledTasksPage.startTask(\"" + task.Id + "\");'>Start</a>";
            }
            else if (task.State == "Running") {

                var progress = task.CurrentProgress || { PercentComplete: 0 };
                progress = Math.round(progress.PercentComplete);
                
                html += '<p><progress max="100" value="' + progress + '" title="' + progress + '%">';
                html += '' + progress + '%';
                html += '</progress>';

                html += "<span style='color:#009F00;margin-left:5px;'>" + progress + "%</span>";
                html += '</p>';

                html += "<a href='#' data-icon='stop' onclick='ScheduledTasksPage.stopTask(\"" + task.Id + "\");'>Stop</a>";

            } else {

                html += "<p style='color:#FF0000;'>Stopping</p>";
                html += "<a href='#' data-icon='play' style='visibility:hidden;'>Start</a>";
            }

            html += "</a>";

            html += "</li>";
        }

        $('#ulScheduledTasks', page).html(html).listview('refresh');
    },

    startTask: function (id) {

        Dashboard.showLoadingMsg();

        ApiClient.startScheduledTask(id).done(function (result) {

            ScheduledTasksPage.reloadList();
        });

    },

    stopTask: function (id) {

        Dashboard.showLoadingMsg();

        ApiClient.stopScheduledTask(id).done(function (result) {

            ScheduledTasksPage.reloadList();
        });
    }
};

$(document).on('pageshow', "#scheduledTasksPage", ScheduledTasksPage.onPageShow).on('pagehide', "#scheduledTasksPage", ScheduledTasksPage.onPageHide);