@@ -33,9 +33,23 @@ namespace OpenWifi {
3333
3434 void MicroService::Exit (int Reason) { std::exit (Reason); }
3535
36+ static std::string MakeServiceListString (const Types::MicroServiceMetaMap &Services) {
37+ std::string SvcList;
38+ for (const auto &Svc : Services) {
39+ if (SvcList.empty ())
40+ SvcList = Svc.second .Type ;
41+ else
42+ SvcList += " , " + Svc.second .Type ;
43+ }
44+ return SvcList;
45+ }
46+
3647 void MicroService::BusMessageReceived ([[maybe_unused]] const std::string &Key,
3748 const std::string &Payload) {
3849 std::lock_guard G (InfraMutex_);
50+
51+ Poco::Logger &BusLogger = EventBusManager ()->Logger ();
52+
3953 try {
4054 Poco::JSON::Parser P;
4155 auto Object = P.parse (Payload).extract <Poco::JSON::Object::Ptr>();
@@ -55,28 +69,18 @@ namespace OpenWifi {
5569 Object->has (KafkaTopics::ServiceEvents::Fields::KEY)) {
5670 auto PrivateEndPoint =
5771 Object->get (KafkaTopics::ServiceEvents::Fields::PRIVATE).toString ();
58- if (Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE &&
59- Services_.find (PrivateEndPoint) != Services_.end ()) {
60- Services_[PrivateEndPoint].LastUpdate = Utils::Now ();
61- } else if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) {
72+ if (Event == KafkaTopics::ServiceEvents::EVENT_LEAVE) {
6273 Services_.erase (PrivateEndPoint);
63- poco_debug (
64- logger () ,
74+ poco_information (
75+ BusLogger ,
6576 fmt::format (
6677 " Service {} ID={} leaving system." ,
6778 Object->get (KafkaTopics::ServiceEvents::Fields::PRIVATE)
6879 .toString (),
6980 ID));
7081 } else if (Event == KafkaTopics::ServiceEvents::EVENT_JOIN ||
7182 Event == KafkaTopics::ServiceEvents::EVENT_KEEP_ALIVE) {
72- poco_debug (
73- logger (),
74- fmt::format (
75- " Service {} ID={} joining system." ,
76- Object->get (KafkaTopics::ServiceEvents::Fields::PRIVATE)
77- .toString (),
78- ID));
79- Services_[PrivateEndPoint] = Types::MicroServiceMeta{
83+ auto ServiceInfo = Types::MicroServiceMeta{
8084 .Id = ID,
8185 .Type = Poco::toLower (
8286 Object->get (KafkaTopics::ServiceEvents::Fields::TYPE)
@@ -94,20 +98,46 @@ namespace OpenWifi {
9498 .toString (),
9599 .LastUpdate = Utils::Now ()};
96100
97- std::string SvcList;
98- for (const auto &Svc : Services_) {
99- if (SvcList.empty ())
100- SvcList = Svc.second .Type ;
101- else
102- SvcList += " , " + Svc.second .Type ;
101+ auto s1 = MakeServiceListString (Services_);
102+ auto PreviousSize = Services_.size ();
103+ Services_[PrivateEndPoint] = ServiceInfo;
104+ auto CurrentSize = Services_.size ();
105+ if (Event == KafkaTopics::ServiceEvents::EVENT_JOIN) {
106+ if (!s1.empty ()) {
107+ poco_information (
108+ BusLogger,
109+ fmt::format (
110+ " Service {} ID={} is joining the system." ,
111+ Object
112+ ->get (
113+ KafkaTopics::ServiceEvents::Fields::PRIVATE)
114+ .toString (),
115+ ID));
116+ }
117+ std::string SvcList;
118+ for (const auto &Svc : Services_) {
119+ if (SvcList.empty ())
120+ SvcList = Svc.second .Type ;
121+ else
122+ SvcList += " , " + Svc.second .Type ;
123+ }
124+ poco_information (
125+ BusLogger,
126+ fmt::format (" Current list of microservices: {}" , SvcList));
127+ } else if (CurrentSize!=PreviousSize) {
128+ poco_information (
129+ BusLogger,
130+ fmt::format (
131+ " Service {} ID={} is being added back in." ,
132+ Object
133+ ->get (KafkaTopics::ServiceEvents::Fields::PRIVATE)
134+ .toString (),
135+ ID));
103136 }
104- poco_information (
105- logger (),
106- fmt::format (" Current list of microservices: {}" , SvcList));
107137 }
108138 } else {
109- poco_error (
110- logger () ,
139+ poco_information (
140+ BusLogger ,
111141 fmt::format (" KAFKA-MSG: invalid event '{}', missing a field." ,
112142 Event));
113143 }
@@ -118,32 +148,39 @@ namespace OpenWifi {
118148 Object->get (KafkaTopics::ServiceEvents::Fields::TOKEN).toString ());
119149#endif
120150 } else {
121- poco_error (
122- logger () ,
151+ poco_information (
152+ BusLogger ,
123153 fmt::format (" KAFKA-MSG: invalid event '{}', missing token" , Event));
124154 }
125155 } else {
126- poco_error ( logger () ,
156+ poco_information (BusLogger ,
127157 fmt::format (" Unknown Event: {} Source: {}" , Event, ID));
128158 }
129159 }
130160 } else {
131- poco_error ( logger (), " Bad bus message. " ) ;
132- std::ostringstream os ;
133- Object-> stringify (std::cout );
161+ std::ostringstream os ;
162+ Object-> stringify ( std::cout) ;
163+ poco_error (BusLogger, fmt::format ( " Bad bus message: {} " , os. str ()) );
134164 }
135165
136- auto i = Services_.begin ();
166+ auto ServiceHint = Services_.begin ();
137167 auto now = Utils::Now ();
138- for (; i != Services_.end ();) {
139- if ((now - i->second .LastUpdate ) > 60 ) {
140- i = Services_.erase (i);
168+ auto si1 = Services_.size ();
169+ auto ss1 = MakeServiceListString (Services_);
170+ while (ServiceHint!=Services_.end ()) {
171+ if ((now - ServiceHint->second .LastUpdate ) > 120 ) {
172+ poco_information (BusLogger, fmt::format (" ZombieService: Removing service {}, " , ServiceHint->second .PublicEndPoint ));
173+ ServiceHint = Services_.erase (ServiceHint);
141174 } else
142- ++i ;
175+ ++ServiceHint ;
143176 }
177+ if (Services_.size () != si1) {
178+ auto ss2 = MakeServiceListString (Services_);
179+ poco_information (BusLogger, fmt::format (" Current list of microservices: {} -> {}" , ss1, ss2));
180+ }
144181
145182 } catch (const Poco::Exception &E) {
146- logger () .log (E);
183+ BusLogger .log (E);
147184 }
148185 }
149186
@@ -412,7 +449,7 @@ namespace OpenWifi {
412449 try {
413450 DataDir.createDirectory ();
414451 } catch (const Poco::Exception &E) {
415- logger () .log (E);
452+ Logger_ .log (E);
416453 }
417454 }
418455 WWWAssetsDir_ = ConfigPath (" openwifi.restapi.wwwassets" , " " );
@@ -530,14 +567,12 @@ namespace OpenWifi {
530567 for (auto i : SubSystems_) {
531568 i->Start ();
532569 }
533- EventBusManager_ = std::make_unique<EventBusManager>(Poco::Logger::create (
534- " EventBusManager" , Poco::Logger::root ().getChannel (), Poco::Logger::root ().getLevel ()));
535- EventBusManager_->Start ();
570+ EventBusManager ()->Start ();
536571 }
537572
538573 void MicroService::StopSubSystemServers () {
539574 AddActivity (" Stopping" );
540- EventBusManager_ ->Stop ();
575+ EventBusManager () ->Stop ();
541576 for (auto i = SubSystems_.rbegin (); i != SubSystems_.rend (); ++i) {
542577 (*i)->Stop ();
543578 }
@@ -697,7 +732,7 @@ namespace OpenWifi {
697732 auto APIKEY = Request.get (" X-API-KEY" );
698733 return APIKEY == MyHash_;
699734 } catch (const Poco::Exception &E) {
700- logger () .log (E);
735+ Logger_ .log (E);
701736 }
702737 return false ;
703738 }
0 commit comments