11const mongoose = require ( 'mongoose' ) ;
22const dotenv = require ( 'dotenv' ) ;
3-
43const logCaching = process . env . LOG_CACHING ;
4+ const jsHelpers = require ( '../lib/helpers/js-helpers' ) ;
5+
6+ function sleep ( ms ) {
7+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
8+ }
9+
10+ /** FOR FINDING ERRANT LOGS **/
11+ if ( process . env . SHOW_LOG_LOCATION == 'true' || 1 == 2 ) {
12+ jsHelpers . showLogLocation ( ) ;
13+ }
514
615process . on ( 'uncaughtException' , ( err ) => {
716 console . log ( 'Uncaught Exception: ' , err ) ;
@@ -51,13 +60,11 @@ console.log(`CACHING IS RUNNING AGAINST: ${database} \n`);
5160
5261const setCache = require ( './setCache' ) ; // index and daily stats
5362
63+ // functions to run caching jobs
5464const cacheRecentUploads = require ( './cacheRecentUploads' ) ; // index and daily stats
5565const cachePopularUploads = require ( './cachePopularUploads' ) ; // index and daily stats
56-
5766const calculateUploadViews = require ( './calculateUploadViews' ) ; // index and daily stats
5867
59- // const cacheRecentUploads = require('./cacheRecentAndPopularUploads');
60-
6168async function cacheOnlyRecentUploads ( ) {
6269
6370 try {
@@ -73,6 +80,7 @@ async function cacheOnlyRecentUploads(){
7380async function cachePopularDailyStatsAndIndex ( ) {
7481 try {
7582 await cachePopularUploads ( ) ;
83+ await calculateUploadViews ( ) ;
7684 await setCache . setDailyStats ( ) ;
7785 await setCache . setIndexValues ( ) ;
7886
@@ -95,24 +103,52 @@ const cacheIntervalInMs = cacheIntervalInMinutes * ( 1000 * 60 );
95103
96104console . log ( `CACHE POPULAR, DAILY STATS AND INDEXES INTERVAL IN MINUTES: ${ cacheIntervalInMinutes } \n` ) ;
97105
98- async function main ( ) {
106+ let cacheTotalViewsInterval = parseInt ( process . env . CACHE_TOTAL_VIEW_INTERVAL ) || 6 ;
99107
100- setInterval ( cacheOnlyRecentUploads , cacheRecentIntervalInMs ) ;
108+ const cacheTotalViewsIntervalInMs = cacheTotalViewsInterval * ( 1000 * 60 ) ;
101109
102- setInterval ( cachePopularDailyStatsAndIndex , cacheIntervalInMs ) ;
110+ console . log ( `CACHE TOTAL VIEW INTERVAL IN MINUTES: ${ cacheIntervalInMinutes } \n` ) ;
103111
104- setInterval ( calculateUploadViews , cacheIntervalInMs ) ;
112+ // TODO: there is a bug here, where when times line up, they will run multiple jobs at once,
113+ // TODO: ideally, they should push into an array, run the most recent job in array, then delete it from array,
114+ // TODO: and run them sequentially
105115
116+ async function runRecentInterval ( ) {
106117 // calculate and cache recent uploads every minute
107118 await cacheOnlyRecentUploads ( ) ;
119+ await sleep ( cacheRecentIntervalInMs )
120+ runRecentInterval ( ) ;
121+ }
108122
109- await cachePopularDailyStatsAndIndex ( ) ;
110-
111- await calculateUploadViews ( ) ;
123+ runRecentInterval ( ) ;
112124
125+ async function runOtherCaching ( ) {
126+ await cachePopularDailyStatsAndIndex ( ) ;
127+ await sleep ( cacheIntervalInMs )
128+ runOtherCaching ( )
113129}
114130
115- main ( ) ;
131+ runOtherCaching ( )
132+
133+ // async function main(){
134+ //
135+ // // setInterval(cacheOnlyRecentUploads, cacheRecentIntervalInMs);
136+ // //
137+ // // setInterval(cachePopularDailyStatsAndIndex, cacheIntervalInMs);
138+ // //
139+ // // // calculate total amount of views for channel display
140+ // // // TODO: couldn't this just be done during popular? maybe not because it doesn't do for sensitive?
141+ // // // TODO: then just do a separate job for
142+ // // setInterval(calculateUploadViews, cacheTotalViewsIntervalInMs);
143+ //
144+ // // calculate and cache recent uploads every minute
145+ // // await cacheOnlyRecentUploads();
146+ //
147+ // // also does total view amounts
148+ // await cachePopularDailyStatsAndIndex();
149+ // }
150+
151+ // main();
116152
117153// calculateUploadViews()
118154
0 commit comments