Skip to content

Commit e3cf62f

Browse files
committed
fix: remove in memory cache on api
1 parent a37373a commit e3cf62f

File tree

8 files changed

+564
-21
lines changed

8 files changed

+564
-21
lines changed

build.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
docker buildx build --platform linux/aarch64 --no-cache -t worker:v-25 --load .
1+
docker buildx build --platform linux/aarch64 -t worker:v-39 --load .
22

3-
docker tag worker:v-25 tgrziminiar/worker:v-25
3+
docker tag worker:v-39 tgrziminiar/worker:v-39
44

5-
docker push tgrziminiar/worker:v-25
5+
docker push tgrziminiar/worker:v-39
66

77
docker run -d \
8-
-p 3001:3001 \
8+
-p 3002:3002 \
99
--env-file .env \
1010
--name fukthis \
11-
worker:v-2
11+
worker:v-36
1212

1313
docker run -d \
1414
-p 3001:3001 \

data/retropgf5-live-data/retropgf5-live-data.json

Lines changed: 528 additions & 0 deletions
Large diffs are not rendered by default.

lib/data/retropgf5-live-data/retropgf5-live-data.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/save-file/save-file.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export async function Savefile(data: any, requiredPath: string[] = [], fileName:
1616
throw new Error("Failed to create directory. Check permissions and path.");
1717
}
1818
}
19-
2019
const filePath = path.join(folderName, fileName);
2120

2221
// Use fs.writeFileSync for more robust file writing

src/controller/trigger-module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { FastifyReply, FastifyRequest } from "fastify";
22
import { importedModules } from "..";
33

44

5-
65
export async function TriggerModule(request: FastifyRequest<{ Params: { scriptName: string } }>, reply: FastifyReply) {
76
const { scriptName } = request.params;
87

@@ -20,7 +19,6 @@ export async function TriggerModule(request: FastifyRequest<{ Params: { scriptNa
2019
});
2120
}
2221

23-
2422
if (importedModules[scriptName] && typeof importedModules[scriptName].Run === 'function') {
2523
try {
2624
await importedModules[scriptName].Run();

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const scriptsDir = path.resolve(__dirname, 'scripts');
2424
const fileNameAndApiPath: { [key: string]: string } = {}
2525

2626
// cronJobsToSchedule use to store the cron jobs that need to be scheduled
27-
const cronJobsToSchedule: { CRON_TIMER: string; run: () => void; dataDir: string }[] = [];
27+
const cronJobsToSchedule: { CRON_TIMER: string; Run: any; DATA_DIR: string }[] = [];
2828

2929
// Store imported modules
3030
// the key is the file name and the value is the module (function to run)
@@ -74,12 +74,13 @@ async function processScripts() {
7474

7575
// console.log("here is cron timer only file",module.DATA_DIR, " ", module.CRON_TIMER, module)
7676
// if there is a cron timer, schedule the cron job
77+
// console.log(module)
7778
if (typeof(module.CRON_TIMER) === "string" && module.CRON_TIMER.trim() !== "") {
7879
console.log(`Scheduling cron job for ${file} with timer: ${module.CRON_TIMER}`);
7980
cronJobsToSchedule.push({
8081
CRON_TIMER: module.CRON_TIMER,
81-
run: module.Run,
82-
dataDir: dataDir,
82+
Run: module.Run,
83+
DATA_DIR: dataDir,
8384
});
8485
} else {
8586
console.log(`No valid cron timer for ${file}, skipping cron job scheduling.`);
@@ -112,15 +113,14 @@ async function startServer() {
112113

113114
await processScripts();
114115

115-
116116
fastify.get('/api/modules', async (request: FastifyRequest, reply: FastifyReply) => {
117117
reply.status(200).send({
118118
msg: "ok",
119119
data: fileNameAndApiPath
120120
});
121121
});
122122

123-
fastify.post<{ Params: { scriptName: string } }>('/api/modules/run/:scriptName', TriggerModule);
123+
fastify.post<{ Params: { scriptName: string } }>('/backoffice/modules/run/:scriptName', TriggerModule);
124124

125125
fastify.get<{ Params: { '*': string } }>('/api/*', GetStaticData);
126126

src/scripts/retropgf5-live-data/retropgf5-live-data.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@ import { ApolloClient, gql, HttpLink, InMemoryCache } from "@apollo/client/core"
22
import fetch from "cross-fetch";
33
import axios from "axios";
44
import { Savefile } from "../../../lib/save-file/save-file";
5+
56
const httpLink = new HttpLink({
67
uri: "https://optimism.easscan.org/graphql",
78
fetch: fetch,
89
});
910

1011
const client = new ApolloClient({
1112
link: httpLink,
12-
cache: new InMemoryCache(),
13+
cache: new InMemoryCache({
14+
resultCaching: false,
15+
}),
16+
defaultOptions: {
17+
query: {
18+
fetchPolicy: 'no-cache',
19+
},
20+
watchQuery: {
21+
fetchPolicy: 'no-cache',
22+
},
23+
},
1324
})
1425

1526
async function fetchMetadataSnapshot() {
@@ -105,6 +116,7 @@ export async function fetchAndProcessData() {
105116
const DATA_DIR = [ 'data', 'retropgf5-live-data']
106117

107118
// Which evaluates to 'At 0 seconds, 0 minutes every 1st hour'.
119+
// const CRON_TIMER:string | undefined = "*/5 * * * *"
108120
const CRON_TIMER:string | undefined = "0 0 */1 * * *"
109121
// const CRON_TIMER:string | undefined = undefined
110122

@@ -117,6 +129,7 @@ async function Run() {
117129
const dataArray = await fetchAndProcessData();
118130
// const dataArray: any[] = []
119131
await Savefile(JSON.stringify(dataArray), DATA_DIR, fileName)
132+
// console.log("save retropgf5 \n", dataArray)
120133

121134
} catch (error) {
122135
console.error("An error occurred during the RetroPGF5 Live Data process:", error);

utils/utils.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ export function headerGithub() : (RawAxiosRequestHeaders) | AxiosHeaders| null
4444

4545

4646
// scheduleCronJobs use to schedule the cron jobs
47-
export function scheduleCronJobs(cronJobsToSchedule: { CRON_TIMER: string; run: () => void; dataDir: string }[]) {
48-
cronJobsToSchedule.forEach(job => {
49-
console.log(`Scheduling cron job for ${job.dataDir} ${job.CRON_TIMER}`);
50-
cron.schedule(job.CRON_TIMER, job.run, {
51-
timezone: "America/Los_Angeles",
47+
export function scheduleCronJobs(cronJobsToSchedule: { CRON_TIMER: string; Run: any; DATA_DIR: string }[]) {
48+
try {
49+
cronJobsToSchedule.forEach(job => {
50+
console.log(`Scheduling cron job for ${job.DATA_DIR} ${job.CRON_TIMER}`);
51+
cron.schedule(job.CRON_TIMER, () => {
52+
job.Run()
53+
}, {
54+
timezone: "America/Los_Angeles",
55+
});
5256
});
53-
});
57+
} catch (error) {
58+
console.log("error when schedule the cron job", error)
59+
}
5460
}

0 commit comments

Comments
 (0)