Merge branch 'feature/IO-3303-Socket-IO-Optimization-Auto-Add-Watchers-Gate' into release/2025-07-18
# Conflicts: # client/src/contexts/SocketIO/socketProvider.jsx
This commit is contained in:
@@ -32,6 +32,7 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
||||
const socketRef = useRef(null);
|
||||
const [clientId, setClientId] = useState(null);
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
const [socketInitialized, setSocketInitialized] = useState(false);
|
||||
const notification = useNotification();
|
||||
const userAssociationId = bodyshop?.associations?.[0]?.id;
|
||||
const { t } = useTranslation();
|
||||
@@ -147,6 +148,13 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
||||
onError: (err) => console.error("MARK_ALL_NOTIFICATIONS_READ error:", err)
|
||||
});
|
||||
|
||||
const checkAndReconnect = () => {
|
||||
if (socketRef.current && !socketRef.current.connected) {
|
||||
console.log("Attempting manual reconnect due to event trigger");
|
||||
socketRef.current.connect();
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const initializeSocket = async (token) => {
|
||||
if (!bodyshop || !bodyshop.id || socketRef.current) return;
|
||||
@@ -158,10 +166,14 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
||||
auth: { token, bodyshopId: bodyshop.id },
|
||||
reconnectionAttempts: Infinity,
|
||||
reconnectionDelay: 2000,
|
||||
reconnectionDelayMax: 10000
|
||||
reconnectionDelayMax: 60000,
|
||||
randomizationFactor: 0.5,
|
||||
transports: ["websocket", "polling"], // Add this to prefer WebSocket with polling fallback
|
||||
rememberUpgrade: true
|
||||
});
|
||||
|
||||
socketRef.current = socketInstance;
|
||||
setSocketInitialized(true);
|
||||
|
||||
const handleBodyshopMessage = (message) => {
|
||||
if (!message || !message.type) return;
|
||||
@@ -546,6 +558,57 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
|
||||
t
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!socketInitialized) return;
|
||||
|
||||
const onVisibilityChange = () => {
|
||||
if (document.visibilityState === "visible") {
|
||||
checkAndReconnect();
|
||||
}
|
||||
};
|
||||
|
||||
const onFocus = () => {
|
||||
checkAndReconnect();
|
||||
};
|
||||
|
||||
const onOnline = () => {
|
||||
checkAndReconnect();
|
||||
};
|
||||
|
||||
const onPageShow = (event) => {
|
||||
if (event.persisted) {
|
||||
checkAndReconnect();
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener("visibilitychange", onVisibilityChange);
|
||||
window.addEventListener("focus", onFocus);
|
||||
window.addEventListener("online", onOnline);
|
||||
window.addEventListener("pageshow", onPageShow);
|
||||
|
||||
// Sleep/wake detection using timer
|
||||
let lastTime = Date.now();
|
||||
const intervalMs = 1000; // Check every second
|
||||
const thresholdMs = 2000; // If more than 2 seconds elapsed, assume sleep/wake
|
||||
|
||||
const sleepCheckInterval = setInterval(() => {
|
||||
const currentTime = Date.now();
|
||||
if (currentTime > lastTime + intervalMs + thresholdMs) {
|
||||
console.log("Detected potential wake from sleep/hibernate");
|
||||
checkAndReconnect();
|
||||
}
|
||||
lastTime = currentTime;
|
||||
}, intervalMs);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener("visibilitychange", onVisibilityChange);
|
||||
window.removeEventListener("focus", onFocus);
|
||||
window.removeEventListener("online", onOnline);
|
||||
window.removeEventListener("pageshow", onPageShow);
|
||||
clearInterval(sleepCheckInterval);
|
||||
};
|
||||
}, [socketInitialized]);
|
||||
|
||||
return (
|
||||
<SocketContext.Provider
|
||||
value={{
|
||||
|
||||
@@ -19,7 +19,7 @@ async function JobCosting(req, res) {
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
//Uncomment for further testing
|
||||
// logger.log("job-costing-start", "DEBUG", req.user.email, jobid, null);
|
||||
logger.log("job-costing-start", "DEBUG", req.user.email, jobid, null);
|
||||
|
||||
try {
|
||||
const resp = await client.setHeaders({ Authorization: BearerToken }).request(queries.QUERY_JOB_COSTING_DETAILS, {
|
||||
@@ -47,9 +47,9 @@ async function JobCostingMulti(req, res) {
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
//Uncomment for further testing
|
||||
// logger.log("job-costing-multi-start", "DEBUG", req?.user?.email, null, {
|
||||
// jobids
|
||||
// });
|
||||
logger.log("job-costing-multi-start", "DEBUG", req?.user?.email, null, {
|
||||
jobids
|
||||
});
|
||||
|
||||
try {
|
||||
const resp = await client
|
||||
|
||||
@@ -8,6 +8,7 @@ const getLifecycleStatusColor = require("../utils/getLifecycleStatusColor");
|
||||
const jobLifecycle = async (req, res) => {
|
||||
// Grab the jobids and statuses from the request body
|
||||
const { jobids, statuses } = req.body;
|
||||
const { logger } = req;
|
||||
|
||||
if (!jobids) {
|
||||
return res.status(400).json({
|
||||
@@ -16,6 +17,12 @@ const jobLifecycle = async (req, res) => {
|
||||
}
|
||||
|
||||
const jobIDs = _.isArray(jobids) ? jobids : [jobids];
|
||||
|
||||
logger.log("job-lifecycle-start", "DEBUG", req?.user?.email, null, {
|
||||
jobids: jobIDs
|
||||
});
|
||||
|
||||
try {
|
||||
const client = req.userGraphQLClient;
|
||||
const resp = await client.request(queries.QUERY_TRANSITIONS_BY_JOBID, { jobids: jobIDs });
|
||||
|
||||
@@ -112,6 +119,16 @@ const jobLifecycle = async (req, res) => {
|
||||
: durationToHumanReadable(moment.duration(0))
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
logger.log("job-lifecycle-error", "ERROR", req?.user?.email, null, {
|
||||
jobids: jobIDs,
|
||||
statuses: statuses ? JSON.stringify(statuses) : "N/A",
|
||||
error: error.message
|
||||
});
|
||||
return res.status(500).json({
|
||||
error: "Internal server error"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = jobLifecycle;
|
||||
|
||||
@@ -50,7 +50,12 @@ const autoAddWatchers = async (req) => {
|
||||
try {
|
||||
// Fetch bodyshop data from Redis
|
||||
const bodyshopData = await getBodyshopFromRedis(shopId);
|
||||
const notificationFollowers = bodyshopData?.notification_followers || [];
|
||||
let notificationFollowers = bodyshopData?.notification_followers;
|
||||
|
||||
// Bail if notification_followers is missing or not an array
|
||||
if (!notificationFollowers || !Array.isArray(notificationFollowers)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute queries in parallel
|
||||
const [notificationData, existingWatchersData] = await Promise.all([
|
||||
|
||||
Reference in New Issue
Block a user