|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <mutex> |
| 4 | +#include <thread> |
| 5 | +#include <unordered_set> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +#include "dbconnector.h" |
| 9 | +#include "notificationproducer.h" |
| 10 | +#include "reboot_common.h" |
| 11 | +#include "reboot_interfaces.h" |
| 12 | +#include "redis_utils.h" |
| 13 | +#include "select.h" |
| 14 | +#include "selectableevent.h" |
| 15 | +#include "selectabletimer.h" |
| 16 | +#include "subscriberstatetable.h" |
| 17 | +#include "system/system.pb.h" |
| 18 | + |
| 19 | +namespace rebootbackend { |
| 20 | + |
| 21 | +// Holds a thread safe representation of the InitThread internal state. |
| 22 | +// Thread-safe: the expectation is one thread will write and multiple threads |
| 23 | +// will read. |
| 24 | +class InitThreadStatus { |
| 25 | + public: |
| 26 | + enum ThreadStatus { |
| 27 | + NOT_STARTED = 0, |
| 28 | + PENDING = 1, |
| 29 | + WAITING_FOR_REGISTRATION = 2, |
| 30 | + WAITING_FOR_RECONCILIATION = 3, |
| 31 | + WAITING_FOR_STATE_VERIFICATION = 4, |
| 32 | + WAITING_FOR_UNFREEZE = 5, |
| 33 | + FINALIZE = 6, |
| 34 | + DONE = 7, |
| 35 | + ERROR = 8, |
| 36 | + }; |
| 37 | + |
| 38 | + enum ErrorCondition { |
| 39 | + NO_ERROR = 0, |
| 40 | + UNKNOWN = 1, |
| 41 | + INTERNAL_ERROR = 2, |
| 42 | + REGISTRATION_FAILED = 3, |
| 43 | + RECONCILIATION_FAILED = 4, |
| 44 | + STATE_VERIFICATION_FAILED = 5, |
| 45 | + UNFREEZE_FAILED = 6, |
| 46 | + DETECTED_CRITICAL_STATE = 7, |
| 47 | + }; |
| 48 | + |
| 49 | + struct DetailedStatus { |
| 50 | + gnoi::system::RebootStatusResponse thread_state; |
| 51 | + InitThreadStatus::ThreadStatus detailed_thread_status = |
| 52 | + InitThreadStatus::ThreadStatus::NOT_STARTED; |
| 53 | + InitThreadStatus::ErrorCondition detailed_thread_error_condition = |
| 54 | + InitThreadStatus::ErrorCondition::NO_ERROR; |
| 55 | + }; |
| 56 | + |
| 57 | + InitThreadStatus() { |
| 58 | + m_status.detailed_thread_status = ThreadStatus::NOT_STARTED; |
| 59 | + m_status.detailed_thread_error_condition = ErrorCondition::NO_ERROR; |
| 60 | + |
| 61 | + m_status.thread_state.set_active(false); |
| 62 | + m_status.thread_state.set_method(gnoi::system::RebootMethod::COLD); |
| 63 | + m_status.thread_state.mutable_status()->set_status( |
| 64 | + gnoi::system::RebootStatus_Status::RebootStatus_Status_STATUS_SUCCESS); |
| 65 | + m_status.thread_state.mutable_status()->set_message(""); |
| 66 | + } |
| 67 | + |
| 68 | + void set_start_status() { |
| 69 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 70 | + m_status.detailed_thread_status = ThreadStatus::PENDING; |
| 71 | + m_status.detailed_thread_error_condition = ErrorCondition::NO_ERROR; |
| 72 | + |
| 73 | + m_status.thread_state.set_active(true); |
| 74 | + m_status.thread_state.set_method(gnoi::system::RebootMethod::NSF); |
| 75 | + m_status.thread_state.mutable_status()->set_status( |
| 76 | + gnoi::system::RebootStatus_Status::RebootStatus_Status_STATUS_UNKNOWN); |
| 77 | + m_status.thread_state.mutable_status()->set_message(""); |
| 78 | + } |
| 79 | + |
| 80 | + bool get_active(void) { |
| 81 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 82 | + return m_status.thread_state.active(); |
| 83 | + } |
| 84 | + |
| 85 | + void set_detailed_thread_status(ThreadStatus new_status) { |
| 86 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 87 | + if (m_status.thread_state.active()) { |
| 88 | + m_status.detailed_thread_status = new_status; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + void set_success() { |
| 93 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 94 | + if (m_status.thread_state.active()) { |
| 95 | + m_status.detailed_thread_status = ThreadStatus::DONE; |
| 96 | + m_status.thread_state.mutable_status()->set_status( |
| 97 | + gnoi::system::RebootStatus_Status:: |
| 98 | + RebootStatus_Status_STATUS_SUCCESS); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + void set_error(ErrorCondition error_condition, |
| 103 | + const std::string &error_message) { |
| 104 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 105 | + if (m_status.thread_state.active()) { |
| 106 | + m_status.detailed_thread_status = ThreadStatus::ERROR; |
| 107 | + m_status.detailed_thread_error_condition = error_condition; |
| 108 | + m_status.thread_state.mutable_status()->set_status( |
| 109 | + gnoi::system::RebootStatus_Status:: |
| 110 | + RebootStatus_Status_STATUS_FAILURE); |
| 111 | + m_status.thread_state.mutable_status()->set_message(error_message); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + void set_inactive() { |
| 116 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 117 | + m_status.thread_state.set_active(false); |
| 118 | + } |
| 119 | + |
| 120 | + DetailedStatus get_detailed_thread_status() { |
| 121 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 122 | + return m_status; |
| 123 | + } |
| 124 | + |
| 125 | + gnoi::system::RebootStatusResponse get_response() { |
| 126 | + const std::lock_guard<std::mutex> lock(m_mutex); |
| 127 | + return m_status.thread_state; |
| 128 | + } |
| 129 | + |
| 130 | + private: |
| 131 | + std::mutex m_mutex; |
| 132 | + DetailedStatus m_status; |
| 133 | +}; |
| 134 | + |
| 135 | + |
| 136 | +} // namespace rebootbackend |
0 commit comments