feature/IO-3205-Paint-Scale-Integrations: Adjust keep alive timer to 15 mins from 5

This commit is contained in:
Dave Richer
2025-04-30 11:52:21 -04:00
parent 7933a8965c
commit a2511685ac
2 changed files with 43 additions and 33 deletions

View File

@@ -7,6 +7,9 @@ import log from "electron-log/main";
const execPromise = promisify(exec); 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<void> { export async function setupKeepAliveAgent(): Promise<void> {
const plistContent = `<?xml version="1.0" encoding="UTF-8"?> const plistContent = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@@ -22,7 +25,7 @@ export async function setupKeepAliveAgent(): Promise<void> {
<key>RunAtLoad</key> <key>RunAtLoad</key>
<true/> <true/>
<key>StartInterval</key> <key>StartInterval</key>
<integer>300</integer> <integer>${KEEP_ALIVE_INTERVAL_SECONDS}</integer>
</dict> </dict>
</plist>`; </plist>`;

View File

@@ -4,43 +4,50 @@ import log from "electron-log/main";
const execPromise = promisify(exec); const execPromise = promisify(exec);
// Define the interval as a variable (in minutes)
const KEEP_ALIVE_INTERVAL_MINUTES = 15;
export async function setupKeepAliveTask(): Promise<void> { export async function setupKeepAliveTask(): Promise<void> {
const taskName = "ImEXShopPartnerKeepAlive"; const taskName = "ImEXShopPartnerKeepAlive";
const protocolUrl = "imexmedia://keep-alive"; const protocolUrl = "imexmedia://keep-alive";
// Use rundll32.exe to silently open the URL as a protocol // Use rundll32.exe to silently open the URL as a protocol
const command = `rundll32.exe url.dll,OpenURL "${protocolUrl}"`; const command = `rundll32.exe url.dll,OpenURL "${protocolUrl}"`;
// Escape quotes for schtasks /tr parameter // Escape quotes for schtasks /tr parameter
const escapedCommand = command.replace(/"/g, '\\"'); 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 { try {
const { stdout, stderr } = await execPromise(schtasksCommand); const { stdout, stderr } = await execPromise(schtasksCommand);
log.info(`Scheduled task created: ${stdout}`); log.info(`Scheduled task created: ${stdout}`);
if (stderr) log.warn(`Scheduled task stderr: ${stderr}`); if (stderr) log.warn(`Scheduled task stderr: ${stderr}`);
} catch (error) { } catch (error) {
log.error(`Error creating scheduled task: ${error instanceof Error ? error.message : String(error)}`); log.error(
throw error; // Rethrow to allow caller to handle `Error creating scheduled task: ${error instanceof Error ? error.message : String(error)}`,
} );
throw error; // Rethrow to allow caller to handle
}
} }
export async function isKeepAliveTaskInstalled(): Promise<boolean> { export async function isKeepAliveTaskInstalled(): Promise<boolean> {
const taskName = "ImEXShopPartnerKeepAlive"; const taskName = "ImEXShopPartnerKeepAlive";
const maxRetries = 3; const maxRetries = 3;
const retryDelay = 500; // 500ms delay between retries const retryDelay = 500; // 500ms delay between retries
for (let attempt = 1; attempt <= maxRetries; attempt++) { for (let attempt = 1; attempt <= maxRetries; attempt++) {
try { try {
const { stdout } = await execPromise(`schtasks /query /tn "${taskName}"`); const { stdout } = await execPromise(`schtasks /query /tn "${taskName}"`);
return !!stdout; // Return true if task exists return !!stdout; // Return true if task exists
} catch (error) { } catch (error) {
log.debug(`Scheduled task ${taskName} not found (attempt ${attempt}/${maxRetries}): ${error instanceof Error ? error.message : String(error)}`); log.debug(
if (attempt === maxRetries) { `Scheduled task ${taskName} not found (attempt ${attempt}/${maxRetries}): ${error instanceof Error ? error.message : String(error)}`,
return false; // Return false after all retries fail );
} if (attempt === maxRetries) {
// Wait before retrying return false; // Return false after all retries fail
await new Promise((resolve) => setTimeout(resolve, retryDelay)); }
} // Wait before retrying
await new Promise((resolve) => setTimeout(resolve, retryDelay));
} }
return false; // Fallback return }
} return false; // Fallback return
}