first commit
This commit is contained in:
21
worker.js
Normal file
21
worker.js
Normal file
@@ -0,0 +1,21 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user