update #5
|
|
@ -45,134 +45,6 @@ const handleUpdateProductTabs = (data) => {
|
||||||
MANAGER_BIDS = newDataManager;
|
MANAGER_BIDS = newDataManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
// const tracking = async () => {
|
|
||||||
// console.log('🚀 Tracking process started...');
|
|
||||||
|
|
||||||
// while (true) {
|
|
||||||
// console.log('🔍 Scanning active bids...');
|
|
||||||
// const productTabs = _.flatMap(MANAGER_BIDS, 'children');
|
|
||||||
|
|
||||||
// for (const apiBid of MANAGER_BIDS) {
|
|
||||||
// if (apiBid.page_context) continue;
|
|
||||||
|
|
||||||
// console.log(`🎧 Listening to events for API Bid ID: ${apiBid.id}`);
|
|
||||||
// await apiBid.listen_events();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for (const productTab of productTabs) {
|
|
||||||
// console.log(`📌 Processing Product ID: ${productTab.id}`);
|
|
||||||
|
|
||||||
// // Tìm parent context nếu chưa có
|
|
||||||
// if (!productTab.parent_browser_context) {
|
|
||||||
// const parent = _.find(MANAGER_BIDS, { id: productTab.web_bid.id });
|
|
||||||
// productTab.parent_browser_context = parent?.browser_context;
|
|
||||||
|
|
||||||
// if (!productTab.parent_browser_context) {
|
|
||||||
// console.log(`⏳ Waiting for parent process to start... (Product ID: ${productTab.id})`);
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Kết nối Puppeteer nếu chưa có page_context
|
|
||||||
// if (!productTab.page_context) {
|
|
||||||
// console.log(`🔌 Connecting to page for Product ID: ${productTab.id}`);
|
|
||||||
// await productTab.puppeteer_connect();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Nếu URL thay đổi, điều hướng đến URL mới
|
|
||||||
// if (productTab.page_context.url() !== productTab.url) {
|
|
||||||
// console.log(`🔄 Redirecting to new URL for Product ID: ${productTab.id}`);
|
|
||||||
// await productTab.gotoLink();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Kiểm tra nếu cần cập nhật trước khi gọi update()
|
|
||||||
// if (shouldUpdateProductTab(productTab)) {
|
|
||||||
// console.log(`🔄 Updating Product ID: ${productTab.id}...`);
|
|
||||||
// await productTab.update();
|
|
||||||
// } else {
|
|
||||||
// console.log(`⏳ Product ID: ${productTab.id} was updated recently. Skipping update.`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Nếu chưa có first_bid (trạng thái chưa đặt giá)
|
|
||||||
// if (!productTab.first_bid) {
|
|
||||||
// console.log(`🎯 Waiting for first bid for Product ID: ${productTab.id}`);
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Nếu chưa đến giờ bid
|
|
||||||
// if (productTab.start_bid_time && !isTimeReached(productTab.start_bid_time)) {
|
|
||||||
// console.log(`⏳ Not yet time to bid. Skipping Product ID: ${productTab.id}`);
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// console.log(`🚀 Executing action for Product ID: ${productTab.id}`);
|
|
||||||
// await productTab.action();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// console.log('🧹 Cleaning up unused tabs...');
|
|
||||||
// await clearLazyTab();
|
|
||||||
|
|
||||||
// console.log('📊 Tracking work status...');
|
|
||||||
// workTracking();
|
|
||||||
|
|
||||||
// console.log(`⏳ Waiting ${configs.AUTO_TRACKING_DELAY / 1000} seconds before the next iteration...`);
|
|
||||||
// await delay(configs.AUTO_TRACKING_DELAY);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// const clearLazyTab = async () => {
|
|
||||||
// if (!browser) {
|
|
||||||
// console.warn('⚠️ Browser is not available or disconnected.');
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// const pages = await browser.pages();
|
|
||||||
|
|
||||||
// // Lấy danh sách URL từ flattenedArray
|
|
||||||
// const activeUrls = _.flatMap(MANAGER_BIDS, (item) => [item.url, ...item.children.map((child) => child.url)]).filter(Boolean); // Lọc bỏ null hoặc undefined
|
|
||||||
|
|
||||||
// console.log(
|
|
||||||
// '🔍 Page URLs:',
|
|
||||||
// pages.map((page) => page.url()),
|
|
||||||
// );
|
|
||||||
|
|
||||||
// for (const page of pages) {
|
|
||||||
// const pageUrl = page.url();
|
|
||||||
|
|
||||||
// // 🔥 Bỏ qua tab 'about:blank' hoặc tab không có URL
|
|
||||||
// if (!pageUrl || pageUrl === 'about:blank') continue;
|
|
||||||
|
|
||||||
// if (!activeUrls.includes(pageUrl)) {
|
|
||||||
// if (!page.isClosed() && browser.isConnected()) {
|
|
||||||
// try {
|
|
||||||
// await page.close();
|
|
||||||
// console.log(`🛑 Closing unused tab: ${pageUrl}`);
|
|
||||||
// } catch (err) {
|
|
||||||
// console.warn(`⚠️ Error closing tab ${pageUrl}:`, err.message);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } catch (err) {
|
|
||||||
// console.error('❌ Error in clearLazyTab:', err.message);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// const workTracking = async () => {
|
|
||||||
// try {
|
|
||||||
// const activeData = _.flatMap(MANAGER_BIDS, (item) => [item, ...item.children]);
|
|
||||||
|
|
||||||
// for (const item of activeData) {
|
|
||||||
// if (item.page_context && !item.page_context.isClosed()) {
|
|
||||||
// item.handleTakeWorkSnapshot();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log('Lỗi rồi:', error);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
const tracking = async () => {
|
const tracking = async () => {
|
||||||
console.log('🚀 Tracking process started...');
|
console.log('🚀 Tracking process started...');
|
||||||
|
|
||||||
|
|
@ -189,8 +61,7 @@ const tracking = async () => {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Duyệt qua từng productTab
|
Promise.allSettled(
|
||||||
await Promise.allSettled(
|
|
||||||
productTabs.map(async (productTab) => {
|
productTabs.map(async (productTab) => {
|
||||||
console.log(`📌 Processing Product ID: ${productTab.id}`);
|
console.log(`📌 Processing Product ID: ${productTab.id}`);
|
||||||
|
|
||||||
|
|
@ -211,7 +82,7 @@ const tracking = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kiểm tra URL và điều hướng nếu cần
|
// Kiểm tra URL và điều hướng nếu cần
|
||||||
if (productTab.page_context.url() !== productTab.url) {
|
if ((await productTab.page_context.url()) !== productTab.url) {
|
||||||
console.log(`🔄 Redirecting to new URL for Product ID: ${productTab.id}`);
|
console.log(`🔄 Redirecting to new URL for Product ID: ${productTab.id}`);
|
||||||
await productTab.gotoLink();
|
await productTab.gotoLink();
|
||||||
}
|
}
|
||||||
|
|
@ -244,7 +115,7 @@ const tracking = async () => {
|
||||||
|
|
||||||
// Dọn dẹp tab không dùng
|
// Dọn dẹp tab không dùng
|
||||||
console.log('🧹 Cleaning up unused tabs...');
|
console.log('🧹 Cleaning up unused tabs...');
|
||||||
await clearLazyTab();
|
clearLazyTab();
|
||||||
|
|
||||||
// Cập nhật trạng thái tracking
|
// Cập nhật trạng thái tracking
|
||||||
console.log('📊 Tracking work status...');
|
console.log('📊 Tracking work status...');
|
||||||
|
|
@ -265,41 +136,38 @@ const clearLazyTab = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const contexts = browser.contexts(); // Lấy tất cả contexts
|
const pages = await browser.pages();
|
||||||
|
|
||||||
for (const context of contexts) {
|
// Lấy danh sách URL từ flattenedArray
|
||||||
const pages = context.pages(); // Lấy tất cả pages trong context
|
const activeUrls = _.flatMap(MANAGER_BIDS, (item) => [item.url, ...item.children.map((child) => child.url)]).filter(Boolean); // Lọc bỏ null hoặc undefined
|
||||||
|
|
||||||
// Lấy danh sách URL từ flattenedArray
|
console.log(
|
||||||
const activeUrls = _.flatMap(MANAGER_BIDS, (item) => [item.url, ...item.children.map((child) => child.url)]).filter(Boolean);
|
'🔍 Page URLs:',
|
||||||
|
pages.map((page) => page.url()),
|
||||||
|
);
|
||||||
|
|
||||||
for (const page of pages) {
|
for (const page of pages) {
|
||||||
const pageUrl = page.url();
|
const pageUrl = page.url();
|
||||||
|
|
||||||
if (!pageUrl || pageUrl === 'about:blank') continue;
|
// 🔥 Bỏ qua tab 'about:blank' hoặc tab không có URL
|
||||||
|
if (!pageUrl || pageUrl === 'about:blank') continue;
|
||||||
|
|
||||||
if (!activeUrls.includes(pageUrl)) {
|
if (!activeUrls.includes(pageUrl)) {
|
||||||
if (!page.isClosed()) {
|
if (!page.isClosed() && browser.isConnected()) {
|
||||||
try {
|
try {
|
||||||
await page.close();
|
await page.close();
|
||||||
console.log(`🛑 Closing unused tab: ${pageUrl}`);
|
console.log(`🛑 Closing unused tab: ${pageUrl}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(`⚠️ Error closing tab ${pageUrl}:`, err.message);
|
console.warn(`⚠️ Error closing tab ${pageUrl}:, err.message`);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nếu context không còn page nào thì đóng luôn
|
|
||||||
if (context.pages().length === 0) {
|
|
||||||
await context.close();
|
|
||||||
console.log(`🗑️ Closing unused context`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('❌ Error in clearLazyTab:', err.message);
|
console.error('❌ Error in clearLazyTab:', err.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const workTracking = async () => {
|
const workTracking = async () => {
|
||||||
try {
|
try {
|
||||||
const activeData = _.flatMap(MANAGER_BIDS, (item) => [item, ...item.children]);
|
const activeData = _.flatMap(MANAGER_BIDS, (item) => [item, ...item.children]);
|
||||||
|
|
@ -344,8 +212,6 @@ const workTracking = async () => {
|
||||||
console.log('📢 Bids Data:', data);
|
console.log('📢 Bids Data:', data);
|
||||||
|
|
||||||
handleUpdateProductTabs(data);
|
handleUpdateProductTabs(data);
|
||||||
|
|
||||||
// await Promise.all(MANAGER_BIDS.map((apiBid) => apiBid.listen_events()));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('webUpdated', async (data) => {
|
socket.on('webUpdated', async (data) => {
|
||||||
|
|
@ -361,8 +227,6 @@ const workTracking = async () => {
|
||||||
if (tabs.length <= 0) return;
|
if (tabs.length <= 0) return;
|
||||||
|
|
||||||
await Promise.all(tabs.map((tab) => safeClosePage(tab)));
|
await Promise.all(tabs.map((tab) => safeClosePage(tab)));
|
||||||
|
|
||||||
await Promise.all(MANAGER_BIDS.map((apiBid) => apiBid.listen_events()));
|
|
||||||
} else {
|
} else {
|
||||||
console.log('⚠️ No profile found to delete.');
|
console.log('⚠️ No profile found to delete.');
|
||||||
}
|
}
|
||||||
|
|
@ -370,8 +234,4 @@ const workTracking = async () => {
|
||||||
|
|
||||||
// AUTO TRACKING
|
// AUTO TRACKING
|
||||||
tracking();
|
tracking();
|
||||||
|
|
||||||
// clearLazyTab();
|
|
||||||
|
|
||||||
// workTracking();
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ export const takeSnapshot = async (page, item, imageName, type = CONSTANTS.TYPE_
|
||||||
});
|
});
|
||||||
|
|
||||||
// Chụp ảnh màn hình và lưu vào filePath
|
// Chụp ảnh màn hình và lưu vào filePath
|
||||||
await page.screenshot({ path: filePath, fullPage: true });
|
await page.screenshot({ path: filePath });
|
||||||
|
|
||||||
console.log(`📸 Image saved at: ${filePath}`);
|
console.log(`📸 Image saved at: ${filePath}`);
|
||||||
|
|
||||||
|
|
@ -49,8 +49,6 @@ export const takeSnapshot = async (page, item, imageName, type = CONSTANTS.TYPE_
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Error when snapshot: ' + error.message);
|
console.log('Error when snapshot: ' + error.message);
|
||||||
} finally {
|
|
||||||
global.IS_CLEANING = true;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue