cleanupOrphanedVms function

Future<void> cleanupOrphanedVms(
  1. String workerId
)

Implementation

Future<void> cleanupOrphanedVms(String workerId) async {
  try {
    _log.info('Cleaning orphaned VMs for worker $workerId...');
    final prefix = 'openci-vm-$workerId-';

    // 1. Kill zombie processes to release file locks first
    await _killZombieAvfProcesses(workerId);
    await _killZombieVirtualizationProcesses(workerId);

    // 2. Clean up filesystem-based VMs for this worker
    final vms = await VirtualMachine.list();
    final workerVms = vms.where((vm) => vm.name.startsWith(prefix)).toList();

    for (final vm in workerVms) {
      _log.info('Deleting orphaned VM: ${vm.name}');
      await VirtualMachine.delete(vm.name);
    }
  } catch (e, s) {
    _log.severe('Error cleaning up orphaned VMs: $e');
    await Sentry.captureException(e, stackTrace: s);
  }
}