Module: check_mk
Branch: master
Commit: 795756da9233d53fc0ce540dc8575978d562911f
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=795756da9233d5…
Author: Sergey Kipnis <sk(a)mathias-kettner.de>
Date: Thu Mar 28 16:31:21 2019 +0100
[CMK-1791] - async plugins stopped also in standalone mode(adhoc)
- light refactoring(ctors moved to .cpp from .h and log output)
- removed excessive ctor
Change-Id: Ib258c809cd2cfc72e8ad6585cd3a1385bcc74079
---
agents/wnx/src/common/wtools.cpp | 14 ++++++++++++++
agents/wnx/src/common/wtools.h | 27 +++++++++------------------
agents/wnx/src/engine/windows_service_api.cpp | 18 +++++++++++++-----
agents/wnx/watest/test-service.cpp | 18 +++++++++---------
4 files changed, 45 insertions(+), 32 deletions(-)
diff --git a/agents/wnx/src/common/wtools.cpp b/agents/wnx/src/common/wtools.cpp
index 27b3016..5b104a2 100644
--- a/agents/wnx/src/common/wtools.cpp
+++ b/agents/wnx/src/common/wtools.cpp
@@ -76,6 +76,20 @@ uint32_t AppRunner::goExec(std::wstring CommandLine, bool Wait,
std::mutex ServiceController::s_lock_;
ServiceController* ServiceController::s_controller_ = nullptr;
+// normal API
+ServiceController::ServiceController(
+ std::unique_ptr<wtools::BaseServiceProcessor> Processor) {
+ if (nullptr == Processor) {
+ XLOG::l.crit("Processor is nullptr unique");
+ return;
+ }
+ std::lock_guard lk(s_lock_);
+ if (processor_ == nullptr && s_controller_ == nullptr) {
+ processor_ = std::move(Processor);
+ s_controller_ = this;
+ }
+}
+
void WINAPI ServiceController::ServiceMain(DWORD Argc, wchar_t** Argv) {
// Register the handler function for the service
XLOG::l.i("Service Main");
diff --git a/agents/wnx/src/common/wtools.h b/agents/wnx/src/common/wtools.h
index d480cc8..dd2bd95 100644
--- a/agents/wnx/src/common/wtools.h
+++ b/agents/wnx/src/common/wtools.h
@@ -261,15 +261,15 @@ public:
if (KillTreeToo) {
if (job_handle_) {
- // this is normal case but with job
+ // this is normal case but with job
TerminateJobObject(job_handle_, 0);
-
- // job:
- CloseHandle(job_handle_);
+
+ // job:
+ CloseHandle(job_handle_);
job_handle_ = nullptr;
-
- // process:
- CloseHandle(process_handle_); // must
+
+ // process:
+ CloseHandle(process_handle_); // must
process_handle_ = nullptr;
} else {
KillProcessTree(proc_id);
@@ -325,17 +325,8 @@ private:
// ptr, but this is clear overkill
public:
- // owns Processor
- ServiceController(BaseServiceProcessor* Processor) {
- assert(Processor); // #TODO replace with own crash and log
- if (!Processor) return;
-
- std::lock_guard lk(s_lock_);
- if (!processor_ && s_controller_ == nullptr) {
- processor_.reset(Processor);
- s_controller_ = this;
- }
- }
+ // normal API, used in production
+ ServiceController(std::unique_ptr<wtools::BaseServiceProcessor> Processor);
// no copy!
ServiceController(const ServiceController& Rhs) = delete;
diff --git a/agents/wnx/src/engine/windows_service_api.cpp
b/agents/wnx/src/engine/windows_service_api.cpp
index f5a1fa1..3b511e4 100644
--- a/agents/wnx/src/engine/windows_service_api.cpp
+++ b/agents/wnx/src/engine/windows_service_api.cpp
@@ -375,14 +375,20 @@ int ExecMainService(bool DuplicateOn) {
using namespace cma::install;
milliseconds Delay = 1000ms;
- auto processor = new ServiceProcessor(Delay, [](const void* Processor) {
+ auto processor =
+ std::make_unique<ServiceProcessor>(Delay, [](const void* Processor) {
+ // default embedded callback for exec
+ // atm does nothing
+ // optional commands listed here
+ // ********
+ // 1. Auto Update when msi file is located by specified address
#if 0
// #TODO planned test
CheckForUpdateFile(kDefaultMsiFileName, cma::cfg::GetUpdateDir(),
UpdateType::kMsiExecQuiet, true);
#endif
- return true;
- });
+ return true;
+ });
processor->startService();
@@ -538,8 +544,10 @@ int ServiceAsService(
// infinite loop to protect from exception
while (1) {
try {
- auto processor = new ServiceProcessor(Delay, InternalCallback);
- wtools::ServiceController service_controller(processor);
+ std::unique_ptr<wtools::BaseServiceProcessor> processor =
+ std::make_unique<ServiceProcessor>(Delay, InternalCallback);
+
+ wtools::ServiceController service_controller(std::move(processor));
auto ret = service_controller.registerAndRun(
cma::srv::kServiceName); // we will stay here till
// service will be stopped
diff --git a/agents/wnx/watest/test-service.cpp b/agents/wnx/watest/test-service.cpp
index d4cd5ac..ed98aea 100644
--- a/agents/wnx/watest/test-service.cpp
+++ b/agents/wnx/watest/test-service.cpp
@@ -37,12 +37,12 @@ int TestProcessor::s_counter = 0;
TEST(ServiceControllerTest, CreateDelete) {
using namespace std::chrono;
{
- auto processor = new TestProcessor;
- wtools::ServiceController controller(processor);
+ wtools::ServiceController controller(std::make_unique<TestProcessor>());
EXPECT_EQ(TestProcessor::s_counter, 1);
- EXPECT_FALSE(processor->started_ || processor->continued_ ||
- processor->paused_ || processor->shutdowned_ ||
- processor->stopped_);
+ auto p = dynamic_cast<TestProcessor*>(controller.processor_.get());
+ ASSERT_NE(nullptr, p);
+ EXPECT_FALSE(p->started_ || p->continued_ || p->paused_ ||
+ p->shutdowned_ || p->stopped_);
EXPECT_NE(controller.processor_, nullptr);
EXPECT_EQ(controller.name_, nullptr);
EXPECT_EQ(controller.can_stop_, false);
@@ -75,13 +75,13 @@ TEST(ServiceControllerTest, StartStop) {
using namespace cma::srv;
using namespace std::chrono;
int counter = 0;
- auto processor =
- new ServiceProcessor(100ms, [&counter](const void* Processor) {
+
+ wtools::ServiceController controller(std::make_unique<ServiceProcessor>(
+ 100ms, [&counter](const void* Processor) {
xlog::l("pip").print();
counter++;
return true;
- });
- wtools::ServiceController controller(processor);
+ }));
EXPECT_NE(controller.processor_, nullptr);
EXPECT_EQ(controller.name_, nullptr);
EXPECT_EQ(controller.can_stop_, false);