Skip to content

Commit ca78c93

Browse files
author
Chris Townsend
committed
delayed_shutdown: Changes based on review
Be more nice about how often the shutdown message is written in the user's terminal. Change wording of the shutdown messages.
1 parent 6efe7ab commit ca78c93

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/daemon/daemon.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,10 +1345,11 @@ try // clang-format on
13451345
{
13461346
if (delayed_shutdown_instances[name]->get_time_remaining() <= std::chrono::minutes(1))
13471347
{
1348-
return grpc::Status(
1349-
grpc::StatusCode::FAILED_PRECONDITION,
1350-
fmt::format("\"{}\" is in a delayed shutdown. Use 'multipass stop -c {}' to cancel.", name, name),
1351-
"");
1348+
return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION,
1349+
fmt::format("\"{}\" is scheduled to shut down in less than a minute, use "
1350+
"'multipass stop --cancel {}' to cancel the shutdown.",
1351+
name, name),
1352+
"");
13521353
}
13531354
}
13541355

src/daemon/delayed_shutdown_timer.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ namespace mpl = multipass::logging;
2525

2626
namespace
2727
{
28-
std::string shutdown_message(const std::chrono::minutes& time_left)
28+
void write_shutdown_message(mp::SSHSession& ssh_session, const std::chrono::minutes& time_left, const std::string& name)
2929
{
3030
if (time_left > std::chrono::milliseconds::zero())
3131
{
32-
return fmt::format("wall The system is going down for poweroff in {} minute{}", time_left.count(),
33-
time_left > std::chrono::minutes(1) ? "s" : "");
32+
ssh_session.exec(fmt::format("wall \"The system is going down for poweroff in {} minute{}, use 'multipass stop "
33+
"--cancel {}' to cancel the shutdown.\"",
34+
time_left.count(), time_left > std::chrono::minutes(1) ? "s" : "", name));
3435
}
3536
else
3637
{
37-
return fmt::format("wall The system is going down for poweroff now");
38+
ssh_session.exec(fmt::format("wall The system is going down for poweroff now"));
3839
}
3940
}
4041
} // namespace
@@ -48,6 +49,7 @@ mp::DelayedShutdownTimer::~DelayedShutdownTimer()
4849
{
4950
if (shutdown_timer.isActive())
5051
{
52+
// exit_code() is here to make sure the command finishes before continuing in the dtor
5153
ssh_session.exec("wall The system shutdown has been cancelled").exit_code();
5254
mpl::log(mpl::Level::info, virtual_machine->vm_name, fmt::format("Cancelling delayed shutdown"));
5355
virtual_machine->state = VirtualMachine::State::running;
@@ -66,13 +68,21 @@ void mp::DelayedShutdownTimer::start(const std::chrono::milliseconds delay)
6668
fmt::format("Shutdown request delayed for {} minute{}",
6769
std::chrono::duration_cast<std::chrono::minutes>(delay).count(),
6870
delay > std::chrono::minutes(1) ? "s" : ""));
69-
ssh_session.exec(shutdown_message(std::chrono::duration_cast<std::chrono::minutes>(delay)));
71+
write_shutdown_message(ssh_session, std::chrono::duration_cast<std::chrono::minutes>(delay),
72+
virtual_machine->vm_name);
7073

7174
time_remaining = delay;
7275
std::chrono::minutes time_elapsed{1};
7376
QObject::connect(&shutdown_timer, &QTimer::timeout, [this, delay, time_elapsed]() mutable {
7477
time_remaining = delay - time_elapsed;
75-
ssh_session.exec(shutdown_message(std::chrono::duration_cast<std::chrono::minutes>(time_remaining)));
78+
79+
if (time_remaining <= std::chrono::minutes(5) ||
80+
time_remaining % std::chrono::minutes(5) == std::chrono::minutes::zero())
81+
{
82+
write_shutdown_message(ssh_session, std::chrono::duration_cast<std::chrono::minutes>(time_remaining),
83+
virtual_machine->vm_name);
84+
}
85+
7686
if (time_elapsed >= delay)
7787
{
7888
shutdown_timer.stop();
@@ -90,7 +100,7 @@ void mp::DelayedShutdownTimer::start(const std::chrono::milliseconds delay)
90100
}
91101
else
92102
{
93-
ssh_session.exec(shutdown_message(std::chrono::minutes::zero()));
103+
write_shutdown_message(ssh_session, std::chrono::minutes::zero(), virtual_machine->vm_name);
94104
shutdown_instance();
95105
}
96106
}

0 commit comments

Comments
 (0)