32 #include <sys/types.h>
47 if (application->getPid() != -1 || application->getStatusWriteBlock())
49 return std::future<void>();
52 application->setStatusWriteBlock(
true);
53 std::packaged_task<void()> task(std::bind(&
ApplicationStarter::startApplication, getStarter(application->getScenario()).get(), application, statusManager, commandLineParameters, printOnly));
55 std::future<void> result = task.get_future();
56 std::thread t(std::move(task));
70 if (application->getStatusWriteBlock())
72 return std::future<void>();
75 application->setStatusWriteBlock(
true);
76 std::packaged_task<void()> task(std::bind(&
StopStrategy::stop, getStopStrategy(application->getScenario()).get(), application));
77 std::future<void> result = task.get_future();
78 std::thread(std::move(task)).detach();
86 getStopStrategy(scenario)->stop(application);
89 auto state = getStarter(scenario)->getStatus(application, statusManager);
93 std::this_thread::sleep_for(std::chrono::milliseconds(100));
98 ARMARX_INFO_S <<
"The application " << application->getName() <<
" is not Stopping please force kill it or try again. Aborting restart";
101 state = getStarter(scenario)->getStatus(application, statusManager);
104 ARMARX_INFO <<
"Starting application `" << application->getName() <<
"` which is `" << application->getExecutableAbsPath() <<
"`";
105 getStarter(scenario)->startApplication(application, statusManager,
"", printOnly);
110 if (application->getStatusWriteBlock())
112 return std::future<void>();
115 application->setStatusWriteBlock(
true);
117 std::packaged_task<void()> task(std::bind(&Executor::asyncApplicationRestart,
this, application, printOnly));
119 std::future<void> result = task.get_future();
120 std::thread(std::move(task)).detach();
127 if (application->getStatusWriteBlock())
131 return getStarter(application->getScenario())->getStatus(application, statusManager);
134 std::future<void>
Exec::Executor::startScenario(std::shared_ptr<Data_Structure::Scenario> scenario,
bool printOnly,
const std::string& commandLineParameters)
136 if (scenario->getStatusWriteBlock())
138 return std::future<void>();
141 scenario->setStatusWriteBlock(
true);
142 std::packaged_task<void()> task(std::bind(&
ApplicationStarter::startScenario, getStarter(scenario).get(), scenario, statusManager, commandLineParameters, printOnly));
144 std::future<void> result = task.get_future();
145 std::thread t(std::move(task));
159 std::vector<std::future<void>> futures;
161 std::vector<ApplicationInstancePtr> apps = *scenario->getApplications();
162 for (
auto it = apps.begin(); it != apps.end(); it++)
164 futures.push_back(stopApplication(*it));
167 for (
auto future = futures.begin(); future != futures.end(); ++future)
175 std::packaged_task<void()> task(std::bind(&Executor::asyncScenarioStop,
this, scenario));
176 std::future<void> result = task.get_future();
177 std::thread(std::move(task)).detach();
184 std::vector<std::future<void>> futures;
185 for (
auto app : *scenario->getApplications())
187 futures.push_back(restartApplication(app, printOnly));
190 for (
auto future = futures.begin(); future != futures.end(); ++future)
198 std::packaged_task<void()> task(std::bind(&Executor::asyncScenarioRestart,
this, scenario, printOnly));
199 std::future<void> result = task.get_future();
200 std::thread(std::move(task)).detach();
207 std::filesystem::path xmlFilePath = std::filesystem::path(path) / std::filesystem::path(application->getPackageName() +
"." + application->getName() +
".xml");
209 if (reload || !std::filesystem::exists(xmlFilePath))
211 std::filesystem::create_directories(path);
213 std::string strCommand = application->getExecutableAbsPath().append(
" -p -f xml -o ").append(xmlFilePath.string());
214 int ret = system(strCommand.c_str());
217 ARMARX_WARNING <<
"Failed to generate properties xml for " << application->getName() <<
"\nCommand was: " << strCommand;
222 application->updateFound();
223 if (!application->getFound())
228 auto xmlDate = (std::filesystem::last_write_time(xmlFilePath));
230 auto execDate = std::filesystem::last_write_time(application->getPathToExecutable().append(
"/").append(application->getExecutableName()));
232 if (execDate > xmlDate)
234 loadAndSetCachedProperties(application, path,
true,
set);
242 application->setProperties(
parser.loadFromXml(xmlFilePath.string()));
248 defaultStopStrategy = strategy;
252 defaultStartStrategy = appStarter;
257 this->stopStrategy[scenario] = strategy;
262 this->starter[scenario] = appStarter;
267 if (starter.count(scenario) == 0)
269 return defaultStartStrategy;
271 return starter[scenario];
276 if (stopStrategy.count(scenario) == 0)
278 return defaultStopStrategy;
280 return stopStrategy[scenario];
285 return defaultStopStrategy;