OnUnregistered(GetSelf());
}
-ScriptTask::Ptr DynamicObject::InvokeMethod(const String& method,
- const vector<Value>& arguments, ScriptTask::CompletionCallback callback)
+ScriptTask::Ptr DynamicObject::MakeMethodTask(const String& method,
+ const vector<Value>& arguments)
{
Value value = Get("methods");
if (!func)
BOOST_THROW_EXCEPTION(invalid_argument("Function '" + funcName + "' does not exist."));
- ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
- task->Start(callback);
-
- return task;
+ return boost::make_shared<ScriptTask>(func, arguments);
}
/*
static signals2::signal<void (const DynamicObject::Ptr&)> OnUnregistered;
static signals2::signal<void (double, const set<DynamicObject *>&)> OnTransactionClosing;
- ScriptTask::Ptr InvokeMethod(const String& method,
- const vector<Value>& arguments, ScriptTask::CompletionCallback callback);
+ ScriptTask::Ptr MakeMethodTask(const String& method,
+ const vector<Value>& arguments);
shared_ptr<DynamicType> GetType(void) const;
String GetName(void) const;
void ScriptTask::Run(void)
{
- ObjectLock olock(this);
m_Function->Invoke(GetSelf(), m_Arguments);
}
Logger::Write(LogInformation, "icinga", "Hello from the Icinga 2 API: " + text);
- task->FinishResult(42);
+ {
+ ObjectLock olock(task);
+ task->FinishResult(42);
+ }
}
vector<Value> arguments;
arguments.push_back(static_cast<Notification::Ptr>(GetSelf()));
arguments.push_back(type);
- ScriptTask::Ptr task;
- task = InvokeMethod("notify", arguments, boost::bind(&Notification::NotificationCompletedHandler, this, _1));
+
+ ScriptTask::Ptr task = MakeMethodTask("notify", arguments);
if (!task) {
Logger::Write(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
return;
}
- if (!task->IsFinished()) {
- /* We need to keep the task object alive until the completion handler is called. */
+ /* We need to keep the task object alive until the completion handler is called. */
+ m_Tasks.insert(task);
- m_Tasks.insert(task);
- }
+ task->Start(boost::bind(&Notification::NotificationCompletedHandler, this, _1));
}
void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
Dictionary::Ptr cr = boost::make_shared<Dictionary>();
cr->Set("state", StateUnknown);
- task->FinishResult(cr);
+ {
+ ObjectLock olock(task);
+ task->FinishResult(cr);
+ }
}
try {
pr = ct.m_Process->GetResult();
} catch (...) {
- ct.m_Task->FinishException(boost::current_exception());
+ {
+ ObjectLock olock(ct.m_Task);
+ ct.m_Task->FinishException(boost::current_exception());
+ }
+
return;
}
result->Set("execution_start", pr.ExecutionStart);
result->Set("execution_end", pr.ExecutionEnd);
- ct.m_Task->FinishResult(result);
+ {
+ ObjectLock olock(ct.m_Task);
+ ct.m_Task->FinishResult(result);
+ }
}
ServiceState PluginCheckTask::ExitStatusToState(int exitStatus)
Logger::Write(LogWarning, "icinga", msgbuf.str());
}
- ct.m_Task->FinishResult(Empty);
+ {
+ ObjectLock olock(ct.m_Task);
+ ct.m_Task->FinishResult(Empty);
+ }
} catch (...) {
- ct.m_Task->FinishException(boost::current_exception());
+ {
+ ObjectLock olock(ct.m_Task);
+ ct.m_Task->FinishException(boost::current_exception());
+ }
+
return;
}
}
scheduleInfo->Set("schedule_start", GetNextCheck());
scheduleInfo->Set("execution_start", Utility::GetTime());
- try {
- vector<Value> arguments;
- arguments.push_back(static_cast<Service::Ptr>(GetSelf()));
- ScriptTask::Ptr task;
- task = InvokeMethod("check", arguments, boost::bind(&Service::CheckCompletedHandler, this, scheduleInfo, _1, callback));
-
- if (!task->IsFinished())
- Set("current_task", task);
- } catch (...) {
- /* something went wrong while setting up the method call -
- * reschedule the service and call the callback anyway. */
-
- UpdateNextCheck();
+ vector<Value> arguments;
+ arguments.push_back(static_cast<Service::Ptr>(GetSelf()));
- callback();
+ ScriptTask::Ptr task = MakeMethodTask("check", arguments);
+ Set("current_task", task);
- throw;
- }
+ task->Start(boost::bind(&Service::CheckCompletedHandler, this, scheduleInfo, _1, callback));
}
void Service::CheckCompletedHandler(const Dictionary::Ptr& scheduleInfo,