feat(daemon): Add UPSD (NUT) protocol support, Proxmox VM shutdown action, pause/resume monitoring, and network-loss/unreachable handling; bump config version to 4.2
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import process from 'node:process';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { Nupst } from '../nupst.ts';
|
||||
import { logger } from '../logger.ts';
|
||||
import { theme } from '../colors.ts';
|
||||
import { PAUSE } from '../constants.ts';
|
||||
import type { IPauseState } from '../daemon.ts';
|
||||
import * as helpers from '../helpers/index.ts';
|
||||
|
||||
/**
|
||||
@@ -104,6 +109,125 @@ export class ServiceHandler {
|
||||
await this.nupst.getSystemd().getStatus(debugOptions.debugMode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause action monitoring
|
||||
* @param args Command arguments (e.g., ['--duration', '30m'])
|
||||
*/
|
||||
public async pause(args: string[]): Promise<void> {
|
||||
try {
|
||||
// Parse --duration argument
|
||||
let resumeAt: number | null = null;
|
||||
const durationIdx = args.indexOf('--duration');
|
||||
if (durationIdx !== -1 && args[durationIdx + 1]) {
|
||||
const durationStr = args[durationIdx + 1];
|
||||
const durationMs = this.parseDuration(durationStr);
|
||||
if (durationMs === null) {
|
||||
logger.error(`Invalid duration format: ${durationStr}`);
|
||||
logger.dim(' Valid formats: 30m, 2h, 1d (minutes, hours, days)');
|
||||
return;
|
||||
}
|
||||
if (durationMs > PAUSE.MAX_DURATION_MS) {
|
||||
logger.error(`Duration exceeds maximum of 24 hours`);
|
||||
return;
|
||||
}
|
||||
resumeAt = Date.now() + durationMs;
|
||||
}
|
||||
|
||||
// Check if already paused
|
||||
if (fs.existsSync(PAUSE.FILE_PATH)) {
|
||||
logger.warn('Monitoring is already paused');
|
||||
try {
|
||||
const data = fs.readFileSync(PAUSE.FILE_PATH, 'utf8');
|
||||
const state = JSON.parse(data) as IPauseState;
|
||||
logger.dim(` Paused at: ${new Date(state.pausedAt).toISOString()}`);
|
||||
if (state.resumeAt) {
|
||||
const remaining = Math.round((state.resumeAt - Date.now()) / 1000);
|
||||
logger.dim(` Auto-resume in: ${remaining > 0 ? remaining : 0} seconds`);
|
||||
}
|
||||
} catch (_e) {
|
||||
// Ignore parse errors
|
||||
}
|
||||
logger.dim(' Run "nupst resume" to resume monitoring');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create pause state
|
||||
const pauseState: IPauseState = {
|
||||
pausedAt: Date.now(),
|
||||
pausedBy: 'cli',
|
||||
resumeAt,
|
||||
};
|
||||
|
||||
// Ensure config directory exists
|
||||
const pauseDir = path.dirname(PAUSE.FILE_PATH);
|
||||
if (!fs.existsSync(pauseDir)) {
|
||||
fs.mkdirSync(pauseDir, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(PAUSE.FILE_PATH, JSON.stringify(pauseState, null, 2));
|
||||
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Monitoring Paused', 45, 'warning');
|
||||
logger.logBoxLine('UPS polling continues but actions are suppressed');
|
||||
if (resumeAt) {
|
||||
const durationStr = args[args.indexOf('--duration') + 1];
|
||||
logger.logBoxLine(`Auto-resume after: ${durationStr}`);
|
||||
logger.logBoxLine(`Resume at: ${new Date(resumeAt).toISOString()}`);
|
||||
} else {
|
||||
logger.logBoxLine('Duration: Indefinite');
|
||||
logger.logBoxLine('Run "nupst resume" to resume');
|
||||
}
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Failed to pause: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume action monitoring
|
||||
*/
|
||||
public async resume(): Promise<void> {
|
||||
try {
|
||||
if (!fs.existsSync(PAUSE.FILE_PATH)) {
|
||||
logger.info('Monitoring is not paused');
|
||||
return;
|
||||
}
|
||||
|
||||
fs.unlinkSync(PAUSE.FILE_PATH);
|
||||
|
||||
logger.log('');
|
||||
logger.logBoxTitle('Monitoring Resumed', 45, 'success');
|
||||
logger.logBoxLine('Action monitoring has been resumed');
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Failed to resume: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a duration string like '30m', '2h', '1d' into milliseconds
|
||||
*/
|
||||
private parseDuration(duration: string): number | null {
|
||||
const match = duration.match(/^(\d+)\s*(m|h|d)$/i);
|
||||
if (!match) return null;
|
||||
|
||||
const value = parseInt(match[1], 10);
|
||||
const unit = match[2].toLowerCase();
|
||||
|
||||
switch (unit) {
|
||||
case 'm': return value * 60 * 1000;
|
||||
case 'h': return value * 60 * 60 * 1000;
|
||||
case 'd': return value * 24 * 60 * 60 * 1000;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the service (requires root)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user