Skip to content

Commit 06a2514

Browse files
committed
call recursively
1 parent d24aa63 commit 06a2514

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

caching/calculateUploadViews.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Promise = require('bluebird');
22
const _ = require('lodash');
33
const View = require('../models/index').View;
44
const Upload = require('../models/index').Upload;
5+
const moment = require('moment');
56

67
const c = {
78
l : console.log
@@ -23,6 +24,7 @@ async function calculateViewAmounts(){
2324

2425
if(logCaching == 'true'){
2526
c.l('Calculating view amounts for particular videos');
27+
console.log(moment(new Date).format('hh:mm:ss A'))
2628
}
2729

2830
// TODO: have to have a job to update upload's view amounts
@@ -47,6 +49,11 @@ async function calculateViewAmounts(){
4749
await upload.save();
4850
}
4951

52+
if(logCaching == 'true'){
53+
c.l('View amounts calculated');
54+
console.log(moment(new Date).format('hh:mm:ss A'))
55+
}
56+
5057
// if(logCaching == 'true'){
5158
// c.l('Uploads received from database');
5259
//

caching/runCaching.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const dotenv = require('dotenv');
33
const logCaching = process.env.LOG_CACHING;
44
const jsHelpers = require('../lib/helpers/js-helpers');
55

6+
function sleep(ms) {
7+
return new Promise(resolve => setTimeout(resolve, ms));
8+
}
9+
610
/** FOR FINDING ERRANT LOGS **/
711
if(process.env.SHOW_LOG_LOCATION == 'true' || 1 == 2){
812
jsHelpers.showLogLocation();
@@ -76,6 +80,7 @@ async function cacheOnlyRecentUploads(){
7680
async function cachePopularDailyStatsAndIndex(){
7781
try {
7882
await cachePopularUploads();
83+
await calculateUploadViews();
7984
await setCache.setDailyStats();
8085
await setCache.setIndexValues();
8186

@@ -108,26 +113,42 @@ console.log(`CACHE TOTAL VIEW INTERVAL IN MINUTES: ${cacheIntervalInMinutes} \n`
108113
// TODO: ideally, they should push into an array, run the most recent job in array, then delete it from array,
109114
// TODO: and run them sequentially
110115

111-
async function main(){
112-
113-
// setInterval(cacheOnlyRecentUploads, cacheRecentIntervalInMs);
114-
//
115-
// setInterval(cachePopularDailyStatsAndIndex, cacheIntervalInMs);
116-
//
117-
// // calculate total amount of views for channel display
118-
// // TODO: couldn't this just be done during popular? maybe not because it doesn't do for sensitive?
119-
// // TODO: then just do a separate job for
120-
// setInterval(calculateUploadViews, cacheTotalViewsIntervalInMs);
121-
116+
async function runRecentInterval(){
122117
// calculate and cache recent uploads every minute
123118
await cacheOnlyRecentUploads();
119+
await sleep(cacheRecentIntervalInMs)
120+
runRecentInterval();
121+
}
124122

125-
await cachePopularDailyStatsAndIndex();
123+
runRecentInterval();
126124

127-
await calculateUploadViews();
125+
async function runOtherCaching(){
126+
await cachePopularDailyStatsAndIndex();
127+
await sleep(cacheIntervalInMs)
128+
runOtherCaching()
128129
}
129130

130-
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();
131152

132153
// calculateUploadViews()
133154

0 commit comments

Comments
 (0)