Skip to content

Commit

Permalink
refactor with main
Browse files Browse the repository at this point in the history
  • Loading branch information
sharder996 authored and Chris Townsend committed Mar 21, 2024
1 parent f4e5506 commit 5a95aa3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
2 changes: 2 additions & 0 deletions src/client/cli/cmd/stop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ mp::ParseCode cmd::Stop::parse_args(mp::ArgParser* parser)
return ParseCode::CommandLineError;
}

request.set_force_stop(parser->isSet(force_option));

auto time = parser->value(time_option);

if (time.startsWith("+"))
Expand Down
40 changes: 8 additions & 32 deletions src/platform/backends/lxd/lxd_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,33 +262,6 @@ void mp::LXDVirtualMachine::start()

void mp::LXDVirtualMachine::stop(bool force)
{
if (force)
{
auto present_state = current_state();

if (present_state != State::stopped && present_state != State::off)
{
const QJsonObject state_json{{"action", "stop"}, {"force", true}};

auto state_task = lxd_request(manager, "PUT", state_url(), state_json, 5000);

try
{
lxd_wait(manager, base_url, state_task, 60000);
}
catch (const LXDNotFoundException&)
{
}

state = State::stopped;
}

if (update_shutdown_status)
update_state();

return;
}

std::unique_lock<decltype(state_mutex)> lock{state_mutex};
auto present_state = current_state();

Expand All @@ -297,14 +270,13 @@ void mp::LXDVirtualMachine::stop(bool force)
mpl::log(mpl::Level::debug, vm_name, "Ignoring stop request since instance is already stopped");
return;
}

if (present_state == State::suspended)
else if (present_state == State::suspended && !force)
{
mpl::log(mpl::Level::info, vm_name, fmt::format("Ignoring shutdown issued while suspended"));
return;
}

request_state("stop");
request_state("stop", {{"force", force}});

state = State::stopped;

Expand Down Expand Up @@ -439,9 +411,13 @@ const QUrl mp::LXDVirtualMachine::network_leases_url()
return base_url.toString() + "/networks/" + bridge_name + "/leases";
}

void mp::LXDVirtualMachine::request_state(const QString& new_state)
void mp::LXDVirtualMachine::request_state(const QString& new_state, const QJsonObject args)
{
const QJsonObject state_json{{"action", new_state}};
QJsonObject state_json{{"action", new_state}};
for (auto it = args.constBegin(); it != args.constEnd(); it++)
{
state_json.insert(it.key(), it.value());
}

auto state_task = lxd_request(manager, "PUT", state_url(), state_json, 5000);

Expand Down
2 changes: 1 addition & 1 deletion src/platform/backends/lxd/lxd_virtual_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class LXDVirtualMachine : public BaseVirtualMachine
const QUrl url();
const QUrl state_url();
const QUrl network_leases_url();
void request_state(const QString& new_state);
void request_state(const QString& new_state, const QJsonObject args = {});
};
} // namespace multipass
#endif // MULTIPASS_LXD_VIRTUAL_MACHINE_H
15 changes: 4 additions & 11 deletions src/platform/backends/qemu/qemu_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,21 +357,14 @@ void mp::QemuVirtualMachine::shutdown(bool force)
if (vm_process)
{
mpl::log(mpl::Level::info, vm_name, "Killing process");
try
{
vm_process->kill();
}
catch (std::exception&)
{
}
vm_process->kill();
}
else
{
mpl::log(mpl::Level::info, vm_name, "No process to kill");

return;
}
}

if (state == State::suspended)
else if (state == State::suspended)
{
mpl::log(mpl::Level::info, vm_name, fmt::format("Ignoring shutdown issued while suspended"));
}
Expand Down

0 comments on commit 5a95aa3

Please sign in to comment.