33 #include <sys/types.h>
48 if (application->getPid() != -1 || application->getStatusWriteBlock())
50 return std::future<void>();
53 application->setStatusWriteBlock(
true);
54 std::packaged_task<void()> task(std::bind(&
ApplicationStarter::startApplication, getStarter(application->getScenario()).get(), application, statusManager, commandLineParameters, printOnly));
56 std::future<void> result = task.get_future();
57 std::thread t(std::move(task));
71 if (application->getPid() != -1)
73 ARMARX_ERROR_S <<
"Could not deploy application " << application->getName() <<
" because it is still running. Please Stop it before deploying";
74 return std::future<void>();
77 application->setStatusWriteBlock(
true);
78 std::packaged_task<void()> task(std::bind(&
ApplicationStarter::deployApplication, getStarter(application->getScenario()).get(), application, statusManager, commandLineParameters, printOnly));
80 std::future<void> result = task.get_future();
81 std::thread t(std::move(task));
90 if (application->getStatusWriteBlock())
92 return std::future<void>();
95 application->setStatusWriteBlock(
true);
96 std::packaged_task<void()> task(std::bind(&
StopStrategy::stop, getStopStrategy(application->getScenario()).get(), application));
97 std::future<void> result = task.get_future();
98 std::thread(std::move(task)).detach();
105 if (application->getStatusWriteBlock())
107 return std::future<void>();
110 application->setStatusWriteBlock(
true);
111 std::packaged_task<void()> task(std::bind(&
StopStrategy::removeApplication, getStopStrategy(application->getScenario()).get(), application, statusManager));
112 std::future<void> result = task.get_future();
113 std::thread(std::move(task)).detach();
121 getStopStrategy(scenario)->stop(application);
124 auto state = getStarter(scenario)->getStatus(application, statusManager);
128 std::this_thread::sleep_for(std::chrono::milliseconds(100));
131 if (waitCount == 200)
133 ARMARX_INFO_S <<
"The application " << application->getName() <<
" is not Stopping please force kill it or try again. Aborting restart";
136 state = getStarter(scenario)->getStatus(application, statusManager);
139 ARMARX_INFO <<
"Starting application `" << application->getName() <<
"` which is `" << application->getExecutableAbsPath() <<
"`";
140 getStarter(scenario)->startApplication(application, statusManager,
"", printOnly);
145 if (application->getStatusWriteBlock())
147 return std::future<void>();
150 application->setStatusWriteBlock(
true);
152 std::packaged_task<void()> task(std::bind(&Executor::asyncApplicationRestart,
this, application, printOnly));
154 std::future<void> result = task.get_future();
155 std::thread(std::move(task)).detach();
162 if (application->getStatusWriteBlock())
166 return getStarter(application->getScenario())->getStatus(application, statusManager);
169 std::future<void>
Exec::Executor::startScenario(std::shared_ptr<Data_Structure::Scenario> scenario,
bool printOnly,
const std::string& commandLineParameters)
171 if (scenario->getStatusWriteBlock())
173 return std::future<void>();
176 scenario->setStatusWriteBlock(
true);
177 std::packaged_task<void()> task(std::bind(&
ApplicationStarter::startScenario, getStarter(scenario).get(), scenario, statusManager, commandLineParameters, printOnly));
179 std::future<void> result = task.get_future();
180 std::thread t(std::move(task));
194 if (scenario->getStatusWriteBlock())
196 return std::future<void>();
202 ARMARX_ERROR_S <<
"Could not deploy Scenario " << scenario->getName() <<
". Please Stop the Scenario before deploying";
203 return std::future<void>();
206 scenario->setStatusWriteBlock(
true);
207 std::packaged_task<void()> task(std::bind(&
ApplicationStarter::deployScenario, getStarter(scenario).get(), scenario, statusManager, commandLineParameters, printOnly));
209 std::future<void> result = task.get_future();
210 std::thread t(std::move(task));
218 std::vector<std::future<void>> futures;
220 std::vector<ApplicationInstancePtr> apps = *scenario->getApplications();
221 for (
auto it = apps.begin(); it != apps.end(); it++)
223 futures.push_back(stopApplication(*it));
226 for (
auto future = futures.begin(); future != futures.end(); ++future)
234 std::packaged_task<void()> task(std::bind(&Executor::asyncScenarioStop,
this, scenario));
235 std::future<void> result = task.get_future();
236 std::thread(std::move(task)).detach();
243 if (scenario->getStatusWriteBlock())
245 return std::future<void>();
248 scenario->setStatusWriteBlock(
true);
250 std::future<void> result = task.get_future();
251 std::thread(std::move(task)).detach();
258 std::vector<std::future<void>> futures;
259 for (
auto app : *scenario->getApplications())
261 futures.push_back(restartApplication(app, printOnly));
264 for (
auto future = futures.begin(); future != futures.end(); ++future)
272 std::packaged_task<void()> task(std::bind(&Executor::asyncScenarioRestart,
this, scenario, printOnly));
273 std::future<void> result = task.get_future();
274 std::thread(std::move(task)).detach();
281 std::filesystem::path xmlFilePath = std::filesystem::path(path) / std::filesystem::path(application->getPackageName() +
"." + application->getName() +
".xml");
283 if (reload || !std::filesystem::exists(xmlFilePath))
285 std::filesystem::create_directories(path);
287 std::string strCommand = application->getExecutableAbsPath().append(
" -p -f xml -o ").append(xmlFilePath.string());
288 int ret = system(strCommand.c_str());
291 ARMARX_WARNING <<
"Failed to generate properties xml for " << application->getName() <<
"\nCommand was: " << strCommand;
296 application->updateFound();
297 if (!application->getFound())
302 auto xmlDate = (std::filesystem::last_write_time(xmlFilePath));
304 auto execDate = std::filesystem::last_write_time(application->getPathToExecutable().append(
"/").append(application->getExecutableName()));
306 if (execDate > xmlDate)
308 loadAndSetCachedProperties(application, path,
true,
set);
316 application->setProperties(
parser.loadFromXml(xmlFilePath.string()));
322 defaultStopStrategy = strategy;
326 defaultStartStrategy = starter;
331 this->stopStrategy[scenario] = strategy;
336 this->starter[scenario] = starter;
341 if (starter.count(scenario) == 0)
343 return defaultStartStrategy;
345 return starter[scenario];
350 if (stopStrategy.count(scenario) == 0)
352 return defaultStopStrategy;
354 return stopStrategy[scenario];
359 return getStarter(application->getScenario())->isApplicationDeployed(application);
364 return getStarter(scenario)->isScenarioDeployed(scenario);
369 return defaultStopStrategy;