Skip to content

Commit

Permalink
Merge pull request #16 from thaddeus/restart-workers
Browse files Browse the repository at this point in the history
Restart workers
  • Loading branch information
Razzmatazzz authored Aug 26, 2024
2 parents f646629 + de395e9 commit 48de622
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 129 deletions.
33 changes: 20 additions & 13 deletions http/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (cluster.isPrimary && workerCount > 0) {
const env = getEnv();

const getKv = async (kvName, rejectOnError = true) => {
let refreshTime = msHalfHour;
try {
console.log(`getting ${kvName} data`);
clearTimeout(kvRefreshTimeout[kvName]);
Expand All @@ -36,7 +37,6 @@ if (cluster.isPrimary && workerCount > 0) {
const data = await kvLoading[kvName];
kvStore[kvName] = data;
delete kvLoading[kvName];
let refreshTime = msHalfHour;
if (data?.expiration && new Date(data.expiration) > new Date()) {
refreshTime = new Date(data.expiration) - new Date();
if (refreshTime < msOneMinute) {
Expand All @@ -46,25 +46,23 @@ if (cluster.isPrimary && workerCount > 0) {
if (data?.expiration === oldExpiration) {
refreshTime = msOneMinute;
}
kvRefreshTimeout[kvName] = setTimeout(() => {
getKv(kvName, false);
}, refreshTime);
return data;
} catch (error) {
delete kvLoading[kvName];
console.error('Error getting KV from cloudflare', error);
if (error.message !== 'Invalid CLOUDFLARE_TOKEN') {
refreshTime = msOneMinute;
if (!kvStore[kvName]) {
if (typeof kvStore[kvName] === 'undefined') {
refreshTime = 1000;
}
kvRefreshTimeout[kvName] = setTimeout(() => {
getKv(kvName, false);
}, refreshTime);
}
if (rejectOnError) {
return Promise.reject(error);
}
} finally {
kvRefreshTimeout[kvName] = setTimeout(() => {
getKv(kvName, false);
}, refreshTime);
}
};

Expand All @@ -79,8 +77,9 @@ if (cluster.isPrimary && workerCount > 0) {
const rcvWorkerMsgSpan = Sentry.startInactiveSpan({ name: "Receive worker message" });

//console.log(`message from worker ${id}:`, message);
let response = false;
if (message.action === 'getKv') {
const response = {
response = {
action: 'kvData',
kvName: message.kvName,
id: message.id,
Expand All @@ -96,10 +95,9 @@ if (cluster.isPrimary && workerCount > 0) {
} catch (error) {
response.error = error.message;
}
cluster.workers[id].send(response);
}
if (message.action === 'cacheResponse') {
const response = {
response = {
id: message.id,
data: false,
};
Expand All @@ -121,15 +119,24 @@ if (cluster.isPrimary && workerCount > 0) {
} catch (error) {
response.error = error.message;
}
cluster.workers[id].send(response);

}
if (response) {
const worker = cluster.workers[id];
if (worker?.isConnected()) {
cluster.workers[id].send(response);
}
}
// End the span
rcvWorkerMsgSpan.end();
});
}

cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
if (!signal) {
console.log('worker ' + worker.process.pid + ' died');
cluster.fork();
}
});
} else {
// Workers can share any TCP connection
Expand Down
Loading

0 comments on commit 48de622

Please sign in to comment.