refactor: Add module::halt(error)

This commit is contained in:
Michael Carlberg 2016-10-25 07:07:22 +02:00
parent bc67e64e79
commit a0f0fc8723
2 changed files with 38 additions and 16 deletions

View File

@ -87,18 +87,24 @@ namespace modules {
try { try {
m_ipc.subscribe(i3ipc::ET_WORKSPACE); m_ipc.subscribe(i3ipc::ET_WORKSPACE);
m_ipc.prepare_to_event_handling(); m_ipc.prepare_to_event_handling();
} catch (std::runtime_error& e) { } catch (std::runtime_error& err) {
throw module_error(e.what()); throw module_error(err.what());
} catch (std::exception& err) {
throw module_error(err.what());
} }
// }}} // }}}
} }
void stop() { void stop() {
// Shutdown ipc connection when before stopping the module {{{ // Shutdown ipc connection when stopping the module {{{
try {
shutdown(m_ipc.get_event_socket_fd(), SHUT_RD);
shutdown(m_ipc.get_main_socket_fd(), SHUT_RD);
} catch (...) {
}
shutdown(m_ipc.get_event_socket_fd(), SHUT_RD);
shutdown(m_ipc.get_main_socket_fd(), SHUT_RD);
event_module::stop(); event_module::stop();
// }}} // }}}

View File

@ -152,6 +152,7 @@ namespace modules {
virtual void setup() = 0; virtual void setup() = 0;
virtual void start() = 0; virtual void start() = 0;
virtual void stop() = 0; virtual void stop() = 0;
virtual void halt(string error_message) = 0;
virtual string contents() = 0; virtual string contents() = 0;
virtual bool handle_event(string cmd) = 0; virtual bool handle_event(string cmd) = 0;
@ -214,7 +215,16 @@ namespace modules {
void setup() { void setup() {
m_log.trace("%s: Setup", name()); m_log.trace("%s: Setup", name());
CAST_MOD(Impl)->setup();
try {
CAST_MOD(Impl)->setup();
} catch (const module_error& err) {
m_log.err("%s: Setup failed", name());
CAST_MOD(Impl)->halt(err.what());
} catch (const std::exception& err) {
m_log.err("%s: Setup failed", name());
CAST_MOD(Impl)->halt(err.what());
}
} }
void stop() { void stop() {
@ -232,6 +242,12 @@ namespace modules {
m_terminator(name()); m_terminator(name());
} }
void halt(string error_message) {
m_log.err("%s: %s", name(), error_message);
m_log.warn("Stopping '%s'...", name());
stop();
}
void teardown() { void teardown() {
CAST_MOD(Impl)->wakeup(); CAST_MOD(Impl)->wakeup();
} }
@ -353,8 +369,8 @@ namespace modules {
using module<Impl>::module; using module<Impl>::module;
void start() { void start() {
CAST_MOD(Impl)->setup();
CAST_MOD(Impl)->enable(true); CAST_MOD(Impl)->enable(true);
CAST_MOD(Impl)->setup();
CAST_MOD(Impl)->broadcast(); CAST_MOD(Impl)->broadcast();
} }
@ -393,10 +409,10 @@ namespace modules {
} }
CAST_MOD(Impl)->sleep(m_interval); CAST_MOD(Impl)->sleep(m_interval);
} }
} catch (const module_error& err) {
CAST_MOD(Impl)->halt(err.what());
} catch (const std::exception& err) { } catch (const std::exception& err) {
this->m_log.err("%s: %s", CONST_MOD(Impl).name(), err.what()); CAST_MOD(Impl)->halt(err.what());
this->m_log.warn("Stopping '%s'...", CONST_MOD(Impl).name());
CAST_MOD(Impl)->stop();
} }
} }
}; };
@ -433,10 +449,10 @@ namespace modules {
else else
CAST_MOD(Impl)->broadcast(); CAST_MOD(Impl)->broadcast();
} }
} catch (const module_error& err) {
CAST_MOD(Impl)->halt(err.what());
} catch (const std::exception& err) { } catch (const std::exception& err) {
this->m_log.err("%s: %s", CONST_MOD(Impl).name(), err.what()); CAST_MOD(Impl)->halt(err.what());
this->m_log.warn("Stopping '%s'...", CONST_MOD(Impl).name());
CAST_MOD(Impl)->stop();
} }
} }
}; };
@ -464,10 +480,10 @@ namespace modules {
while (CAST_MOD(Impl)->enabled()) { while (CAST_MOD(Impl)->enabled()) {
CAST_MOD(Impl)->poll_events(); CAST_MOD(Impl)->poll_events();
} }
} catch (const module_error& err) {
CAST_MOD(Impl)->halt(err.what());
} catch (const std::exception& err) { } catch (const std::exception& err) {
this->m_log.err("%s: %s", CONST_MOD(Impl).name(), err.what()); CAST_MOD(Impl)->halt(err.what());
this->m_log.warn("Stopping '%s'...", CONST_MOD(Impl).name());
CAST_MOD(Impl)->stop();
} }
} }