Compare commits

..

2 Commits

Author SHA1 Message Date
Admin a286e814b2 update alram 2025-09-25 09:05:15 +07:00
Admin ac7edfbc8b update alram 2025-09-25 08:48:07 +07:00
4 changed files with 73 additions and 41 deletions

File diff suppressed because one or more lines are too long

View File

@ -80,49 +80,81 @@ function broadcastToPorts(message: any) {
}); });
} }
function initDailyReloadAlarm() { // function initDailyReloadAlarm() {
// Tên alarm // // Tên alarm
const alarmName = "dailyReload"; // const alarmName = "dailyReload";
// // Xóa alarm cũ nếu có
// chrome.alarms.clear(alarmName, () => {
// // Lấy thời gian hiện tại
// const now = new Date();
// // Thiết lập 2 giờ sáng ngày hôm nay hoặc ngày mai nếu đã quá giờ
// const firstTrigger = new Date();
// firstTrigger.setHours(2, 0, 0, 0); // 2:00:00
// if (firstTrigger.getTime() <= now.getTime()) {
// // Nếu đã qua 2h sáng hôm nay, đặt vào ngày mai
// firstTrigger.setDate(firstTrigger.getDate() + 1);
// }
// const delayInMinutes = (firstTrigger.getTime() - now.getTime()) / 1000 / 60;
// // Tạo alarm
// chrome.alarms.create(alarmName, {
// delayInMinutes,
// periodInMinutes: 24 * 60, // lặp lại mỗi 24h
// });
// console.log(`⏰ Daily reload alarm set for ${firstTrigger}`);
// });
// // Lắng nghe alarm
// chrome.alarms.onAlarm.addListener((alarm) => {
// if (alarm.name === "dailyReload") {
// console.log("🔄 Reloading page (2 AM alarm)");
// // Reload tất cả tab đang mở (hoặc tab cụ thể nếu muốn)
// chrome.tabs.query({}, (tabs) => {
// tabs.forEach((tab) => {
// if (tab.id) chrome.tabs.reload(tab.id);
// });
// });
// }
// });
// }
function initReloadAlarmEvery4Hours() {
const alarmName = "reloadEvery4Hours";
// Xóa alarm cũ nếu có // Xóa alarm cũ nếu có
chrome.alarms.clear(alarmName, () => { chrome.alarms.clear(alarmName, () => {
// Lấy thời gian hiện tại // Tạo alarm chạy sau 1 phút và lặp lại mỗi 4 tiếng
const now = new Date();
// Thiết lập 2 giờ sáng ngày hôm nay hoặc ngày mai nếu đã quá giờ
const firstTrigger = new Date();
firstTrigger.setHours(2, 0, 0, 0); // 2:00:00
if (firstTrigger.getTime() <= now.getTime()) {
// Nếu đã qua 2h sáng hôm nay, đặt vào ngày mai
firstTrigger.setDate(firstTrigger.getDate() + 1);
}
const delayInMinutes = (firstTrigger.getTime() - now.getTime()) / 1000 / 60;
// Tạo alarm
chrome.alarms.create(alarmName, { chrome.alarms.create(alarmName, {
delayInMinutes, delayInMinutes: 1, // chạy lần đầu sau 1 phút
periodInMinutes: 24 * 60, // lặp lại mỗi 24h periodInMinutes: 4 * 60, // 4 giờ = 240 phút
}); });
console.log(`⏰ Daily reload alarm set for ${firstTrigger}`); console.log(`⏰ Reload alarm "${alarmName}" set to repeat every 4 hours`);
});
// Lắng nghe alarm
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "dailyReload") {
console.log("🔄 Reloading page (2 AM alarm)");
// Reload tất cả tab đang mở (hoặc tab cụ thể nếu muốn)
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (tab.id) chrome.tabs.reload(tab.id);
});
});
}
}); });
} }
// Listener nên đặt bên ngoài để không bị mất khi service worker restart
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "reloadEvery4Hours") {
console.log(
"🔄 Reloading all tabs (every 4 hours)",
new Date().toLocaleString()
);
// Reload tất cả các tab đang mở
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (tab.id) chrome.tabs.reload(tab.id);
});
});
}
});
initSocket(); initSocket();
initDailyReloadAlarm(); initReloadAlarmEvery4Hours();

View File

@ -45,10 +45,10 @@ contentService.startSyncConversations();
// AUTO SYNC MESAGE PREFIX (INTERNAL) // AUTO SYNC MESAGE PREFIX (INTERNAL)
contentService.autoSyncConversationPrefixMessages(); contentService.autoSyncConversationPrefixMessages();
// setTimeout(() => { // setTimeout(async () => {
// const a = new TeamsChatService(); // const a = new TeamsChatService();
// const message = a.extractAllMessages(); // const conversations = await a.handleGetConversations();
// console.log({ message }); // console.log({ conversations });
// }, 10000); // }, 10000);

View File

@ -18,7 +18,7 @@ export class LogsService {
) {} ) {}
async saveLog(data: SendLogDto) { async saveLog(data: SendLogDto) {
const thirtyMinutesAgo = new Date(Date.now() - 30 * 60 * 1000); const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
// 1. Check nếu đã có log trùng trong 30 phút // 1. Check nếu đã có log trùng trong 30 phút
const existingLog = await this.repo const existingLog = await this.repo
@ -26,7 +26,7 @@ export class LogsService {
.where('log.message = :message', { message: data.message }) .where('log.message = :message', { message: data.message })
.andWhere('log.type = :type', { type: data.type }) .andWhere('log.type = :type', { type: data.type })
.andWhere('log.created_at > :time', { .andWhere('log.created_at > :time', {
time: thirtyMinutesAgo.toISOString(), time: oneHourAgo.toISOString(),
}) // convert sang ISO }) // convert sang ISO
.getOne(); .getOne();