bulltrackers-module 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/functions/orchestrator/index.js +130 -5
- package/package.json +1 -1
|
@@ -1,11 +1,136 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview
|
|
2
|
+
* @fileoverview Main orchestration logic for Discovery and Updates.
|
|
3
3
|
*/
|
|
4
|
+
const { logger } = require("sharedsetup")(__filename); // Assuming sharedsetup is available in module context
|
|
5
|
+
const coreUtils = require('../../core/utils');
|
|
6
|
+
const { firestore: firestoreUtils } = coreUtils;
|
|
7
|
+
const { discovery, updates } = require('./helpers'); // Import local helpers
|
|
4
8
|
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Runs the complete discovery orchestration process for a given user type.
|
|
11
|
+
* @param {string} userType - 'normal' or 'speculator'.
|
|
12
|
+
* @param {object} config - The specific discovery config slice for the user type.
|
|
13
|
+
* @param {object} globalConfig - Global config like proxiesCollectionName.
|
|
14
|
+
*/
|
|
15
|
+
async function runDiscovery(userType, config, globalConfig) {
|
|
16
|
+
logger.log('INFO', `[Module Orchestrator] Starting discovery for ${userType} users...`);
|
|
17
|
+
|
|
18
|
+
// Reset locks happens once per entry point, keep it there.
|
|
19
|
+
|
|
20
|
+
// 1. Check Need
|
|
21
|
+
const { needsDiscovery, blocksToFill } = await discovery.checkDiscoveryNeed(
|
|
22
|
+
userType,
|
|
23
|
+
config.targetUsersPerBlock,
|
|
24
|
+
config // Pass the whole config slice down
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
if (!needsDiscovery) {
|
|
28
|
+
logger.log('INFO', `[Module Orchestrator] No discovery needed for ${userType}.`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// 2. Get Candidates
|
|
33
|
+
const candidates = await discovery.getDiscoveryCandidates(
|
|
34
|
+
userType,
|
|
35
|
+
blocksToFill,
|
|
36
|
+
config // Pass the whole config slice down
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
// 3. Dispatch
|
|
40
|
+
await discovery.dispatchDiscovery(
|
|
41
|
+
userType,
|
|
42
|
+
candidates,
|
|
43
|
+
config // Pass the whole config slice down
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
logger.log('SUCCESS', `[Module Orchestrator] Dispatched discovery tasks for ${userType}.`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Runs the complete update orchestration process for a given user type.
|
|
51
|
+
* @param {string} userType - 'normal' or 'speculator'.
|
|
52
|
+
* @param {object} config - The update configuration object.
|
|
53
|
+
* @param {object} globalConfig - Global config like dispatcherTopicName.
|
|
54
|
+
*/
|
|
55
|
+
async function runUpdates(userType, config, globalConfig) {
|
|
56
|
+
logger.log('INFO', `[Module Orchestrator] Collecting users for daily update (${userType})...`);
|
|
57
|
+
|
|
58
|
+
// Reset locks happens once per entry point, keep it there.
|
|
59
|
+
|
|
60
|
+
// 1. Define Thresholds (Remains the same logic as before)
|
|
61
|
+
const now = new Date();
|
|
62
|
+
const startOfTodayUTC = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
|
63
|
+
const sevenDaysAgoUTC = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); // Speculator grace period
|
|
64
|
+
|
|
65
|
+
const thresholds = {
|
|
66
|
+
dateThreshold: startOfTodayUTC,
|
|
67
|
+
gracePeriodThreshold: sevenDaysAgoUTC
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// 2. Get Targets
|
|
71
|
+
const targets = await updates.getUpdateTargets(
|
|
72
|
+
userType,
|
|
73
|
+
thresholds,
|
|
74
|
+
config // Pass the whole update config down
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// 3. Dispatch
|
|
78
|
+
await updates.dispatchUpdates(
|
|
79
|
+
targets,
|
|
80
|
+
userType,
|
|
81
|
+
config.dispatcherTopicName, // Use topic name from config
|
|
82
|
+
config.taskBatchSize,
|
|
83
|
+
config.pubsubBatchSize
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
logger.log('SUCCESS', `[Module Orchestrator] Dispatched update tasks for ${userType}.`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Creates the higher-level discovery orchestrator function.
|
|
92
|
+
* This is the function the Cloud Function entry point will call.
|
|
93
|
+
* @param {object} config - The full orchestratorConfig object loaded from config.js.
|
|
94
|
+
* @returns {Function} The async function handler for the Cloud Function.
|
|
95
|
+
*/
|
|
96
|
+
function createDiscoveryOrchestrator(config) {
|
|
97
|
+
return async (req, res) => {
|
|
98
|
+
logger.log('INFO', '🚀 Discovery Orchestrator triggered via module...');
|
|
99
|
+
try {
|
|
100
|
+
await firestoreUtils.resetProxyLocks(config.proxiesCollectionName);
|
|
101
|
+
await runDiscovery('normal', config.discoveryConfig.normal, config);
|
|
102
|
+
await runDiscovery('speculator', config.discoveryConfig.speculator, config);
|
|
103
|
+
res.status(200).send('Discovery orchestration complete.');
|
|
104
|
+
} catch (error) {
|
|
105
|
+
logger.log('ERROR', 'FATAL Error in Discovery Orchestrator (Module)', { errorMessage: error.message, errorStack: error.stack });
|
|
106
|
+
res.status(500).send("An internal discovery error occurred.");
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Creates the higher-level update orchestrator function.
|
|
113
|
+
* This is the function the Cloud Function entry point will call.
|
|
114
|
+
* @param {object} config - The full orchestratorConfig object loaded from config.js.
|
|
115
|
+
* @returns {Function} The async function handler for the Cloud Function.
|
|
116
|
+
*/
|
|
117
|
+
function createUpdateOrchestrator(config) {
|
|
118
|
+
return async (req, res) => {
|
|
119
|
+
logger.log('INFO', '🚀 Update Orchestrator triggered via module...');
|
|
120
|
+
try {
|
|
121
|
+
await firestoreUtils.resetProxyLocks(config.proxiesCollectionName);
|
|
122
|
+
await runUpdates('normal', config.updateConfig, config);
|
|
123
|
+
await runUpdates('speculator', config.updateConfig, config);
|
|
124
|
+
res.status(200).send('Update orchestration complete.');
|
|
125
|
+
} catch (error) {
|
|
126
|
+
logger.log('ERROR', 'FATAL Error in Update Orchestrator (Module)', { errorMessage: error.message, errorStack: error.stack });
|
|
127
|
+
res.status(500).send("An internal update error occurred.");
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
7
131
|
|
|
8
132
|
module.exports = {
|
|
9
|
-
|
|
10
|
-
|
|
133
|
+
createDiscoveryOrchestrator,
|
|
134
|
+
createUpdateOrchestrator,
|
|
135
|
+
helpers: { discovery, updates } // Keep exporting helpers if needed elsewhere
|
|
11
136
|
};
|