Skip to content

Commit

Permalink
Merge pull request #1900 from Giveth/feat/sitemap_cron_job
Browse files Browse the repository at this point in the history
Feat/Cron job for sitemap generating on FE
  • Loading branch information
kkatusic authored Jan 3, 2025
2 parents c74dc3b + 829f973 commit 09186cd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -421,6 +422,9 @@ export async function bootstrap() {
runUpdateRecurringDonationStream();
runCheckUserSuperTokenBalancesJob();
}
if (process.env.SITEMAP_CRON_SECRET !== '') {
runGenerateSitemapOnFrontend();
}
logger.debug(
'initializeCronJobs() before runCheckActiveStatusOfQfRounds() ',
new Date(),
Expand Down
39 changes: 39 additions & 0 deletions src/services/cronJobs/generateSitemapOnFrontend.ts
Original file line number Diff line number Diff line change
@@ -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');
});
};

0 comments on commit 09186cd

Please sign in to comment.