update tracking fn

This commit is contained in:
nkhangg 2025-03-25 13:19:04 +07:00
parent 1fc6e75be2
commit add0cf0263
3 changed files with 111 additions and 93 deletions

View File

@ -46,22 +46,29 @@ const handleUpdateProductTabs = (data) => {
};
// const tracking = async () => {
// if (_INTERVAL_TRACKING_ID) {
// clearInterval(_INTERVAL_TRACKING_ID);
// _INTERVAL_TRACKING_ID = null;
// }
// console.log('🚀 Tracking process started...');
// _INTERVAL_TRACKING_ID = setInterval(async () => {
// 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})`);
// console.log(` Waiting for parent process to start... (Product ID: ${productTab.id})`);
// continue;
// }
// }
@ -74,6 +81,7 @@ const handleUpdateProductTabs = (data) => {
// // 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();
// }
@ -87,7 +95,7 @@ const handleUpdateProductTabs = (data) => {
// // Nếu chưa có first_bid (trạng thái chưa đặt giá)
// if (!productTab.first_bid) {
// console.log(`🎯 Tracking out-bid event for Product ID: ${productTab.id}`);
// console.log(`🎯 Waiting for first bid for Product ID: ${productTab.id}`);
// continue;
// }
@ -100,84 +108,18 @@ const handleUpdateProductTabs = (data) => {
// console.log(`🚀 Executing action for Product ID: ${productTab.id}`);
// await productTab.action();
// }
// }, configs.AUTO_TRACKING_DELAY);
// 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 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.');
@ -231,6 +173,91 @@ const tracking = async () => {
// }
// };
const tracking = async () => {
console.log('🚀 Tracking process started...');
while (true) {
try {
console.log('🔍 Scanning active bids...');
const productTabs = _.flatMap(MANAGER_BIDS, 'children');
// Lắng nghe sự kiện của API bids (chạy song song)
await Promise.allSettled(
MANAGER_BIDS.filter((bid) => !bid.page_context).map((apiBid) => {
console.log(`🎧 Listening to events for API Bid ID: ${apiBid.id}`);
return apiBid.listen_events();
}),
);
// Duyệt qua từng productTab
await Promise.allSettled(
productTabs.map(async (productTab) => {
console.log(`📌 Processing Product ID: ${productTab.id}`);
// Xác định parent context
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... (Product ID: ${productTab.id})`);
return;
}
}
// 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();
}
// Kiểm tra URL và điều hướng nếu cần
if (productTab.page_context.url() !== productTab.url) {
console.log(`🔄 Redirecting to new URL for Product ID: ${productTab.id}`);
await productTab.gotoLink();
}
// Cập nhật nếu cần thiết
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.`);
}
// Chờ first bid
if (!productTab.first_bid) {
console.log(`🎯 Waiting for first bid for Product ID: ${productTab.id}`);
return;
}
// Kiểm tra thời gian bid
if (productTab.start_bid_time && !isTimeReached(productTab.start_bid_time)) {
console.log(`⏳ Not yet time to bid. Skipping Product ID: ${productTab.id}`);
return;
}
// Thực thi hành động
console.log(`🚀 Executing action for Product ID: ${productTab.id}`);
await productTab.action();
}),
);
// Dọn dẹp tab không dùng
console.log('🧹 Cleaning up unused tabs...');
await clearLazyTab();
// Cập nhật trạng thái tracking
console.log('📊 Tracking work status...');
workTracking();
} catch (error) {
console.error('❌ Error in tracking loop:', error);
}
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.');

View File

@ -19,7 +19,7 @@ export class Bid {
if (!this.page_context) return;
try {
await this.page_context.waitForSelector('#pageContainer', { timeout: 10000 });
// await this.page_context.waitForSelector('#pageContainer', { timeout: 10000 });
console.log(`✅ Page fully loaded. Taking snapshot for Product ID: ${this.id}`);
takeSnapshot(this.page_context, this, 'working', CONSTANTS.TYPE_IMAGE.WORK);
} catch (error) {

View File

@ -38,8 +38,6 @@ export class GraysProductBid extends ProductBid {
});
if (!response.status) {
// await this.handleReturnProductPage(page);
// await safeClosePage(this);
return { result: false, bid_price: 0 };
}
@ -95,7 +93,6 @@ export class GraysProductBid extends ProductBid {
if (!close_time || new Date(close_time).getTime() <= new Date().getTime()) {
console.log(`Product is close ${close_time}`);
// await safeClosePage(this);
return { result: true, close_time };
}
@ -126,10 +123,8 @@ export class GraysProductBid extends ProductBid {
await takeSnapshot(page, this, 'bid-success', CONSTANTS.TYPE_IMAGE.SUCCESS);
return true;
} catch (error) {
console.log({ error: error.message });
console.log('❌ Timeout to loading');
await takeSnapshot(page, this, 'timeout to loading');
// await safeClosePage(this);
return false;
}
}
@ -140,8 +135,6 @@ export class GraysProductBid extends ProductBid {
}
async handleUpdateBid({ lot_id, close_time, name, current_price, reserve_price }) {
// if (close_time && this.close_time == close_time) return;
const response = await updateBid(this.id, { lot_id, close_time, name, current_price, reserve_price: Number(reserve_price) || 0 });
if (response) {
@ -236,7 +229,6 @@ export class GraysProductBid extends ProductBid {
if (!resultPlaceBid) {
console.log('❌ Error occurred while placing the bid.');
await takeSnapshot(page, this, 'place-bid-action');
// await safeClosePage(this);
return;
}
@ -245,7 +237,6 @@ export class GraysProductBid extends ProductBid {
await this.handleReturnProductPage(page);
} catch (error) {
console.error(`🚨 Error navigating the page: ${error.message}`);
// safeClosePage(this);
}
};
}