Skip to content

Commit

Permalink
Merge pull request #314 from the-hideout/update-logging
Browse files Browse the repository at this point in the history
Update logging
  • Loading branch information
Razzmatazzz authored Jul 18, 2024
2 parents 6484405 + 3ee567d commit be1f961
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ Prerequisites:
- Install [Wrangler](https://github.com/cloudflare/wrangler)
- Run `wrangler login` - (needed for k/v store and secrets)

You may want to create a .dev.vars file in the main project folder with the following values:

- CACHE_BASIC_AUTH (used for caching)
- HTTP_GRAPHQL_SERVER (the address to the dedicated graphql server; must be https and a domain - not an IP address)

Start the API server:

- Start the dev environment by running `npm run dev`
Expand Down
2 changes: 1 addition & 1 deletion http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Tarkov Data API",
"main": "index.mjs",
"engines": {
"node": ">=18.16.0"
"node": ">=20.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\"",
Expand Down
3 changes: 1 addition & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ async function graphqlHandler(request, env, ctx) {
if (env.HTTP_GRAPHQL_SERVER) {
try {
const serverUrl = `${env.HTTP_GRAPHQL_SERVER}${graphQLOptions.baseEndpoint}`;
console.log(serverUrl);
const queryResult = await fetch(serverUrl, {
method: request.method,
body: JSON.stringify({
Expand All @@ -167,7 +166,7 @@ async function graphqlHandler(request, env, ctx) {
},
});
if (queryResult.status !== 200) {
throw new Error(`${queryResult.status} ${queryResult.statusText}`);
throw new Error(`${queryResult.status} ${queryResult.statusText}: ${await queryResult.text()}`);
}
console.log('Request served from graphql server');
return new Response(await queryResult.text(), responseOptions);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Tarkov Data API",
"main": "index.mjs",
"engines": {
"node": ">=18.16.0"
"node": ">=20.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\"",
Expand Down
11 changes: 10 additions & 1 deletion utils/worker-kv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class WorkerKV {
this.loadingPromises = {};
this.loadingInterval = false;
this.dataExpires = {};
this.lastRefresh = {};
this.refreshCooldown = 1000 * 60;
this.dataSource = dataSource;
this.gameModes = ['regular'];
}
Expand All @@ -17,7 +19,13 @@ class WorkerKV {
if (gameMode !== 'regular' && !forceRegular) {
requestKv += `_${gameMode}`;
}
if (this.cache[gameMode] && (!this.dataExpires[gameMode] || new Date() < this.dataExpires[gameMode])) {
let dataNeedsRefresh = false;
if (this.dataExpires[gameMode]) {
const stale = new Date() > this.dataExpires[gameMode];
const dataAge = new Date() - (this.lastRefresh[gameMode] ?? 0);
dataNeedsRefresh = stale && dataAge > this.refreshCooldown;
}
if (this.cache[gameMode] && !dataNeedsRefresh) {
//console.log(`${requestKv} is fresh; not refreshing`);
this.dataSource.setKvUsedForRequest(requestKv, requestId);
return {cache: this.cache[gameMode], gameMode};
Expand Down Expand Up @@ -92,6 +100,7 @@ class WorkerKV {
if (newDataExpires && this.dataExpires === newDataExpires) {
console.log(`${requestKv} is still stale after re-load`);
}
this.lastRefresh[gameMode] = new Date();
this.dataExpires[gameMode] = newDataExpires;
this.dataSource.setKvLoadedForRequest(requestKv, requestId);
this.loading[gameMode] = false;
Expand Down

0 comments on commit be1f961

Please sign in to comment.