From 9780b42cd1f02a0ea7ac3b78fef4a05b4aa60bb1 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 18 Apr 2021 18:37:48 +0300 Subject: [PATCH] disable periodic ping for stopped machines --- .env | 6 ++- .../machines/components/WakeComponent.js | 40 ++++++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/.env b/.env index 8890868..f5b0bbc 100644 --- a/.env +++ b/.env @@ -6,4 +6,8 @@ REACT_APP_NETWORK_RESURRECTOR_API_URL=http://localhost:5064 REACT_APP_NETWORK_RESURRECTOR_SERVER_URL=https://toodle.ddns.net/network-resurrector-api -REACT_APP_MACHINE_PING_INTERVAL=300000 \ No newline at end of file +#600000 milliseconds = 10 minutes +REACT_APP_MACHINE_PING_INTERVAL=600000 + +#300000 milliseconds = 5 minutes +REACT_APP_MACHINE_STARTING_TIME=300000 \ No newline at end of file diff --git a/src/features/machines/components/WakeComponent.js b/src/features/machines/components/WakeComponent.js index 6c3fe63..2193262 100644 --- a/src/features/machines/components/WakeComponent.js +++ b/src/features/machines/components/WakeComponent.js @@ -8,15 +8,19 @@ import * as api from "../api"; const initialState = { on: false }; const defaultPingInterval = 1200000; //20 minutes +const defaultStartingTime = 300000; //5 minutes const WakeComponent = ({ machine, addLog }) => { const [state, setState] = useState(initialState); const [trigger, setTrigger] = useState(false); + const { t } = useTranslation(); const { success, error } = useToast(); const pingInterval = process.env.REACT_APP_MACHINE_PING_INTERVAL || defaultPingInterval; + const startingTime = + process.env.REACT_APP_MACHINE_STARTING_TIME || defaultStartingTime; const getCurrentDateTime = useCallback(() => { const currentDateTime = Date.now(); @@ -26,34 +30,40 @@ const WakeComponent = ({ machine, addLog }) => { return result; }, [t]); + const log = useCallback( + message => addLog(`[${getCurrentDateTime()}] ${message}`), + [addLog, getCurrentDateTime] + ); + const wakeMachine = useCallback(async () => { const result = await api.wakeMachine(machine.macAddress); setState(prev => ({ ...prev, on: result.success })); - addLog( - `[${getCurrentDateTime()}] [Wake]: Success: ${result.success}. Status: ${ - result.status - }` - ); + log(`[Wake]: Success: ${result.success}. Status: ${result.status}`); if (result.success) { success(result.status); + + //retrigger + log(`Periodic ping will be re-triggered in ${startingTime} ms.`); + setTimeout(() => { + setTrigger(prev => !prev); + }, startingTime); } else { error(result.status); } - }, [addLog, getCurrentDateTime, success, error, machine.macAddress]); + }, [log, success, error, startingTime, machine.macAddress]); useEffect(() => { api.pingMachine(machine.iPv4Address || machine.machineName).then(result => { setState(prev => ({ ...prev, on: result.success })); - addLog( - `[${getCurrentDateTime()}] [Ping]: Success: ${ - result.success - }. Status: ${result.status}` - ); + log(`[Ping]: Success: ${result.success}. Status: ${result.status}`); + + if (result.success) { + setTimeout(() => { + setTrigger(prev => !prev); + }, pingInterval); + } }); - setTimeout(() => { - setTrigger(prev => !prev); - }, pingInterval); - }, [machine, addLog, getCurrentDateTime, pingInterval, trigger]); + }, [machine, log, pingInterval, trigger]); return (