testaro 35.0.4 → 35.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -6
- package/dirWatch.js +9 -7
- package/package.json +1 -1
- package/testaro/focVis.js +24 -19
- package/tests/aslint.js +1 -1
- package/tests/ed11y.js +1 -1
- package/tests/wave.js +1 -1
- package/watch-old.js +0 -77
- package/watch.js +0 -396
package/README.md
CHANGED
|
@@ -718,12 +718,6 @@ The validity criterion named in item 2 may be any of these:
|
|
|
718
718
|
|
|
719
719
|
Testaro can be called by modules and by users.
|
|
720
720
|
|
|
721
|
-
### Functions
|
|
722
|
-
|
|
723
|
-
Testaro contains these modules that export executable functions:
|
|
724
|
-
- `run.js` exports `doJob` for immediate execution.
|
|
725
|
-
- `watch.js` exports `cycle` for watch-triggered execution.
|
|
726
|
-
|
|
727
721
|
#### Imports
|
|
728
722
|
|
|
729
723
|
Before a module can execute a Testaro function, it must import that function from the module that exports it. A Testaro module can import function `f` from module `m` with the statement
|
package/dirWatch.js
CHANGED
|
@@ -59,12 +59,13 @@ const writeDirReport = async report => {
|
|
|
59
59
|
try {
|
|
60
60
|
const reportJSON = JSON.stringify(report, null, 2);
|
|
61
61
|
const reportName = `${jobID}.json`;
|
|
62
|
-
|
|
63
|
-
await fs.
|
|
64
|
-
|
|
62
|
+
const rawDir = `${reportDir}/raw`;
|
|
63
|
+
await fs.mkdir(rawDir, {recursive: true});
|
|
64
|
+
await fs.writeFile(`${rawDir}/${reportName}`, `${reportJSON}\n`);
|
|
65
|
+
console.log(`Report ${jobID} saved in ${rawDir}`);
|
|
65
66
|
}
|
|
66
67
|
catch(error) {
|
|
67
|
-
console.log(`ERROR: Failed to save report ${jobID} in ${
|
|
68
|
+
console.log(`ERROR: Failed to save report ${jobID} in ${rawDir} (${error.message})`);
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
71
|
else {
|
|
@@ -76,14 +77,15 @@ const archiveJob = async (job, isFile) => {
|
|
|
76
77
|
// Save the job in the done subdirectory.
|
|
77
78
|
const {id} = job;
|
|
78
79
|
const jobJSON = JSON.stringify(job, null, 2);
|
|
79
|
-
|
|
80
|
-
await fs.
|
|
80
|
+
const doneDir = `${jobDir}/done`;
|
|
81
|
+
await fs.mkdir(doneDir, {recursive: true});
|
|
82
|
+
await fs.writeFile(`${doneDir}/${id}.json`, `${jobJSON}\n`);
|
|
81
83
|
// If the job had been saved as a file in the todo subdirectory:
|
|
82
84
|
if (isFile) {
|
|
83
85
|
// Delete the file.
|
|
84
86
|
await fs.rm(`${jobDir}/todo/${id}.json`);
|
|
85
87
|
}
|
|
86
|
-
console.log(`Job ${id} archived in ${
|
|
88
|
+
console.log(`Job ${id} archived in ${doneDir} (${nowString()})`);
|
|
87
89
|
};
|
|
88
90
|
// Waits.
|
|
89
91
|
const wait = ms => {
|
package/package.json
CHANGED
package/testaro/focVis.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
© 2022–
|
|
2
|
+
© 2022–2024 CVS Health and/or one of its affiliates. All rights reserved.
|
|
3
3
|
|
|
4
4
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
5
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -40,25 +40,30 @@ exports.reporter = async (page, withItems) => {
|
|
|
40
40
|
// For each locator:
|
|
41
41
|
for (const loc of all.allLocs) {
|
|
42
42
|
// Get how its element violates the rule, if it does.
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
try {
|
|
44
|
+
const isBad = await loc.evaluate(el => {
|
|
45
|
+
const isAbove = el.offsetTop + el.offsetHeight <= 0;
|
|
46
|
+
const isLeft = el.offsetLeft + el.offsetWidth <= 0;
|
|
47
|
+
return [isAbove, isLeft];
|
|
48
|
+
}, {timeout: 500});
|
|
49
|
+
// If it does:
|
|
50
|
+
if (isBad[0] || isBad[1]) {
|
|
51
|
+
// Add the locator to the array of violators.
|
|
52
|
+
let param;
|
|
53
|
+
if (isBad[0] && isBad[1]) {
|
|
54
|
+
param = 'above and to the left of';
|
|
55
|
+
}
|
|
56
|
+
else if (isBad[0]) {
|
|
57
|
+
param = 'above';
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
param = 'to the left of';
|
|
61
|
+
}
|
|
62
|
+
all.locs.push([loc, param]);
|
|
54
63
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
param = 'to the left of';
|
|
60
|
-
}
|
|
61
|
-
all.locs.push([loc, param]);
|
|
64
|
+
}
|
|
65
|
+
catch(error) {
|
|
66
|
+
console.log(`ERROR analyzing locator for focVis (${error.message})`);
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
// Populate and return the result.
|
package/tests/aslint.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
© 2023 CVS Health and/or one of its affiliates. All rights reserved.
|
|
2
|
+
© 2023–2024 CVS Health and/or one of its affiliates. All rights reserved.
|
|
3
3
|
|
|
4
4
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
5
|
of this software and associated documentation files (the "Software"), to deal
|
package/tests/ed11y.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
© 2023 CVS Health and/or one of its affiliates. All rights reserved.
|
|
2
|
+
© 2023–2024 CVS Health and/or one of its affiliates. All rights reserved.
|
|
3
3
|
|
|
4
4
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
5
|
of this software and associated documentation files (the "Software"), to deal
|
package/tests/wave.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
© 2021–
|
|
2
|
+
© 2021–2024 CVS Health and/or one of its affiliates. All rights reserved.
|
|
3
3
|
|
|
4
4
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
5
|
of this software and associated documentation files (the "Software"), to deal
|
package/watch-old.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
© 2023 CVS Health and/or one of its affiliates. All rights reserved.
|
|
3
|
-
|
|
4
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
in the Software without restriction, including without limitation the rights
|
|
7
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
furnished to do so, subject to the following conditions:
|
|
10
|
-
|
|
11
|
-
The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
copies or substantial portions of the Software.
|
|
13
|
-
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/*
|
|
24
|
-
dirWatch.js
|
|
25
|
-
Module for launching a one-time directory watch.
|
|
26
|
-
Argument:
|
|
27
|
-
1: interval in seconds from a no-job check to the next check.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
// ########## IMPORTS
|
|
31
|
-
|
|
32
|
-
// Module to spawn a child process.
|
|
33
|
-
const {spawn} = require('node:child_process');
|
|
34
|
-
|
|
35
|
-
// ########## CONSTANTS
|
|
36
|
-
|
|
37
|
-
const repeat = process.argv[2];
|
|
38
|
-
const interval = process.argv[3];
|
|
39
|
-
|
|
40
|
-
// ########## FUNCTIONS
|
|
41
|
-
|
|
42
|
-
// Spawns a one-time directory watch.
|
|
43
|
-
const spawnWatch = () => spawn(
|
|
44
|
-
'node', 'dirWatch', 'false', interval, {stdio: ['inherit', 'inherit', 'pipe']}
|
|
45
|
-
);
|
|
46
|
-
// Repeatedly spawns a one-time directory watch.
|
|
47
|
-
const reWatch = () => {
|
|
48
|
-
const watcher = spawnWatch('node', ['call', 'dirWatch', 'false', interval]);
|
|
49
|
-
let error = '';
|
|
50
|
-
watcher.stderr.on('data', data => {
|
|
51
|
-
error += data.toString();
|
|
52
|
-
});
|
|
53
|
-
watcher.on('close', async code => {
|
|
54
|
-
if (error) {
|
|
55
|
-
if (error.startsWith('Navigation timeout of 30000 ms exceeded')) {
|
|
56
|
-
console.log('ERROR: Playwright claims 30-second timeout exceeded');
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
console.log(`ERROR watching: ${error.slice(0, 200)}`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (! error && code === 0) {
|
|
63
|
-
console.log('Watcher exited successfully\n');
|
|
64
|
-
reWatch();
|
|
65
|
-
}
|
|
66
|
-
else if (code) {
|
|
67
|
-
console.log(`Watcher exited with error code ${code}`);
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
console.log('Watch aborted');
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// ########## OPERATION
|
|
76
|
-
|
|
77
|
-
reWatch();
|
package/watch.js
DELETED
|
@@ -1,396 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
© 2022–2024 CVS Health and/or one of its affiliates. All rights reserved.
|
|
3
|
-
|
|
4
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
in the Software without restriction, including without limitation the rights
|
|
7
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
furnished to do so, subject to the following conditions:
|
|
10
|
-
|
|
11
|
-
The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
copies or substantial portions of the Software.
|
|
13
|
-
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/*
|
|
24
|
-
watch.js
|
|
25
|
-
Module for watching for a job and running it when found.
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
// ########## IMPORTS
|
|
29
|
-
|
|
30
|
-
// Module to keep secrets.
|
|
31
|
-
require('dotenv').config();
|
|
32
|
-
// Module to read and write files.
|
|
33
|
-
const fs = require('fs/promises');
|
|
34
|
-
// Module to make requests to servers.
|
|
35
|
-
const httpClient = require('http');
|
|
36
|
-
const httpsClient = require('https');
|
|
37
|
-
// Module to perform jobs.
|
|
38
|
-
const {doJob} = require('./run');
|
|
39
|
-
|
|
40
|
-
// CONSTANTS
|
|
41
|
-
|
|
42
|
-
const jobDir = process.env.JOBDIR;
|
|
43
|
-
const jobURLs = process.env.JOB_URLS;
|
|
44
|
-
const reportDir = process.env.REPORTDIR;
|
|
45
|
-
const agent = process.env.AGENT;
|
|
46
|
-
|
|
47
|
-
// ########## FUNCTIONS
|
|
48
|
-
|
|
49
|
-
// Returns a string representing the date and time.
|
|
50
|
-
const nowString = () => (new Date()).toISOString().slice(2, 16);
|
|
51
|
-
// Waits.
|
|
52
|
-
const wait = ms => {
|
|
53
|
-
return new Promise(resolve => {
|
|
54
|
-
setTimeout(() => {
|
|
55
|
-
resolve('');
|
|
56
|
-
}, ms);
|
|
57
|
-
});
|
|
58
|
-
};
|
|
59
|
-
// Serves an object in JSON format.
|
|
60
|
-
const serveObject = (object, response) => {
|
|
61
|
-
response.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
62
|
-
response.end(JSON.stringify(object));
|
|
63
|
-
};
|
|
64
|
-
// Writes a directory report.
|
|
65
|
-
const writeDirReport = async report => {
|
|
66
|
-
const jobID = report && report.id;
|
|
67
|
-
if (jobID) {
|
|
68
|
-
try {
|
|
69
|
-
const reportJSON = JSON.stringify(report, null, 2);
|
|
70
|
-
const reportName = `${jobID}.json`;
|
|
71
|
-
await fs.mkdir(reportDir, {recursive: true});
|
|
72
|
-
await fs.writeFile(`${reportDir}/${reportName}`, reportJSON);
|
|
73
|
-
console.log(`Report ${reportName} saved in ${reportDir}`);
|
|
74
|
-
}
|
|
75
|
-
catch(error) {
|
|
76
|
-
console.log(`ERROR: Failed to write report ${jobID} in ${reportDir} (${error.message})`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
console.log('ERROR: Job has no ID');
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
// Archives a job.
|
|
84
|
-
const archiveJob = async (job, isFile) => {
|
|
85
|
-
// Save the job in the done subdirectory.
|
|
86
|
-
const {id} = job;
|
|
87
|
-
const jobJSON = JSON.stringify(job, null, 2);
|
|
88
|
-
await fs.mkdir(`${jobDir}/done`, {recursive: true});
|
|
89
|
-
await fs.writeFile(`${jobDir}/done/${id}.json`, jobJSON);
|
|
90
|
-
// If the job had been saved as a file in the todo subdirectory:
|
|
91
|
-
if (isFile) {
|
|
92
|
-
// Delete the file.
|
|
93
|
-
await fs.rm(`${jobDir}/todo/${id}.json`);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
// Checks for a directory job and, if found, performs and reports it, once or repeatedly.
|
|
97
|
-
const checkDirJob = async (isForever, interval) => {
|
|
98
|
-
try {
|
|
99
|
-
// If there are any jobs in the watched directory:
|
|
100
|
-
const toDoFileNames = await fs.readdir(`${jobDir}/todo`);
|
|
101
|
-
const jobFileNames = toDoFileNames.filter(fileName => fileName.endsWith('.json'));
|
|
102
|
-
if (jobFileNames.length) {
|
|
103
|
-
// If the first one is ready to do:
|
|
104
|
-
const firstJobTime = jobFileNames[0].replace(/-.+$/, '');
|
|
105
|
-
if (Date.now() > dateOf(firstJobTime)) {
|
|
106
|
-
// Perform it.
|
|
107
|
-
const jobJSON = await fs.readFile(`${jobDir}/todo/${jobFileNames[0]}`, 'utf8');
|
|
108
|
-
try {
|
|
109
|
-
const job = JSON.parse(jobJSON, null, 2);
|
|
110
|
-
const {id} = job;
|
|
111
|
-
console.log(`Directory job ${id} found (${nowString()})`);
|
|
112
|
-
await doJob(job);
|
|
113
|
-
console.log(`Job ${id} finished (${nowString()})`);
|
|
114
|
-
// Report it.
|
|
115
|
-
await writeDirReport(job);
|
|
116
|
-
// Archive it.
|
|
117
|
-
await archiveJob(job, true);
|
|
118
|
-
console.log(`Job ${id} archived in ${jobDir} (${nowString()})`);
|
|
119
|
-
// If watching is repetitive:
|
|
120
|
-
if (isForever) {
|
|
121
|
-
// Wait 2 seconds.
|
|
122
|
-
await wait(2000);
|
|
123
|
-
// Check the directory again.
|
|
124
|
-
checkDirJob(true, interval);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
catch(error) {
|
|
128
|
-
console.log(`ERROR processing directory job (${error.message})`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
// Otherwise, i.e. if the first one is not yet ready to do:
|
|
132
|
-
else {
|
|
133
|
-
// Report this.
|
|
134
|
-
console.log(`All jobs in ${jobDir} not yet ready to do (${nowString()})`);
|
|
135
|
-
// Wait for the specified interval.
|
|
136
|
-
await wait(1000 * interval);
|
|
137
|
-
// Check the directory again.
|
|
138
|
-
await checkDirJob(true, interval);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
// Otherwise, i.e. if there are no more jobs in the watched directory:
|
|
142
|
-
else {
|
|
143
|
-
console.log(`No job in ${jobDir} (${nowString()})`);
|
|
144
|
-
// Wait for the specified interval.
|
|
145
|
-
await wait(1000 * interval);
|
|
146
|
-
// Check the directory again.
|
|
147
|
-
await checkDirJob(true, interval);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
catch(error) {
|
|
151
|
-
console.log(`ERROR: Directory watching failed (${error.message})`);
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
// Checks servers for a network job.
|
|
155
|
-
const checkNetJob = async (servers, serverIndex, isForever, interval, noJobCount) => {
|
|
156
|
-
// If all servers are jobless:
|
|
157
|
-
if (noJobCount === servers.length) {
|
|
158
|
-
// Wait for the specified interval.
|
|
159
|
-
await wait(1000 * interval);
|
|
160
|
-
// Reset the count of jobless servers.
|
|
161
|
-
noJobCount = 0;
|
|
162
|
-
}
|
|
163
|
-
// Otherwise, i.e. if any server may still have a job:
|
|
164
|
-
else {
|
|
165
|
-
// Wait 2 seconds.
|
|
166
|
-
await wait(2000);
|
|
167
|
-
}
|
|
168
|
-
// If the last server has been checked:
|
|
169
|
-
serverIndex = serverIndex % servers.length;
|
|
170
|
-
if (serverIndex === 0) {
|
|
171
|
-
// Report this.
|
|
172
|
-
console.log('--');
|
|
173
|
-
}
|
|
174
|
-
// Check the next server.
|
|
175
|
-
const server = servers[serverIndex];
|
|
176
|
-
const client = server.startsWith('https://') ? httpsClient : httpClient;
|
|
177
|
-
const fullURL = `${server}?agent=${agent}`;
|
|
178
|
-
const logStart = `Requested job from server ${server} and got `;
|
|
179
|
-
// Tolerate unrecognized certificate authorities if the environment specifies.
|
|
180
|
-
const ruOpt = process.env.REJECT_UNAUTHORIZED === 'false' ? {rejectUnauthorized: false} : {};
|
|
181
|
-
client.request(fullURL, ruOpt, response => {
|
|
182
|
-
const chunks = [];
|
|
183
|
-
response
|
|
184
|
-
// If the response to the job request threw an error:
|
|
185
|
-
.on('error', async error => {
|
|
186
|
-
// Report it.
|
|
187
|
-
console.log(`${logStart}error message ${error.message}`);
|
|
188
|
-
// Check the next server.
|
|
189
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
190
|
-
})
|
|
191
|
-
.on('data', chunk => {
|
|
192
|
-
chunks.push(chunk);
|
|
193
|
-
})
|
|
194
|
-
// When the response arrives:
|
|
195
|
-
.on('end', async () => {
|
|
196
|
-
const content = chunks.join('');
|
|
197
|
-
try {
|
|
198
|
-
// If there was no job to do:
|
|
199
|
-
let contentObj = JSON.parse(content);
|
|
200
|
-
if (! Object.keys(contentObj).length) {
|
|
201
|
-
// Report this.
|
|
202
|
-
console.log(`No job to do at ${server}`);
|
|
203
|
-
// Check the next server.
|
|
204
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
205
|
-
}
|
|
206
|
-
// Otherwise, i.e. if there was a job or a message:
|
|
207
|
-
else {
|
|
208
|
-
const {id, message, sendReportTo, sources} = contentObj;
|
|
209
|
-
// If the server sent a message, not a job:
|
|
210
|
-
if (message) {
|
|
211
|
-
// Report it.
|
|
212
|
-
console.log(`${logStart}${message}`);
|
|
213
|
-
// Check the next server.
|
|
214
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
215
|
-
}
|
|
216
|
-
// Otherwise, if the server sent a valid job:
|
|
217
|
-
else if (id && sources && sources.target && sources.target.which) {
|
|
218
|
-
// Add the agent to it.
|
|
219
|
-
sources.agent = agent;
|
|
220
|
-
// If the job specifies a report destination:
|
|
221
|
-
if (sendReportTo) {
|
|
222
|
-
// Perform the job, adding result data to it.
|
|
223
|
-
const testee = sources.target.which;
|
|
224
|
-
console.log(
|
|
225
|
-
`${logStart}job ${id} (${nowString()})\n>> It will test ${testee}\n>> It will send report to ${sendReportTo}\n`
|
|
226
|
-
);
|
|
227
|
-
await doJob(contentObj);
|
|
228
|
-
let reportJSON = JSON.stringify(contentObj, null, 2);
|
|
229
|
-
console.log(`Job ${id} finished (${nowString()})`);
|
|
230
|
-
// Send the report to the specified server.
|
|
231
|
-
console.log(`Sending report ${id} to ${sendReportTo}`);
|
|
232
|
-
const reportClient = sendReportTo.startsWith('https://') ? httpsClient : httpClient;
|
|
233
|
-
const reportLogStart = `Sent report ${id} to ${sendReportTo} and got `;
|
|
234
|
-
reportClient.request(sendReportTo, {method: 'POST'}, repResponse => {
|
|
235
|
-
const chunks = [];
|
|
236
|
-
repResponse
|
|
237
|
-
// If the response to the report threw an error:
|
|
238
|
-
.on('error', async error => {
|
|
239
|
-
// Report this.
|
|
240
|
-
console.log(`${reportLogStart}error message ${error.message}\n`);
|
|
241
|
-
// Check the next server.
|
|
242
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
243
|
-
})
|
|
244
|
-
.on('data', chunk => {
|
|
245
|
-
chunks.push(chunk);
|
|
246
|
-
})
|
|
247
|
-
// When the response arrives:
|
|
248
|
-
.on('end', async () => {
|
|
249
|
-
const content = chunks.join('');
|
|
250
|
-
try {
|
|
251
|
-
// If the server sent a message, as expected:
|
|
252
|
-
const ackObj = JSON.parse(content);
|
|
253
|
-
const {message} = ackObj;
|
|
254
|
-
if (message) {
|
|
255
|
-
// Report it.
|
|
256
|
-
console.log(`${reportLogStart}${message}\n`);
|
|
257
|
-
// Free the memory used by the report.
|
|
258
|
-
reportJSON = '';
|
|
259
|
-
contentObj = {};
|
|
260
|
-
// Check the next server.
|
|
261
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, 0);
|
|
262
|
-
}
|
|
263
|
-
// Otherwise, i.e. if the server sent anything else:
|
|
264
|
-
else {
|
|
265
|
-
// Report it.
|
|
266
|
-
console.log(
|
|
267
|
-
`ERROR: ${reportLogStart}status ${repResponse.statusCode} and error message ${JSON.stringify(ackObj, null, 2)}\n`
|
|
268
|
-
);
|
|
269
|
-
// Check the next server, disregarding the failed job.
|
|
270
|
-
await checkNetJob(
|
|
271
|
-
servers, serverIndex + 1, isForever, interval, noJobCount + 1
|
|
272
|
-
);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
// If processing the report threw an error:
|
|
276
|
-
catch(error) {
|
|
277
|
-
// Report it.
|
|
278
|
-
console.log(
|
|
279
|
-
`ERROR: ${reportLogStart}status ${repResponse.statusCode}, error message ${error.message}, and response ${content.slice(0, 1000)}\n`
|
|
280
|
-
);
|
|
281
|
-
// Check the next server, disregarding the failed job.
|
|
282
|
-
await checkNetJob(
|
|
283
|
-
servers, serverIndex + 1, isForever, interval, noJobCount + 1
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
})
|
|
288
|
-
// If the report submission throws an error:
|
|
289
|
-
.on('error', async error => {
|
|
290
|
-
// Report this.
|
|
291
|
-
console.log(`ERROR: ${reportLogStart}error message ${error.message}\n`);
|
|
292
|
-
// Check the next server, disregarding the failed job.
|
|
293
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
294
|
-
})
|
|
295
|
-
// Finish submitting the report.
|
|
296
|
-
.end(reportJSON);
|
|
297
|
-
}
|
|
298
|
-
// Otherwise, i.e. if the job specifies no report destination:
|
|
299
|
-
else {
|
|
300
|
-
// Report this.
|
|
301
|
-
const message = `ERROR: ${logStart}job with no report destination`;
|
|
302
|
-
serveObject({message}, response);
|
|
303
|
-
console.log(message);
|
|
304
|
-
// Check the next server, disregarding the defective job.
|
|
305
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
// Otherwise, if the server sent an invalid job:
|
|
309
|
-
else {
|
|
310
|
-
// Report this.
|
|
311
|
-
const message
|
|
312
|
-
= `ERROR: ${logStart}invalid job:\n${JSON.stringify(contentObj, null, 2)}`;
|
|
313
|
-
console.log(message);
|
|
314
|
-
serveObject({message}, response);
|
|
315
|
-
// Check the next server, disregarding the defective job.
|
|
316
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
// If an error is thrown:
|
|
321
|
-
catch(error) {
|
|
322
|
-
// Report this.
|
|
323
|
-
console.log(`ERROR: ${error.message} (response ${content.slice(0, 1000)})`);
|
|
324
|
-
// Check the next server.
|
|
325
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
})
|
|
329
|
-
// If the job request throws an error:
|
|
330
|
-
.on('error', async error => {
|
|
331
|
-
// If it was a refusal to connect:
|
|
332
|
-
const {message} = error;
|
|
333
|
-
if (message.includes('ECONNREFUSED')) {
|
|
334
|
-
// Report this.
|
|
335
|
-
console.log(`${logStart}no connection`);
|
|
336
|
-
// Check the next server.
|
|
337
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
338
|
-
}
|
|
339
|
-
// Otherwise, if it was a DNS failure:
|
|
340
|
-
else if (message.includes('ENOTFOUND')) {
|
|
341
|
-
// Report this.
|
|
342
|
-
console.log(`${logStart}no domain name resolution`);
|
|
343
|
-
// Check the next server.
|
|
344
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
345
|
-
}
|
|
346
|
-
// Otherwise, i.e. if it was any other error:
|
|
347
|
-
else {
|
|
348
|
-
// Report this.
|
|
349
|
-
console.log(
|
|
350
|
-
`ERROR: ${logStart}no response, but got error message ${error.message.slice(0, 200)}`
|
|
351
|
-
);
|
|
352
|
-
// Check the next server.
|
|
353
|
-
await checkNetJob(servers, serverIndex + 1, isForever, interval, noJobCount + 1);
|
|
354
|
-
}
|
|
355
|
-
})
|
|
356
|
-
// Finish sending the request.
|
|
357
|
-
.end();
|
|
358
|
-
};
|
|
359
|
-
// Composes an interval description.
|
|
360
|
-
const intervalSpec = interval => {
|
|
361
|
-
if (interval > -1) {
|
|
362
|
-
return `repeatedly, with ${interval}-second intervals `;
|
|
363
|
-
}
|
|
364
|
-
else {
|
|
365
|
-
return '';
|
|
366
|
-
}
|
|
367
|
-
};
|
|
368
|
-
// Checks for a directory job, performs it, and submits a report, once or repeatedly.
|
|
369
|
-
exports.dirWatch = async (isForever, interval = 300) => {
|
|
370
|
-
console.log(`Directory watching started ${intervalSpec(interval)}(${nowString()})\n`);
|
|
371
|
-
// Start the checking.
|
|
372
|
-
await checkDirJob(isForever, interval);
|
|
373
|
-
};
|
|
374
|
-
// Checks for a network job, performs it, and submits a report, once or repeatedly.
|
|
375
|
-
exports.netWatch = async (isForever, interval = 300) => {
|
|
376
|
-
console.log('Starting netWatch');
|
|
377
|
-
// If the servers to be checked are valid:
|
|
378
|
-
const servers = jobURLs
|
|
379
|
-
.split('+')
|
|
380
|
-
.map(url => [Math.random(), url])
|
|
381
|
-
.sort((a, b) => a[0] - b[0])
|
|
382
|
-
.map(pair => pair[1]);
|
|
383
|
-
if (
|
|
384
|
-
servers
|
|
385
|
-
&& servers.length
|
|
386
|
-
&& servers
|
|
387
|
-
.every(server => ['http://', 'https://'].some(prefix => server.startsWith(prefix)))
|
|
388
|
-
) {
|
|
389
|
-
console.log(`Network watching started ${intervalSpec(interval)}(${nowString()})\n`);
|
|
390
|
-
// Start checking.
|
|
391
|
-
await checkNetJob(servers, 0, isForever, interval, 0);
|
|
392
|
-
}
|
|
393
|
-
else {
|
|
394
|
-
console.log('ERROR: List of job URLs invalid');
|
|
395
|
-
}
|
|
396
|
-
};
|