40 static std::mutex mutex;
47 static std::shared_mutex mutex;
53 application(
std::move(application)), statusManager(statusManager)
61 const std::string& commandLineParameters,
64 if (intent == Intent::None)
69 std::lock_guard<std::mutex> guard(mutex);
71 if (pending.intent != Intent::None && pending.intent != intent)
74 << application->getName() <<
"` with " <<
ToString(intent) <<
".";
77 pending = Request{intent, starter, stopStrategy, commandLineParameters, printOnly};
85 std::thread([self = shared_from_this()] { self->run(); }).detach();
92 std::lock_guard<std::mutex> guard(mutex);
93 return pending.intent != Intent::None ? pending.intent : current.intent;
105 std::unique_lock<std::mutex> guard(mutex);
106 idleCondition.wait(guard,
107 [
this] {
return !workerRunning && pending.intent == Intent::None; });
111ApplicationLifecycle::publishIntent()
113 application->setPendingIntent(pending.intent != Intent::None ? pending.intent : current.intent);
117ApplicationLifecycle::run()
123 std::lock_guard<std::mutex> guard(mutex);
124 if (pending.intent == Intent::None)
128 workerRunning =
false;
130 idleCondition.notify_all();
147 << application->getName()
152 std::lock_guard<std::mutex> guard(mutex);
160ApplicationLifecycle::execute(
const Request& request)
173 case Intent::Restart:
182ApplicationLifecycle::executeStart(
const Request& request)
186 ARMARX_INFO_S <<
"Application `" << application->getName() <<
"` is already running.";
194 if (supersededByStop())
197 <<
"`: a stop was requested while it was being prepared.";
205ApplicationLifecycle::executeStop(
const Request& request)
216ApplicationLifecycle::executeRestart(
const Request& request)
227 <<
"`: it did not shut down. Force kill it and try again.";
231 if (supersededByStop())
233 ARMARX_INFO_S <<
"Not restarting `" << application->getName()
234 <<
"`: a stop was requested while it was shutting down.";
242 if (supersededByStop())
245 <<
"`: a stop was requested while it was being prepared.";
253ApplicationLifecycle::prepare()
262ApplicationLifecycle::launch(
const Request& request)
266 std::lock_guard<std::mutex> serialize(
LaunchMutex());
267 request.starter->startApplication(
268 application, statusManager,
request.commandLineParameters,
request.printOnly);
272ApplicationLifecycle::stopAndWait(
const Request& request)
275 request.stopStrategy->stop(application);
279ApplicationLifecycle::isRunning()
281 const int pid = application->getPid();
297 application->getExecutableName(),
298 application->getConfigPath()) !=
304 ARMARX_INFO_S <<
"The pid " << pid <<
" remembered for application `"
305 << application->getName()
306 <<
"` has been recycled by an unrelated process. Forgetting it.";
311 if (application->compareAndResetPid(pid))
313 statusManager.savePid(application);
319ApplicationLifecycle::supersededByStop()
const
321 std::lock_guard<std::mutex> guard(mutex);
322 return pending.intent == Intent::Stop;
ApplicationLifecycle(Data_Structure::ApplicationInstancePtr application, StatusManager statusManager)
void request(Data_Structure::Intent intent, const ApplicationStarterPtr &starter, const StopStrategyPtr &stopStrategy, const std::string &commandLineParameters="", bool printOnly=false)
Requests that the application be started, stopped or restarted, superseding any intent that has not b...
Data_Structure::Intent effectiveIntent() const
void wait()
Blocks until nothing is requested or running any more.
static void loadAndSetCachedProperties(Data_Structure::ApplicationPtr application, std::string path, bool reload=false, bool set=true)
Generates an XML file of the given application and saves it in the specified path.
static std::string getCacheDir()
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Intent
What the ScenarioManager is currently trying to do with an application.
const char * ToString(Intent intent)
Human readable name of an intent, for logging.
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
@ Differs
argv says this is a different program, or the same program serving a different config.
Identity IdentifyProcess(int pid, const std::string &executableName, const std::string &configPath)
Checks whether the process with the given pid is still the application it is supposed to be.
std::shared_mutex & GlobalOperationMutex()
Held shared by every application operation, and exclusively by global operations that act on all arma...
std::shared_ptr< StopStrategy > StopStrategyPtr
std::mutex & LaunchMutex()
Serializes the actual spawning of applications.
std::shared_ptr< ApplicationStarter > ApplicationStarterPtr
std::string GetHandledExceptionString()