Terminate process when container manager disconnects or container start fails
This commit is contained in:
parent
7a2cef31da
commit
d11a281d6c
3 changed files with 26 additions and 3 deletions
|
|
@ -144,6 +144,10 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
|
|||
}));
|
||||
|
||||
container::Client container(rt);
|
||||
container.register_terminate_handler([&]() {
|
||||
WARNING("Lost connection to container manager, terminating.");
|
||||
trap->stop();
|
||||
});
|
||||
container::Configuration container_configuration;
|
||||
container_configuration.bind_mounts = {
|
||||
{qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"},
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@
|
|||
#include "anbox/container/client.h"
|
||||
#include "anbox/config.h"
|
||||
#include "anbox/container/management_api_stub.h"
|
||||
#include "anbox/logger.h"
|
||||
#include "anbox/network/local_socket_messenger.h"
|
||||
#include "anbox/rpc/channel.h"
|
||||
#include "anbox/rpc/message_processor.h"
|
||||
#include "anbox/rpc/pending_call_cache.h"
|
||||
#include "anbox/logger.h"
|
||||
|
||||
namespace ba = boost::asio;
|
||||
namespace bs = boost::system;
|
||||
|
|
@ -43,7 +43,17 @@ Client::Client(const std::shared_ptr<Runtime> &rt)
|
|||
Client::~Client() {}
|
||||
|
||||
void Client::start_container(const Configuration &configuration) {
|
||||
management_api_->start_container(configuration);
|
||||
try {
|
||||
management_api_->start_container(configuration);
|
||||
} catch (const std::exception &e) {
|
||||
ERROR("Failed to start container: %s", e.what());
|
||||
if (terminate_callback_)
|
||||
terminate_callback_();
|
||||
}
|
||||
}
|
||||
|
||||
void Client::register_terminate_handler(const TerminateCallback &callback) {
|
||||
terminate_callback_ = callback;
|
||||
}
|
||||
|
||||
void Client::read_next_message() {
|
||||
|
|
@ -54,7 +64,11 @@ void Client::read_next_message() {
|
|||
|
||||
void Client::on_read_size(const boost::system::error_code &error,
|
||||
std::size_t bytes_read) {
|
||||
if (error) BOOST_THROW_EXCEPTION(std::runtime_error(error.message()));
|
||||
if (error) {
|
||||
if (terminate_callback_)
|
||||
terminate_callback_();
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> data(bytes_read);
|
||||
std::copy(buffer_.data(), buffer_.data() + bytes_read, data.data());
|
||||
|
|
|
|||
|
|
@ -34,11 +34,15 @@ namespace container {
|
|||
class ManagementApiStub;
|
||||
class Client {
|
||||
public:
|
||||
typedef std::function<void()> TerminateCallback;
|
||||
|
||||
Client(const std::shared_ptr<Runtime> &rt);
|
||||
~Client();
|
||||
|
||||
void start_container(const Configuration &configuration);
|
||||
|
||||
void register_terminate_handler(const TerminateCallback &callback);
|
||||
|
||||
private:
|
||||
void read_next_message();
|
||||
void on_read_size(const boost::system::error_code &ec,
|
||||
|
|
@ -50,6 +54,7 @@ class Client {
|
|||
std::shared_ptr<ManagementApiStub> management_api_;
|
||||
std::shared_ptr<rpc::MessageProcessor> processor_;
|
||||
std::array<std::uint8_t, 8192> buffer_;
|
||||
TerminateCallback terminate_callback_;
|
||||
};
|
||||
} // namespace container
|
||||
} // namespace anbox
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue