-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
128 lines (102 loc) · 3.99 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const config = require( './config.js' )[ process.env.STAGE ];
const JsonArchive = require( './JsonArchive.js' ).init({
gcsProjectId: config.STORAGE_PROJECT,
gcsBucket: config.STORAGE_BUCKET
});
const BigqueryArchive = require( './BigqueryArchive.js' ).init({
gcsProjectId: config.BIGQUERY_PROJECT,
gcsBqDataset: config.BIGQUERY_DATASET
});
const jsonArchive = new JsonArchive();
const bigqueryArchive = new BigqueryArchive();
const archiveConfig = {
USER:{
kind:'USER', schema:require( `./schema/USER.js` ), fileName:'USER',
sortBy:'_TIMESTAMP_', minValue:new Date(0),
batchSize:1000, minUpdate: 100,
timeInt:900, minTimeInt:300, maxTimeInt: 900, nextRun:0, boost: 100 },
AUTHOR:{
kind:'AUTHOR', schema:require( `./schema/AUTHOR.js` ), fileName:'AUTHOR',
sortBy:'_TIMESTAMP_', minValue:new Date(0),
batchSize:1000, minUpdate: 100,
timeInt:900, minTimeInt:300, maxTimeInt: 900, nextRun:0, boost: 100 },
PRATILIPI:{
kind:'PRATILIPI', schema:require( `./schema/PRATILIPI.js` ), fileName:'PRATILIPI',
sortBy:'_TIMESTAMP_', minValue:new Date(0),
batchSize:1000, minUpdate: 100,
timeInt:900, minTimeInt:300, maxTimeInt: 900, nextRun:0, boost: 100 }
};
const bigqueryConfig = {
AUDIT_LOG: {
kind:'AUDIT_LOG',
schema:require( `./schema/AUDIT_LOG.js` ),
sortBy:'CREATION_DATE',
lastValue:null,
batchSize:1000,
minUpdate:100,
timeInt:15,
minTimeInt:15,
maxTimeInt:900,
nextRun:0
}
};
/* (function run() {
var archives = Object.keys( archiveConfig );
for( var i = 0; i < archives.length; i++ ) {
var archive = archives[ i ];
var config = archiveConfig[ archive ];
console.log(`RUN: before run ${archive}: ${config.nextRun} : ${config.timeInt}`);
if( config.nextRun > new Date().getTime() ) {
continue;
}
var callback = ( err, updateCount ) => {
console.log(`RUN: after run in callback ${archive}: ${config.nextRun} : ${config.timeInt}`);
if( err ) {
console.error( archive + ": " + JSON.stringify( err ) );
} else {
if( updateCount >= config.batchSize ) {
config.timeInt = Math.max( config.minTimeInt, Math.ceil( config.timeInt / 2 ) );
} else if( updateCount < config.minUpdate ) {
config.timeInt = Math.min( config.maxTimeInt, config.timeInt * 2 );
}
}
config.nextRun = new Date().getTime() + config.timeInt * 1000;
console.log(`RUN: executing again.`);
run();
};
console.log(`${archive}: Taking Backup`);
return jsonArchive.run( archive, config, callback );
}
console.log(`RUN: All Backups Complete. Starting again after 5 seconds`);
setTimeout( run, 5 * 1000 );
})(); */
( function bigQueryRun() {
var bigQueries = Object.keys( bigqueryConfig );
for( var i = 0; i < bigQueries.length; i++ ) {
var bigQuery = bigQueries[ i ];
var config = bigqueryConfig[ bigQuery ];
console.log(`bigQueryRUN: before run ${bigQuery}: ${config.nextRun} : ${config.timeInt}`);
if( config.nextRun > new Date().getTime() ) {
continue;
}
var callback = ( err, updateCount ) => {
console.log(`bigQueryRUN: after run in callback ${bigQuery}: ${config.nextRun} : ${config.timeInt}`);
if( err ) {
console.error( bigQuery + ": " + JSON.stringify( err ) );
} else {
if( ( updateCount + 1 ) === config.batchSize ) {
config.timeInt = Math.max( config.minTimeInt, Math.ceil( config.timeInt / 2 ) );
} else if( updateCount < config.minUpdate ) {
config.timeInt = Math.min( config.maxTimeInt, config.timeInt * 2 );
}
}
config.nextRun = new Date().getTime() + config.timeInt * 1000;
console.log(`bigQueryRUN: executing again.`);
bigQueryRun();
};
console.log(`${bigQuery}: Taking Backup`);
return bigqueryArchive.run( bigQuery, config, callback );
}
console.log(`bigQueryRUN: All Backups Complete. Starting again after 5 seconds`);
setTimeout( bigQueryRun, 5 * 1000 );
} )();