Skip to content

Commit

Permalink
Implement ReloadTimeout constant and wait for enqueued checks on Stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Friedrich committed Feb 25, 2019
1 parent 89634c2 commit ab7a799
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/17-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ Constant | Description
--------------------|-------------------
Vars |**Read-write.** Contains a dictionary with global custom attributes. Not set by default.
NodeName |**Read-write.** Contains the cluster node name. Set to the local hostname by default.
ReloadTimeout |**Read-write.** Defines the reload timeout for child processes. Defaults to `300s`.
Environment |**Read-write.** The name of the Icinga environment. Included in the SNI host name for outbound connections. Not set by default.
RunAsUser |**Read-write.** Defines the user the Icinga 2 daemon is running as. Set in the Icinga 2 sysconfig.
RunAsGroup |**Read-write.** Defines the group the Icinga 2 daemon is running as. Set in the Icinga 2 sysconfig.
Expand Down
17 changes: 14 additions & 3 deletions lib/base/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ void Application::OnConfigLoaded()

ASSERT(m_Instance == nullptr);
m_Instance = this;

String reloadTimeout;

if (ScriptGlobal::Exists("ReloadTimeout"))
reloadTimeout = ScriptGlobal::Get("ReloadTimeout");

if (!reloadTimeout.IsEmpty())
Configuration::ReloadTimeout = Convert::ToDouble(reloadTimeout);
}

/**
Expand Down Expand Up @@ -401,8 +409,6 @@ static void ReloadProcessCallback(const ProcessResult& pr)

pid_t Application::StartReloadProcess()
{
Log(LogInformation, "Application", "Got reload command: Starting new instance.");

// prepare arguments
ArrayData args;
args.push_back(GetExePath(m_ArgV[0]));
Expand All @@ -422,9 +428,14 @@ pid_t Application::StartReloadProcess()
#endif /* _WIN32 */

Process::Ptr process = new Process(Process::PrepareCommand(new Array(std::move(args))));
process->SetTimeout(300);
process->SetTimeout(Configuration::ReloadTimeout);
process->Run(&ReloadProcessCallback);

Log(LogInformation, "Application")
<< "Got reload command: Started new instance with PID '"
<< (unsigned long)(process->GetPID()) << "' (timeout is "
<< Configuration::ReloadTimeout << "s).";

return process->GetPID();
}

Expand Down
11 changes: 11 additions & 0 deletions lib/base/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ String Configuration::PidPath;
String Configuration::PkgDataDir;
String Configuration::PrefixDir;
String Configuration::ProgramData;
double Configuration::ReloadTimeout{300};
int Configuration::RLimitFiles;
int Configuration::RLimitProcesses;
int Configuration::RLimitStack;
Expand Down Expand Up @@ -240,6 +241,16 @@ void Configuration::SetProgramData(const String& val, bool suppress_events, cons
HandleUserWrite("ProgramData", &Configuration::ProgramData, val, m_ReadOnly);
}

double Configuration::GetReloadTimeout() const
{
return Configuration::ReloadTimeout;
}

void Configuration::SetReloadTimeout(double val, bool suppress_events, const Value& cookie)
{
HandleUserWrite("ReloadTimeout", &Configuration::ReloadTimeout, val, m_ReadOnly);
}

int Configuration::GetRLimitFiles() const
{
return Configuration::RLimitFiles;
Expand Down
4 changes: 4 additions & 0 deletions lib/base/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class Configuration : public ObjectImpl<Configuration>
String GetProgramData() const override;
void SetProgramData(const String& value, bool suppress_events = false, const Value& cookie = Empty) override;

double GetReloadTimeout() const override;
void SetReloadTimeout(double value, bool suppress_events = false, const Value& cookie = Empty) override;

int GetRLimitFiles() const override;
void SetRLimitFiles(int value, bool suppress_events = false, const Value& cookie = Empty) override;

Expand Down Expand Up @@ -147,6 +150,7 @@ class Configuration : public ObjectImpl<Configuration>
static String PkgDataDir;
static String PrefixDir;
static String ProgramData;
static double ReloadTimeout;
static int RLimitFiles;
static int RLimitProcesses;
static int RLimitStack;
Expand Down
5 changes: 5 additions & 0 deletions lib/base/configuration.ti
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ abstract class Configuration
set;
};

[config, no_storage, virtual] double ReloadTimeout {
get;
set;
};

[config, no_storage, virtual] int RLimitFiles {
get;
set;
Expand Down
29 changes: 26 additions & 3 deletions lib/checker/checkercomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,41 @@ void CheckerComponent::Start(bool runtimeCreated)

void CheckerComponent::Stop(bool runtimeRemoved)
{
Log(LogInformation, "CheckerComponent")
<< "'" << GetName() << "' stopped.";

{
boost::mutex::scoped_lock lock(m_Mutex);
m_Stopped = true;
m_CV.notify_all();
}

double wait = 0.0;

while (GetPendingCheckables() > 0) {
Log(LogDebug, "CheckerComponent")
<< "Waiting for running checks (" << GetPendingCheckables()
<< ") to finish. Waited for " << wait << " seconds now.";

Utility::Sleep(0.1);
wait += 0.1;

/* Pick a timeout slightly shorther than the process reload timeout. */
double waitMax = Configuration::ReloadTimeout - 30;
if (waitMax <= 0)
waitMax = 1;

if (wait > waitMax) {
Log(LogWarning, "CheckerComponent")
<< "Checks running too long for " << wait
<< " seconds, hard shutdown before reload timeout: " << Configuration::ReloadTimeout << ".";
break;
}
}

m_ResultTimer->Stop();
m_Thread.join();

Log(LogInformation, "CheckerComponent")
<< "'" << GetName() << "' stopped.";

ObjectImpl<CheckerComponent>::Stop(runtimeRemoved);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/remote/configpackageutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void ConfigPackageUtility::AsyncTryActivateStage(const String& packageName, cons
args->Add("ActiveStageOverride=" + packageName + ":" + stageName);

Process::Ptr process = new Process(Process::PrepareCommand(args));
process->SetTimeout(300);
process->SetTimeout(Configuration::ReloadTimeout);
process->Run(std::bind(&TryActivateStageCallback, _1, packageName, stageName, reload));
}

Expand Down

0 comments on commit ab7a799

Please sign in to comment.