-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathJobs.js
More file actions
317 lines (278 loc) · 10.5 KB
/
Jobs.js
File metadata and controls
317 lines (278 loc) · 10.5 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* global */
import {Character} from "../Character.js";
import {Panel} from "./Panel.js";
import {Utils} from "../Utils.js";
export class JobsPanel extends Panel {
onShow (cnt) {
// collect the list of hidden/shown functions
this._showJobs = Utils.getStorageItemList("session", "show_jobs");
this._hideJobs = Utils.getStorageItemList("session", "hide_jobs");
const runnerJobsListJobsPromise = this.api.getRunnerJobsListJobs();
const runnerJobsActivePromise = this.api.getRunnerJobsActive();
runnerJobsListJobsPromise.then((pRunnerJobsListJobsData) => {
this._handleRunnerJobsListJobs(pRunnerJobsListJobsData, cnt);
runnerJobsActivePromise.then((pRunnerJobsActiveData) => {
this._handleRunnerJobsActive(pRunnerJobsActiveData);
return true;
}, (pRunnerJobsActiveMsg) => {
this._handleRunnerJobsActive(JSON.stringify(pRunnerJobsActiveMsg));
return false;
});
return true;
}, (pRunnerJobsListJobsMsg) => {
this._handleRunnerJobsListJobs(JSON.stringify(pRunnerJobsListJobsMsg));
Utils.ignorePromise(runnerJobsActivePromise);
return false;
});
}
startRunningJobs () {
const runnerJobsActivePromise = this.api.getRunnerJobsActive();
runnerJobsActivePromise.then((pRunnerJobsActiveData) => {
this._handleRunnerJobsActive(pRunnerJobsActiveData);
return true;
}, (pRunnerJobsActiveMsg) => {
this._handleRunnerJobsActive(JSON.stringify(pRunnerJobsActiveMsg));
return false;
});
}
_handleRunnerJobsActive (pData) {
if (!pData) {
return;
}
const tbody = this.table.tBodies[0];
if (typeof pData !== "object") {
// update all jobs (page) with the error message
for (const tr of tbody.rows) {
const statusField = tr.querySelector("span.no-job-status");
if (!statusField) {
continue;
}
Utils.addErrorToTableCell(statusField.parentElement, pData);
}
return;
}
const jobs = pData.return[0];
// update all running jobs
for (const jobId in jobs) {
const job = jobs[jobId];
let targetText = "";
const targetField = this.table.querySelector("tr#" + Utils.getIdFromJobId(jobId) + " span.job-status");
const maxTextLength = 50;
if (targetText.length > maxTextLength) {
// prevent column becoming too wide
// yes, the addition of running/returned may again make
// the string longer than 50 characters, we accept that
targetText = targetText.substring(0, maxTextLength) + Character.HORIZONTAL_ELLIPSIS;
}
// then add the operational statistics
if (job.Running && job.Running.length > 0) {
targetText += job.Running.length + " running";
}
if (job.Returned && job.Returned.length > 0) {
targetText += ", " + job.Returned.length + " returned";
}
// the field may not (yet) be on the screen
if (!targetField) {
continue;
}
targetField.classList.remove("no-job-status");
targetField.innerText = targetText;
targetField.insertBefore(Utils.createJobStatusSpan(jobId, true), targetField.firstChild);
Utils.addToolTip(targetField, "Click to refresh column");
}
// update all finished jobs (page)
for (const tr of tbody.rows) {
const statusField = tr.querySelector("span.no-job-status");
if (!statusField) {
continue;
}
statusField.classList.remove("no-job-status");
statusField.innerText = "done";
// we show the tooltip here so that the user is invited to click on this
// the user then sees other rows being updated without becoming invisible
Utils.addToolTip(statusField, "Click to refresh column");
// remove the refresh button when it is still there
// needed when not all minions have reported their status
// but also there are no jobs running anymore
const detailsField = tr.querySelector("#status" + tr.dataset.jobid);
if (detailsField) {
detailsField.remove();
}
}
}
_handleRunnerJobsListJobs (pData, pMaxNumberOfJobs = 7) {
if (this.showErrorRowInstead(pData)) {
return;
}
const jobs = JobsPanel._jobsToArray(pData.return[0]);
JobsPanel._sortJobs(jobs);
// These jobs are likely started by the SaltGUI
// do not display them
this._hideJobs.push("beacons.add");
this._hideJobs.push("beacons.delete");
this._hideJobs.push("beacons.disable");
this._hideJobs.push("beacons.disable_beacon");
this._hideJobs.push("beacons.enable");
this._hideJobs.push("beacons.enable_beacon");
this._hideJobs.push("beacons.list");
this._hideJobs.push("beacons.list_available");
this._hideJobs.push("beacons.modify");
this._hideJobs.push("beacons.reset");
this._hideJobs.push("beacons.save");
this._hideJobs.push("grains.append");
this._hideJobs.push("grains.delkey");
this._hideJobs.push("grains.delval");
this._hideJobs.push("grains.items");
this._hideJobs.push("grains.setval");
this._hideJobs.push("pillar.items");
this._hideJobs.push("pillar.obfuscate");
this._hideJobs.push("ps.kill_pid");
this._hideJobs.push("ps.proc_info");
this._hideJobs.push("test.providers");
this._hideJobs.push("test.version");
this._hideJobs.push("saltutil.find_job");
this._hideJobs.push("saltutil.kill_job");
this._hideJobs.push("saltutil.refresh_grains");
this._hideJobs.push("saltutil.refresh_pillar");
this._hideJobs.push("saltutil.running");
this._hideJobs.push("saltutil.signal_job");
this._hideJobs.push("saltutil.term_job");
this._hideJobs.push("schedule.add");
this._hideJobs.push("schedule.delete");
this._hideJobs.push("schedule.disable");
this._hideJobs.push("schedule.disable_job");
this._hideJobs.push("schedule.enable");
this._hideJobs.push("schedule.enable_job");
this._hideJobs.push("schedule.list");
this._hideJobs.push("schedule.modify");
this._hideJobs.push("schedule.run_job");
this._hideJobs.push("sys.doc");
// runner jobs
this._hideJobs.push("runner.cache.grains");
this._hideJobs.push("runner.cache.pillar");
this._hideJobs.push("runner.doc.runner");
this._hideJobs.push("runner.doc.wheel");
this._hideJobs.push("runner.jobs.active");
this._hideJobs.push("runner.jobs.list_job");
this._hideJobs.push("runner.jobs.list_jobs");
this._hideJobs.push("runner.manage.versions");
// do not hide "runner.state.orchestrate"
this._hideJobs.push("runner.state.orchestrate_show_sls");
// wheel jobs
this._hideJobs.push("wheel.config.values");
this._hideJobs.push("wheel.key.accept");
this._hideJobs.push("wheel.key.delete");
this._hideJobs.push("wheel.key.finger");
this._hideJobs.push("wheel.key.list_all");
this._hideJobs.push("wheel.key.reject");
this._hideJobs.push("wheel.minions.connected");
// this was automatically executed by really old minions
this._hideJobs.push("mine.update");
let numberOfJobsShown = 0;
let numberOfJobsEligible = 0;
const numberOfJobsPresent = jobs.length;
for (const job of jobs) {
if (Utils.isIncluded(job.Function, this._showJobs, this._hideJobs)) {
numberOfJobsEligible += 1;
} else if (pMaxNumberOfJobs !== 99999) {
continue;
}
// Add only <pMaxNumberOfJobs> most recent jobs
if (numberOfJobsShown >= pMaxNumberOfJobs) {
continue;
}
// Note that "addJob" has a specialized version
// in each of the subclasses
this.addJob(job);
numberOfJobsShown += 1;
}
this.numberOfJobsShown = numberOfJobsShown;
this.numberOfJobsEligible = numberOfJobsEligible;
this.numberOfJobsPresent = numberOfJobsPresent;
this.setPlayPauseButton(numberOfJobsShown === 0 ? "none" : "play");
this.updateFooter();
this.jobsListIsReady();
}
updateFooter () {
let txt = Utils.txtZeroOneMany(this.numberOfJobsShown,
"No jobs shown", "{0} job shown", "{0} jobs shown");
if (this.numberOfJobsEligible > 0 && this.numberOfJobsEligible > this.numberOfJobsShown) {
txt += Utils.txtZeroOneMany(this.numberOfJobsEligible,
"", ", {0} job eligible", ", {0} jobs eligible");
}
txt += Utils.txtZeroOneMany(this.numberOfJobsPresent,
"", ", {0} job present", ", {0} jobs present");
this.setMsg(txt);
}
static _jobsToArray (jobs) {
if (typeof jobs === "string") {
// typically when special returner is misconfigured
// the warning may help solve that too
/* eslint-disable no-console */
console.warn(jobs);
/* eslint-enable no-console */
return [];
}
const keys = Object.keys(jobs);
const newArray = [];
for (const key of keys) {
const job = jobs[key];
job.id = key;
newArray.push(job);
}
return newArray;
}
static _sortJobs (jobs) {
jobs.sort((aa, bb) => {
// The id is already a string value based on the date,
// let's use it to sort the jobs
/* eslint-disable curly */
if (aa.id < bb.id) return 1;
if (aa.id > bb.id) return -1;
/* eslint-enable curly */
return 0;
});
}
handleSaltJobRetEvent (pData) {
// ignore the most common events until someone complains
if (pData.fun === "saltutil.find_job") {
return;
}
if (pData.fun === "saltutil.running") {
return;
}
// { fun_args: […], jid: "20190704194624366796", return: true, retcode: 0, success: true, cmd: "_return", fun: "test.rand_sleep", id: "autobuild-it-4092", _stamp: "2019-07-04T17:46:28.448689" }
const jid = pData.jid;
if (!jid) {
return;
}
let newLevel = 0;
if (pData.success === true && pData.retcode === 0) {
newLevel = 1;
} else if (pData.success === true) {
newLevel = 2;
} else {
newLevel = 3;
}
// This element only exists when the user happens to look at the output of that jobId.
const spans = this.div.querySelectorAll("#status" + jid);
for (const span of spans) {
let oldLevel = span.dataset.level;
if (oldLevel === undefined) {
oldLevel = 0;
}
if (newLevel > oldLevel) {
span.dataset.level = newLevel;
span.classList.remove("text-success", "text-warning", "text-error");
if (newLevel === 1) {
span.classList.add("text-success");
} else if (newLevel === 2) {
span.classList.add("text-warning");
} else if (newLevel === 3) {
span.classList.add("text-error");
}
}
span.style.removeProperty("display");
}
}
}