21 lines
810 B
JavaScript
21 lines
810 B
JavaScript
const { parentPort, workerData } = require('worker_threads');
|
|
const axios = require('axios');
|
|
let { url, body, headers, run, id } = workerData;
|
|
|
|
const workerFunction = async () => {
|
|
parentPort.postMessage({ status: 'starting', id, url, body, headers });
|
|
while (run) {
|
|
try {
|
|
await axios.post(url, body, { headers });
|
|
parentPort.postMessage({ status: 'success' });
|
|
} catch (error) {
|
|
if(error.status === 500) {
|
|
parentPort.postMessage({ status: 'recoverable-error', error: `Unable to POST at ${url} status: ${error.status}, message: ${error.message}`});
|
|
} else {
|
|
throw new Error(`Unable to POST at ${url} status: ${error.status}, message: ${error.message}`);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
workerFunction(); |