Split DynamicObject::OnConfigLoaded into two separate events

refs #7780
This commit is contained in:
Gunnar Beutner 2014-11-21 18:31:37 +01:00
parent b22d3c099a
commit 334e61037a
18 changed files with 60 additions and 61 deletions

View File

@ -182,6 +182,11 @@ void DynamicObject::OnConfigLoaded(void)
/* Nothing to do here. */
}
void DynamicObject::OnAllConfigLoaded(void)
{
/* Nothing to do here. */
}
void DynamicObject::OnStateLoaded(void)
{
/* Nothing to do here. */

View File

@ -77,6 +77,7 @@ public:
virtual void Resume(void);
virtual void OnConfigLoaded(void);
virtual void OnAllConfigLoaded(void);
virtual void OnStateLoaded(void);
template<typename T>

View File

@ -44,6 +44,7 @@ using namespace icinga;
boost::mutex ConfigItem::m_Mutex;
ConfigItem::TypeMap ConfigItem::m_Items;
ConfigItem::ItemList ConfigItem::m_UnnamedItems;
ConfigItem::ItemList ConfigItem::m_CommittedItems;
/**
* Constructor for the ConfigItem class.
@ -198,6 +199,13 @@ DynamicObject::Ptr ConfigItem::Commit(bool discard)
dobj->SetShortName(m_Name);
dobj->SetName(name);
dobj->OnConfigLoaded();
{
boost::mutex::scoped_lock lock(m_Mutex);
m_CommittedItems.push_back(this);
}
Dictionary::Ptr attrs = Serialize(dobj, FAConfig);
@ -315,8 +323,15 @@ bool ConfigItem::CommitNewItems(ParallelWorkQueue& upq)
if (ConfigCompilerContext::GetInstance()->HasErrors())
return false;
BOOST_FOREACH(const ItemPair& ip, items) {
upq.Enqueue(boost::bind(&DynamicObject::OnConfigLoaded, ip.first->m_Object));
std::vector<ConfigItem::Ptr> new_items;
{
boost::mutex::scoped_lock lock(m_Mutex);
new_items.swap(m_CommittedItems);
}
BOOST_FOREACH(const ConfigItem::Ptr& item, new_items) {
upq.Enqueue(boost::bind(&DynamicObject::OnAllConfigLoaded, item->m_Object));
}
upq.Join();

View File

@ -91,6 +91,7 @@ private:
typedef std::vector<ConfigItem::Ptr> ItemList;
static ItemList m_UnnamedItems;
static ItemList m_CommittedItems;
static ConfigItem::Ptr GetObjectUnlocked(const String& type,
const String& name);

View File

@ -1564,6 +1564,7 @@ Value ApiEvents::ExecuteCommandAPIHandler(const MessageOrigin& origin, const Dic
static_pointer_cast<DynamicObject>(host)->OnStateLoaded();
static_pointer_cast<DynamicObject>(host)->OnConfigLoaded();
static_pointer_cast<DynamicObject>(host)->OnAllConfigLoaded();
Dictionary::Ptr macros = params->Get("macros");

View File

@ -72,9 +72,7 @@ void Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons
builder->AddExpression(new OwnedExpression(rule.GetExpression()));
ConfigItem::Ptr dependencyItem = builder->Compile();
DynamicObject::Ptr dobj = dependencyItem->Commit();
dobj->OnConfigLoaded();
dependencyItem->Commit();
}
bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)

View File

@ -35,11 +35,11 @@ using namespace icinga;
REGISTER_TYPE(Host);
void Host::OnConfigLoaded(void)
void Host::OnAllConfigLoaded(void)
{
Checkable::OnConfigLoaded();
Checkable::OnAllConfigLoaded();
ASSERT(!OwnsLock());
HostGroup::EvaluateObjectRules(this);
Array::Ptr groups = GetGroups();
@ -56,7 +56,6 @@ void Host::OnConfigLoaded(void)
}
}
HostGroup::EvaluateObjectRules(this);
ScheduledDowntime::EvaluateApplyRules(this);
Notification::EvaluateApplyRules(this);
Dependency::EvaluateApplyRules(this);

View File

@ -68,7 +68,7 @@ public:
protected:
virtual void Stop(void);
virtual void OnConfigLoaded(void);
virtual void OnAllConfigLoaded(void);
private:
mutable boost::mutex m_ServicesMutex;

View File

@ -54,16 +54,8 @@ bool HostGroup::EvaluateObjectRule(const Host::Ptr& host, const ConfigItem::Ptr&
Log(LogDebug, "HostGroup")
<< "Assigning membership for group '" << group_name << "' to host '" << host->GetName() << "'";
HostGroup::Ptr groupObject = HostGroup::GetByName(group_name);
if (!groupObject) {
Log(LogCritical, "HostGroup")
<< "Invalid membership assignment. Group '" << group_name << "' does not exist.";
return false;
}
/* assign host group membership */
groupObject->ResolveGroupMembership(host, true);
Array::Ptr groups = host->GetGroups();
groups->Add(group_name);
return true;
}

View File

@ -71,9 +71,7 @@ void Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co
builder->AddExpression(new OwnedExpression(rule.GetExpression()));
ConfigItem::Ptr notificationItem = builder->Compile();
DynamicObject::Ptr dobj = notificationItem->Commit();
dobj->OnConfigLoaded();
notificationItem->Commit();
}
bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)

View File

@ -71,8 +71,7 @@ void ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl
builder->AddExpression(new OwnedExpression(rule.GetExpression()));
ConfigItem::Ptr downtimeItem = builder->Compile();
DynamicObject::Ptr dobj = downtimeItem->Commit();
dobj->OnConfigLoaded();
downtimeItem->Commit();
}
bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)

View File

@ -64,8 +64,7 @@ void Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam
builder->AddExpression(new OwnedExpression(rule.GetExpression()));
ConfigItem::Ptr serviceItem = builder->Compile();
DynamicObject::Ptr dobj = serviceItem->Commit();
dobj->OnConfigLoaded();
serviceItem->Commit();
}
bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)

View File

@ -41,8 +41,17 @@ String ServiceNameComposer::MakeName(const String& shortName, const Object::Ptr&
return service->GetHostName() + "!" + shortName;
}
void Service::OnConfigLoaded(void)
void Service::OnAllConfigLoaded(void)
{
Checkable::OnAllConfigLoaded();
m_Host = Host::GetByName(GetHostName());
if (m_Host)
m_Host->AddService(this);
ServiceGroup::EvaluateObjectRules(this);
Array::Ptr groups = GetGroups();
if (groups) {
@ -58,16 +67,6 @@ void Service::OnConfigLoaded(void)
}
}
m_Host = Host::GetByName(GetHostName());
if (m_Host)
m_Host->AddService(this);
SetSchedulingOffset(Utility::Random());
Checkable::OnConfigLoaded();
ServiceGroup::EvaluateObjectRules(this);
ScheduledDowntime::EvaluateApplyRules(this);
Notification::EvaluateApplyRules(this);
Dependency::EvaluateApplyRules(this);

View File

@ -56,7 +56,7 @@ public:
static void EvaluateApplyRules(const Host::Ptr& host);
protected:
virtual void OnConfigLoaded(void);
virtual void OnAllConfigLoaded(void);
private:
Host::Ptr m_Host;

View File

@ -57,16 +57,8 @@ bool ServiceGroup::EvaluateObjectRule(const Service::Ptr& service, const ConfigI
Log(LogDebug, "ServiceGroup")
<< "Assigning membership for group '" << group_name << "' to service '" << service->GetName() << "'";
ServiceGroup::Ptr groupObject = ServiceGroup::GetByName(group_name);
if (!groupObject) {
Log(LogCritical, "ServiceGroup")
<< "Invalid membership assignment. Group '" << group_name << "' does not exist.";
return false;
}
/* assign service group membership */
groupObject->ResolveGroupMembership(service, true);
Array::Ptr groups = service->GetGroups();
groups->Add(group_name);
return true;
}

View File

@ -35,8 +35,17 @@ boost::signals2::signal<void (const User::Ptr&, bool, const MessageOrigin&)> Use
void User::OnConfigLoaded(void)
{
DynamicObject::OnConfigLoaded();
SetTypeFilter(FilterArrayToInt(GetTypes(), ~0));
SetStateFilter(FilterArrayToInt(GetStates(), ~0));
}
void User::OnAllConfigLoaded(void)
{
DynamicObject::OnAllConfigLoaded();
UserGroup::EvaluateObjectRules(this);
Array::Ptr groups = GetGroups();
@ -52,8 +61,6 @@ void User::OnConfigLoaded(void)
ug->ResolveGroupMembership(this, true);
}
}
UserGroup::EvaluateObjectRules(this);
}
void User::Stop(void)

View File

@ -58,6 +58,7 @@ protected:
virtual void Stop(void);
virtual void OnConfigLoaded(void);
virtual void OnAllConfigLoaded(void);
private:
mutable boost::mutex m_UserMutex;
};

View File

@ -54,16 +54,8 @@ bool UserGroup::EvaluateObjectRule(const User::Ptr& user, const ConfigItem::Ptr&
Log(LogDebug, "UserGroup")
<< "Assigning membership for group '" << group_name << "' to user '" << user->GetName() << "'";
UserGroup::Ptr groupObject = UserGroup::GetByName(group_name);
if (!groupObject) {
Log(LogCritical, "UserGroup")
<< "Invalid membership assignment. Group '" << group_name << "' does not exist.";
return false;
}
/* assign user group membership */
groupObject->ResolveGroupMembership(user, true);
Array::Ptr groups = user->GetGroups();
groups->Add(group_name);
return true;
}