Skip to content

Commit fa95324

Browse files
linking everything together
1 parent bb1efb2 commit fa95324

File tree

5 files changed

+69
-32
lines changed

5 files changed

+69
-32
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ WORKERS=
44
GOOGLE_SHEETS_API_KEY=
55
FREEDGE_SHEET_ID=
66
FREEDGE_APPROVED_EMAILS=
7+
SERVER_URL=
78

89
# Server Configuration
910
PORT=8000

prisma/migrations/20251120022949/migration.sql

Lines changed: 0 additions & 26 deletions
This file was deleted.

prisma/scraper.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,27 @@ async function processAllEateries(
362362
});
363363
}
364364

365+
async function getAllEateriesData() {
366+
return await prisma.eatery.findMany({
367+
include: {
368+
events: {
369+
include: {
370+
menu: {
371+
include: {
372+
items: {
373+
include: {
374+
dietaryPreferences: true,
375+
allergens: true,
376+
},
377+
},
378+
},
379+
},
380+
},
381+
},
382+
},
383+
});
384+
}
385+
365386
export async function main() {
366387
const startTime = Date.now();
367388
console.log('Starting scraper at', new Date(startTime).toString(), '\n');
@@ -467,6 +488,48 @@ export async function main() {
467488
console.error('✗ Error during database transaction:', error);
468489
throw error;
469490
}
491+
492+
// Send newly populated data to server
493+
console.log('[Scheduler] Scraper run finished\n');
494+
try {
495+
console.log('[Scheduler] Fetching all eatery data for server update...');
496+
const startFetchTime = Date.now();
497+
const allEateryData = await getAllEateriesData();
498+
const fetchDuration = ((Date.now() - startFetchTime) / 1000).toFixed(2);
499+
console.log(
500+
`[Scheduler] Fetched ${allEateryData.length} eateries in ${fetchDuration}s`,
501+
);
502+
503+
console.log('[Scheduler] Updating server cache with new eatery data...');
504+
const startUpdateTime = Date.now();
505+
const response = await fetch(
506+
`${process.env.SERVER_URL}/internal/cache/`,
507+
{
508+
method: 'POST',
509+
headers: {
510+
'Content-Type': 'application/json',
511+
[process.env.CACHE_REFRESH_HEADER!]:
512+
process.env.CACHE_REFRESH_SECRET!,
513+
},
514+
body: JSON.stringify({ eateries: allEateryData }),
515+
},
516+
);
517+
if (!response.ok) {
518+
throw new Error(
519+
`Server responded with status ${response.status} during cache update`,
520+
);
521+
}
522+
const updateDuration = ((Date.now() - startUpdateTime) / 1000).toFixed(2);
523+
console.log(
524+
`[Scheduler] Server cache updated successfully in ${updateDuration}s`,
525+
);
526+
}
527+
catch (error) {
528+
console.error(
529+
'[Scheduler] Failed to update server cache with new eatery data:',
530+
error,
531+
);
532+
}
470533

471534
const totalDuration = ((Date.now() - startTime) / 1000).toFixed(2);
472535
console.log(`\n✅ Dining data scraped successfully in ${totalDuration}s`);

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ app.set('trust proxy', 1);
2222

2323
app.use(requestLogger);
2424
app.use(helmet());
25-
app.use(express.json({ limit: '10kb' }));
25+
app.use(express.json({ limit: '2mb' }));
2626
app.use(ipRateLimiter);
2727

2828
const router = express.Router();

src/utils/cache.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ export function getEateriesEtag(): string {
6868

6969
export async function refreshCacheFromDB() {
7070
clearAppCache();
71-
71+
7272
// Temporarily suppress console logs for this query
7373
const originalLog = console.log;
7474
console.log = () => {};
75-
75+
7676
const eateries = await prisma.eatery.findMany({
7777
include: {
7878
events: {
@@ -87,18 +87,17 @@ export async function refreshCacheFromDB() {
8787
},
8888
},
8989
},
90-
userEventVotes: true,
9190
},
9291
orderBy: {
9392
startTimestamp: 'asc',
9493
},
9594
},
9695
},
9796
});
98-
97+
9998
// Restore console.log
10099
console.log = originalLog;
101-
100+
102101
populateCache(eateries);
103102
}
104103

0 commit comments

Comments
 (0)