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);
// Define the interval as a variable (in seconds)
const KEEP_ALIVE_INTERVAL_SECONDS = 15 * 60; // 15 minutes
export async function setupKeepAliveAgent(): Promise<void> {
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">
@@ -22,7 +25,7 @@ export async function setupKeepAliveAgent(): Promise<void> {
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>300</integer>
<integer>${KEEP_ALIVE_INTERVAL_SECONDS}</integer>
</dict>
</plist>`;

View File

@@ -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<void> {
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<boolean> {
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
}
}
return false; // Fallback return
}