async function fetchFilteredSheetData() {
const sheets = google.sheets({ version: 'v4' });
const auth = new google.auth.GoogleAuth({
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'],
});
const authClient = await auth.getClient();
const spreadsheetId = process.env.GOOGLE_SHEET_ID;
// Define the range to fetch (from row 2 to 250, columns A to AZ)
const range = "Club master!A2:Z250"; // Adjust based on your actual sheet name
// Fetch the defined range from Google Sheets
const response = await sheets.spreadsheets.values.get({
auth: authClient,
spreadsheetId: spreadsheetId,
range: range,
});
const rows = response.data.values || [];
// Clear previous cached data and dictionary
cachedData = [];
dataDictionary = {};
// Step 1: Filter out blank rows and build the dictionary based on "Slack Id"
rows.forEach(row => {
const key = row[COLUMN_MAP["Slack Id"]]; // Get the key from the defined column
// Check if the key is not blank
if (key) {
cachedData.push(row); // Keep the row with a valid key
dataDictionary[key] = row; // Add the row to the dictionary with key
}
});
// Log the count of non-blank rows and successful data fetch
console.log(`Filtered non-blank rows count: ${cachedData.length}`);
console.log('Data fetch completed successfully.');
}
// Initial fetch when the app starts
// Function to initialize and start the application
async function startApp() {
console.log("Starting up the app...");
try {
// Perform an initial data fetch
await fetchFilteredSheetData(); // Wait for the fetch to complete
console.log('Startup filtered data fetch complete.');
// Schedule to fetch data every day at 04:00
cron.schedule('0 4 * * *', () => {
console.log('Running scheduled fetch at 04:00...');
fetchFilteredSheetData().catch(err => {
console.error('Error fetching sheet data:', err);
});
});
// After the fetch is complete, start the server
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
} catch (error) {
console.error('Error during startup:', error);
}
}
// Immediately invoke startApp using an IIFE
(async () => {
await startApp();
})();
现在,APP架构了将内存数据保存在本地云存储中,该存储非常有效,并且在应用程序再次旋转时可以迅速阅读。 这是实现GCP中持久应用所需的一些效率的解决方法 - 我没有找到一种实现这一目标的方法。