From 829f9732eee24ff47382c1de6b914287f54f00dd Mon Sep 17 00:00:00 2001 From: kkatusic Date: Thu, 2 Jan 2025 14:11:54 +0100 Subject: [PATCH] cron job for sitemap generating --- src/server/bootstrap.ts | 4 ++ .../cronJobs/generateSitemapOnFrontend.ts | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/services/cronJobs/generateSitemapOnFrontend.ts diff --git a/src/server/bootstrap.ts b/src/server/bootstrap.ts index 9c55eac75..f26037f31 100644 --- a/src/server/bootstrap.ts +++ b/src/server/bootstrap.ts @@ -70,6 +70,7 @@ import { runCheckPendingRecurringDonationsCronJob } from '../services/cronJobs/s import { runCheckQRTransactionJob } from '../services/cronJobs/checkQRTransactionJob'; import { addClient } from '../services/sse/sse'; import { runCheckPendingUserModelScoreCronjob } from '../services/cronJobs/syncUsersModelScore'; +import { runGenerateSitemapOnFrontend } from '../services/cronJobs/generateSitemapOnFrontend'; Resource.validate = validate; @@ -421,6 +422,9 @@ export async function bootstrap() { runUpdateRecurringDonationStream(); runCheckUserSuperTokenBalancesJob(); } + if (process.env.SITEMAP_CRON_SECRET !== '') { + runGenerateSitemapOnFrontend(); + } logger.debug( 'initializeCronJobs() before runCheckActiveStatusOfQfRounds() ', new Date(), diff --git a/src/services/cronJobs/generateSitemapOnFrontend.ts b/src/services/cronJobs/generateSitemapOnFrontend.ts new file mode 100644 index 000000000..c8cbfe626 --- /dev/null +++ b/src/services/cronJobs/generateSitemapOnFrontend.ts @@ -0,0 +1,39 @@ +/** + * This cron job is responsible for generating sitemap on frontend. + * + * It sends a request to frontend to generate sitemap. + * + * It is scheduled to run every Sunday at 00:00. + * + * It use SITEMAP_CRON_SECRET that is set in .env file and MUST be the same on frontend! + */ +import { schedule } from 'node-cron'; +import axios from 'axios'; +import { logger } from '../../utils/logger'; + +const cronJobTime = '0 0 * * 0'; // Every Sunday at 00:00 + +export const runGenerateSitemapOnFrontend = () => { + logger.debug( + 'runGenerateSitemapOnFrontend() has been called, cronJobTime:', + cronJobTime, + ); + + schedule(cronJobTime, async () => { + logger.debug('runGenerateSitemapOnFrontend() job has started'); + try { + const response = await axios.get( + `{process.env.FRONTEND_URL}/api/generate-sitemap`, + { + headers: { + Authorization: `Bearer {process.env.SITEMAP_CRON_SECRET}`, + }, + }, + ); + logger.info('runGenerateSitemapOnFrontend() response:', response.data); + } catch (error) { + logger.error('runGenerateSitemapOnFrontend() error:', error.message); + } + logger.debug('runGenerateSitemapOnFrontend() job has finished'); + }); +};