This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
forked from StrictlySkyler/sleep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep.js
91 lines (73 loc) · 2.01 KB
/
sleep.js
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
(function init () {
'use strict';
const name = 'sleep';
return module.exports = {
render_input: function (values) {
values = values || {
seconds: 1,
exit_code: 0,
};
return `
<p>Sleep for how many seconds?</p>
<input type=text required name="seconds" value=${values.seconds}>
<p>Exit with:</p>
<label>
<input
type=radio
name=exit_code
value=0
required
${values.exit_code == 0 ? 'checked' : ''}
>
Success
</label>
<label>
<input
type=radio
name=exit_code
value=1
required
${values.exit_code == 1 ? 'checked' : ''}
>
Error
</label>
`;
},
render_work_preview: function (manifest) {
return `
<p>Sleep for <span class="pre">${manifest.seconds}</span> seconds.</p>
`;
},
register: () => name,
update: function (lane, values) {
if (values.seconds) values.seconds = parseInt(values.seconds, 10);
values.seconds = values.seconds || false;
if (typeof values.seconds == 'number') return true;
return false;
},
work: function (lane, manifest) {
let exit_code = parseInt(manifest.exit_code, 10);
if (typeof manifest.seconds != 'number') {
throw new TypeError(`
Invalid number of seconds to sleep!
The manifest must contain a "seconds" member, which must be an
integer representing the number of seconds to sleep.
`);
}
try {
let seconds = manifest.random ?
get_random_seconds(manifest.seconds) :
manifest.seconds
;
$H.setTimeout(function () {
$H.call('Lanes#end_shipment', lane, exit_code, manifest);
}, seconds * 1000);
}
catch (err) {
console.error(err);
manifest.error = err;
}
return manifest;
},
};
})();