From 0423f42d035121a23cddb72a99fb304eff7b635c Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 24 Jul 2012 15:38:30 +0200 Subject: [PATCH] Cleaned up status calculation code. --- cib/service.cpp | 17 +++++++++++ cib/servicestatusmessage.cpp | 10 ------- cib/servicestatusmessage.h | 3 -- components/checker/checkercomponent.cpp | 6 ---- components/cibsync/cibsynccomponent.cpp | 28 +------------------ .../convenience/conveniencecomponent.cpp | 3 ++ 6 files changed, 21 insertions(+), 46 deletions(-) diff --git a/cib/service.cpp b/cib/service.cpp index 75ec64a35..ff9ccd8d0 100644 --- a/cib/service.cpp +++ b/cib/service.cpp @@ -301,6 +301,12 @@ time_t Service::GetLastHardStateChange(void) const void Service::ApplyCheckResult(const CheckResult& cr) { + time_t now; + time(&now); + + ServiceState old_state = GetState(); + ServiceStateType old_stateType = GetStateType(); + long attempt = GetCurrentCheckAttempt(); if (cr.GetState() == StateOK) { @@ -320,6 +326,17 @@ void Service::ApplyCheckResult(const CheckResult& cr) SetCurrentCheckAttempt(attempt); SetState(cr.GetState()); + + SetLastCheckResult(cr); + + if (old_state != GetState()) { + SetLastStateChange(now); + + if (old_stateType != GetStateType()) + SetLastHardStateChange(now); + } + + UpdateNextCheck(); } ServiceState Service::StateFromString(const string& state) diff --git a/cib/servicestatusmessage.cpp b/cib/servicestatusmessage.cpp index 02c6a3f16..3b0e95adb 100644 --- a/cib/servicestatusmessage.cpp +++ b/cib/servicestatusmessage.cpp @@ -61,16 +61,6 @@ void ServiceStatusMessage::SetStateType(ServiceStateType type) Set("state_type", static_cast(type)); } -bool ServiceStatusMessage::GetCurrentCheckAttempt(long *attempt) const -{ - return Get("current_attempt", attempt); -} - -void ServiceStatusMessage::SetCurrentCheckAttempt(long attempt) -{ - Set("current_attempt", attempt); -} - bool ServiceStatusMessage::GetCheckResult(CheckResult *cr) const { Dictionary::Ptr obj; diff --git a/cib/servicestatusmessage.h b/cib/servicestatusmessage.h index 74fca85a9..6d67d1d3b 100644 --- a/cib/servicestatusmessage.h +++ b/cib/servicestatusmessage.h @@ -38,9 +38,6 @@ public: bool GetStateType(ServiceStateType *type) const; void SetStateType(ServiceStateType type); - bool GetCurrentCheckAttempt(long *attempt) const; - void SetCurrentCheckAttempt(long attempt); - bool GetCheckResult(CheckResult *cr) const; void SetCheckResult(CheckResult cr); }; diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp index 4ef7dda1a..24a437231 100644 --- a/components/checker/checkercomponent.cpp +++ b/components/checker/checkercomponent.cpp @@ -107,17 +107,11 @@ void CheckerComponent::CheckCompletedHandler(Service service, const ScriptTask:: if (vresult.IsObjectType()) { CheckResult result = CheckResult(static_cast(vresult)); - /* update service state */ - service.ApplyCheckResult(result); - RequestMessage rm; rm.SetMethod("checker::CheckResult"); ServiceStatusMessage params; params.SetService(service.GetName()); - params.SetState(service.GetState()); - params.SetStateType(service.GetStateType()); - params.SetCurrentCheckAttempt(service.GetCurrentCheckAttempt()); params.SetCheckResult(result); rm.SetParams(params); diff --git a/components/cibsync/cibsynccomponent.cpp b/components/cibsync/cibsynccomponent.cpp index d3fc478df..a3df2adea 100644 --- a/components/cibsync/cibsynccomponent.cpp +++ b/components/cibsync/cibsynccomponent.cpp @@ -91,35 +91,9 @@ void CIBSyncComponent::ServiceStatusRequestHandler(const Endpoint::Ptr& sender, Service service = Service::GetByName(svcname); - ServiceState state; - ServiceStateType stateType; - if (params.GetState(&state) && params.GetStateType(&stateType)) { - ServiceState old_state = service.GetState(); - ServiceStateType old_stateType = service.GetStateType(); - - if (state != old_state) { - time_t now; - time(&now); - - service.SetLastStateChange(now); - - if (old_stateType != stateType) - service.SetLastHardStateChange(now); - } - - service.SetState(state); - service.SetStateType(stateType); - } - - long attempt; - if (params.GetCurrentCheckAttempt(&attempt)) - service.SetCurrentCheckAttempt(attempt); - CheckResult cr; if (params.GetCheckResult(&cr)) - service.SetLastCheckResult(cr); - - service.UpdateNextCheck(); + service.ApplyCheckResult(cr); time_t now; time(&now); diff --git a/components/convenience/conveniencecomponent.cpp b/components/convenience/conveniencecomponent.cpp index 129d61f4c..6a21e6182 100644 --- a/components/convenience/conveniencecomponent.cpp +++ b/components/convenience/conveniencecomponent.cpp @@ -152,6 +152,9 @@ void ConvenienceComponent::HostCommittedHandler(const ConfigItem::Ptr& item) if (oldServices) { ConfigItem::Ptr service; BOOST_FOREACH(tie(tuples::ignore, service), oldServices) { + if (!service) + continue; + if (!newServices->Contains(service->GetName())) service->Unregister(); }