From a2511685ac2d139d1a937f374582af689df8abc7 Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Wed, 30 Apr 2025 11:52:21 -0400 Subject: [PATCH] feature/IO-3205-Paint-Scale-Integrations: Adjust keep alive timer to 15 mins from 5 --- src/main/setup-keep-alive-agent.ts | 5 ++- src/main/setup-keep-alive-task.ts | 71 ++++++++++++++++-------------- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/main/setup-keep-alive-agent.ts b/src/main/setup-keep-alive-agent.ts index 911be5b..60ae2b4 100644 --- a/src/main/setup-keep-alive-agent.ts +++ b/src/main/setup-keep-alive-agent.ts @@ -7,6 +7,9 @@ import log from "electron-log/main"; const execPromise = promisify(exec); +// Define the interval as a variable (in seconds) +const KEEP_ALIVE_INTERVAL_SECONDS = 15 * 60; // 15 minutes + export async function setupKeepAliveAgent(): Promise { const plistContent = ` @@ -22,7 +25,7 @@ export async function setupKeepAliveAgent(): Promise { RunAtLoad StartInterval - 300 + ${KEEP_ALIVE_INTERVAL_SECONDS} `; diff --git a/src/main/setup-keep-alive-task.ts b/src/main/setup-keep-alive-task.ts index 14d917e..a0af962 100644 --- a/src/main/setup-keep-alive-task.ts +++ b/src/main/setup-keep-alive-task.ts @@ -4,43 +4,50 @@ import log from "electron-log/main"; const execPromise = promisify(exec); +// Define the interval as a variable (in minutes) +const KEEP_ALIVE_INTERVAL_MINUTES = 15; + export async function setupKeepAliveTask(): Promise { - const taskName = "ImEXShopPartnerKeepAlive"; - const protocolUrl = "imexmedia://keep-alive"; - // Use rundll32.exe to silently open the URL as a protocol - const command = `rundll32.exe url.dll,OpenURL "${protocolUrl}"`; - // Escape quotes for schtasks /tr parameter - const escapedCommand = command.replace(/"/g, '\\"'); + const taskName = "ImEXShopPartnerKeepAlive"; + const protocolUrl = "imexmedia://keep-alive"; + // Use rundll32.exe to silently open the URL as a protocol + const command = `rundll32.exe url.dll,OpenURL "${protocolUrl}"`; + // Escape quotes for schtasks /tr parameter + const escapedCommand = command.replace(/"/g, '\\"'); - const schtasksCommand = `schtasks /create /tn "${taskName}" /tr "${escapedCommand}" /sc minute /mo 5 /f`; + const schtasksCommand = `schtasks /create /tn "${taskName}" /tr "${escapedCommand}" /sc minute /mo ${KEEP_ALIVE_INTERVAL_MINUTES} /f`; - try { - const { stdout, stderr } = await execPromise(schtasksCommand); - log.info(`Scheduled task created: ${stdout}`); - if (stderr) log.warn(`Scheduled task stderr: ${stderr}`); - } catch (error) { - log.error(`Error creating scheduled task: ${error instanceof Error ? error.message : String(error)}`); - throw error; // Rethrow to allow caller to handle - } + try { + const { stdout, stderr } = await execPromise(schtasksCommand); + log.info(`Scheduled task created: ${stdout}`); + if (stderr) log.warn(`Scheduled task stderr: ${stderr}`); + } catch (error) { + log.error( + `Error creating scheduled task: ${error instanceof Error ? error.message : String(error)}`, + ); + throw error; // Rethrow to allow caller to handle + } } export async function isKeepAliveTaskInstalled(): Promise { - const taskName = "ImEXShopPartnerKeepAlive"; - const maxRetries = 3; - const retryDelay = 500; // 500ms delay between retries + const taskName = "ImEXShopPartnerKeepAlive"; + const maxRetries = 3; + const retryDelay = 500; // 500ms delay between retries - for (let attempt = 1; attempt <= maxRetries; attempt++) { - try { - const { stdout } = await execPromise(`schtasks /query /tn "${taskName}"`); - return !!stdout; // Return true if task exists - } catch (error) { - log.debug(`Scheduled task ${taskName} not found (attempt ${attempt}/${maxRetries}): ${error instanceof Error ? error.message : String(error)}`); - if (attempt === maxRetries) { - return false; // Return false after all retries fail - } - // Wait before retrying - await new Promise((resolve) => setTimeout(resolve, retryDelay)); - } + for (let attempt = 1; attempt <= maxRetries; attempt++) { + try { + const { stdout } = await execPromise(`schtasks /query /tn "${taskName}"`); + return !!stdout; // Return true if task exists + } catch (error) { + log.debug( + `Scheduled task ${taskName} not found (attempt ${attempt}/${maxRetries}): ${error instanceof Error ? error.message : String(error)}`, + ); + if (attempt === maxRetries) { + return false; // Return false after all retries fail + } + // Wait before retrying + await new Promise((resolve) => setTimeout(resolve, retryDelay)); } - return false; // Fallback return -} \ No newline at end of file + } + return false; // Fallback return +}