diff --git a/config/example.env b/config/example.env index 9e61f4c03..2f03bd6f9 100644 --- a/config/example.env +++ b/config/example.env @@ -206,5 +206,5 @@ QF_ROUND_ESTIMATED_MATCHING_CACHE_DURATION=60000 # OPTIONAL - default: Every 10 minutes PROJECT_CAMPAIGNS_CACHE_DURATION=600000 -# OPTIONAL - default: */10 * * * * * ( Every 10 minutes) -PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=*/10 * * * * * +# OPTIONAL - default: *0 */5 * * * ( Every 5 minutes) +PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=0 */5 * * * diff --git a/config/test.env b/config/test.env index 71fce3f69..5f1398a3b 100644 --- a/config/test.env +++ b/config/test.env @@ -179,5 +179,5 @@ QF_ROUND_ESTIMATED_MATCHING_CACHE_DURATION=1 PROJECT_CAMPAIGNS_CACHE_DURATION=1 -# OPTIONAL - default: */10 * * * * * ( Every 10 minutes) -PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=*/10 * * * * * +# OPTIONAL - default: *0 */5 * * * ( Every 5 minutes) +PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION=0 */5 * * * diff --git a/src/services/campaignService.ts b/src/services/campaignService.ts index d3c31707d..129e0a0cc 100644 --- a/src/services/campaignService.ts +++ b/src/services/campaignService.ts @@ -10,6 +10,7 @@ import { ProjectResolverWorker } from '../workers/projectsResolverWorker'; import { QueryBuilder } from 'typeorm/query-builder/QueryBuilder'; import { findAllActiveCampaigns } from '../repositories/campaignRepository'; import { logger } from '../utils/logger'; +import { getRedisObject, setObjectInRedis } from '../redis'; const projectFiltersCacheDuration = Number(process.env.PROJECT_FILTERS_THREADS_POOL_DURATION) || 60000; @@ -41,13 +42,17 @@ const createFetchCampaignProjectsQuery = ( return projectsQueryParams; }; -let projectCampaignCache: { [key: number]: string[] } = {}; +const PROJECT_CAMPAIGN_CACHE_REDIS_KEY = + 'projectCampaignCache-for-projectBySlug'; -export const getAllProjectsRelatedToActiveCampaigns = (): { +export const getAllProjectsRelatedToActiveCampaigns = async (): Promise<{ [key: number]: string[]; -} => { +}> => { + const projectCampaignCache = await getRedisObject( + PROJECT_CAMPAIGN_CACHE_REDIS_KEY, + ); // It returns all project and campaigns( excluding manuallySelectedCampaign) - return projectCampaignCache; + return projectCampaignCache || {}; }; export const cacheProjectCampaigns = async (): Promise => { @@ -67,10 +72,15 @@ export const cacheProjectCampaigns = async (): Promise => { : (newProjectCampaignCache[project.id] = [campaign.slug]); } } - projectCampaignCache = newProjectCampaignCache; + await setObjectInRedis({ + key: PROJECT_CAMPAIGN_CACHE_REDIS_KEY, + value: newProjectCampaignCache, + // cronjob would fill it every 10 minutes so the expiration doesnt matter + expiration: 60 * 60 * 24 * 1, // 1 day + }); logger.debug( 'cacheProjectCampaigns() ended successfully, projectCampaignCache size ', - Object.keys(projectCampaignCache).length, + Object.keys(newProjectCampaignCache).length, ); }; diff --git a/src/services/cronJobs/updateProjectCampaignsCacheJob.ts b/src/services/cronJobs/updateProjectCampaignsCacheJob.ts index 3797278ab..b29b11cd6 100644 --- a/src/services/cronJobs/updateProjectCampaignsCacheJob.ts +++ b/src/services/cronJobs/updateProjectCampaignsCacheJob.ts @@ -8,7 +8,7 @@ import { CacheProjectCampaignsWorker } from '../../workers/cacheProjectCampaigns // every 10 minutes const cronJobTime = (config.get('CACHE_PROJECT_CAMPAIGNS_CRONJOB_EXPRESSION') as string) || - '*/10 * * * *'; + '0 */5 * * *'; const projectsFiltersThreadPool: Pool< ModuleThread