Skip to content

Commit c996cf7

Browse files
committed
Merge branch 'main' into testing
2 parents 4aa2d6b + 72e1b8a commit c996cf7

File tree

1 file changed

+29
-23
lines changed
  • org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu

1 file changed

+29
-23
lines changed

org.jdrupes.vmoperator.runner.qemu/src/org/jdrupes/vmoperator/runner/qemu/QemuMonitor.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ protected void processInput(String line)
139139
@SuppressWarnings({ "PMD.AvoidSynchronizedStatement",
140140
"PMD.AvoidDuplicateLiterals" })
141141
public void onClosed(Closed<?> event, SocketIOChannel channel) {
142-
logger.finer(() -> "Closing QMP socket.");
143142
super.onClosed(event, channel);
143+
logger.finer(() -> "QMP socket closed.");
144+
monitorReady = false;
144145
channel.associated(this, getClass()).ifPresent(qm -> {
145146
synchronized (this) {
146147
if (powerdownTimer != null) {
@@ -199,28 +200,31 @@ public void onMonitorCommand(MonitorCommand event) throws IOException {
199200
@Handler(priority = 100)
200201
@SuppressWarnings("PMD.AvoidSynchronizedStatement")
201202
public void onStop(Stop event) {
202-
if (qemuChannel() != null) {
203-
// We have a connection to Qemu, attempt ACPI shutdown.
204-
event.suspendHandling();
205-
suspendedStop = event;
203+
if (!monitorReady) {
204+
logger.fine(() -> "No QMP connection,"
205+
+ " cannot send powerdown command");
206+
return;
207+
}
208+
// We have a connection to Qemu, attempt ACPI shutdown.
209+
event.suspendHandling();
210+
suspendedStop = event;
206211

207-
// Attempt powerdown command. If not confirmed, assume
208-
// "hanging" qemu process.
209-
powerdownTimer = Components.schedule(t -> {
210-
// Powerdown not confirmed
211-
logger.fine(() -> "QMP powerdown command has not effect.");
212-
synchronized (this) {
213-
powerdownTimer = null;
214-
if (suspendedStop != null) {
215-
suspendedStop.resumeHandling();
216-
suspendedStop = null;
217-
}
212+
// Attempt powerdown command. If not confirmed, assume
213+
// "hanging" qemu process.
214+
powerdownTimer = Components.schedule(t -> {
215+
// Powerdown not confirmed
216+
logger.fine(() -> "QMP powerdown command not confirmed");
217+
synchronized (this) {
218+
powerdownTimer = null;
219+
if (suspendedStop != null) {
220+
suspendedStop.resumeHandling();
221+
suspendedStop = null;
218222
}
219-
}, Duration.ofSeconds(1));
220-
logger.fine(() -> "Attempting QMP powerdown.");
221-
powerdownStartedAt = Instant.now();
222-
fire(new MonitorCommand(new QmpPowerdown()));
223-
}
223+
}
224+
}, Duration.ofSeconds(1));
225+
logger.fine(() -> "Attempting QMP powerdown.");
226+
powerdownStartedAt = Instant.now();
227+
fire(new MonitorCommand(new QmpPowerdown()));
224228
}
225229

226230
/**
@@ -238,7 +242,9 @@ public void onPowerdownEvent(PowerdownEvent event) {
238242
}
239243

240244
// (Re-)schedule timer as fallback
241-
logger.fine(() -> "QMP powerdown confirmed, waiting...");
245+
var waitUntil = powerdownStartedAt.plusSeconds(powerdownTimeout);
246+
logger.fine(() -> "QMP powerdown confirmed, waiting for"
247+
+ " termination until " + waitUntil);
242248
powerdownTimer = Components.schedule(t -> {
243249
logger.fine(() -> "Powerdown timeout reached.");
244250
synchronized (this) {
@@ -247,7 +253,7 @@ public void onPowerdownEvent(PowerdownEvent event) {
247253
suspendedStop = null;
248254
}
249255
}
250-
}, powerdownStartedAt.plusSeconds(powerdownTimeout));
256+
}, waitUntil);
251257
powerdownConfirmed = true;
252258
}
253259
}

0 commit comments

Comments
 (0)