51 const std::string& commandLineParameters)
53 if (application->getPid() != -1 || application->getStatusWriteBlock())
55 return std::future<void>();
58 application->setStatusWriteBlock(
true);
63 commandLineParameters,
66 std::future<void> result = task.get_future();
67 std::thread t(std::move(task));
82 if (application->getStatusWriteBlock())
84 return std::future<void>();
87 application->setStatusWriteBlock(
true);
91 std::future<void> result = task.get_future();
92 std::thread(std::move(task)).detach();
101 getStopStrategy(scenario)->stop(application);
104 auto state = getStarter(scenario)->getStatus(application, statusManager);
108 std::this_thread::sleep_for(std::chrono::milliseconds(100));
111 if (waitCount == 200)
113 ARMARX_INFO_S <<
"The application " << application->getName()
114 <<
" is not Stopping please force kill it or try again. Aborting restart";
117 state = getStarter(scenario)->getStatus(application, statusManager);
120 ARMARX_INFO <<
"Starting application `" << application->getName() <<
"` which is `"
121 << application->getExecutableAbsPath() <<
"`";
122 getStarter(scenario)->startApplication(application, statusManager,
"", printOnly);
128 if (application->getStatusWriteBlock())
130 return std::future<void>();
133 application->setStatusWriteBlock(
true);
135 std::packaged_task<void()> task(std::bind(
136 &Executor::asyncApplicationRestart,
this, application, printOnly));
138 std::future<void> result = task.get_future();
139 std::thread(std::move(task)).detach();
147 if (application->getStatusWriteBlock())
151 return getStarter(application->getScenario())->getStatus(application, statusManager);
157 const std::string& commandLineParameters)
159 if (scenario->getStatusWriteBlock())
161 return std::future<void>();
164 scenario->setStatusWriteBlock(
true);
169 commandLineParameters,
172 std::future<void> result = task.get_future();
173 std::thread t(std::move(task));
188 std::vector<std::future<void>> futures;
190 std::vector<ApplicationInstancePtr> apps = *scenario->getApplications();
191 for (
auto it = apps.begin(); it != apps.end(); it++)
193 futures.push_back(stopApplication(*it));
196 for (
auto future = futures.begin(); future != futures.end(); ++future)
205 std::packaged_task<void()> task(
206 std::bind(&Executor::asyncScenarioStop,
this, scenario));
207 std::future<void> result = task.get_future();
208 std::thread(std::move(task)).detach();
216 std::vector<std::future<void>> futures;
217 for (
auto app : *scenario->getApplications())
219 futures.push_back(restartApplication(app, printOnly));
222 for (
auto future = futures.begin(); future != futures.end(); ++future)
231 std::packaged_task<void()> task(
232 std::bind(&Executor::asyncScenarioRestart,
this, scenario, printOnly));
233 std::future<void> result = task.get_future();
234 std::thread(std::move(task)).detach();
245 std::filesystem::path xmlFilePath =
246 std::filesystem::path(path) / std::filesystem::path(application->getPackageName() +
"." +
247 application->getName() +
".xml");
249 if (reload || !std::filesystem::exists(xmlFilePath))
251 std::filesystem::create_directories(path);
253 std::string strCommand = application->getExecutableAbsPath()
254 .append(
" -p -f xml -o ")
255 .append(xmlFilePath.string());
256 int ret = system(strCommand.c_str());
259 ARMARX_WARNING <<
"Failed to generate properties xml for " << application->getName()
260 <<
"\nCommand was: " << strCommand;
265 application->updateFound();
266 if (!application->getFound())
271 auto xmlDate = (std::filesystem::last_write_time(xmlFilePath));
274 std::filesystem::last_write_time(application->getPathToExecutable().append(
"/").append(
275 application->getExecutableName()));
277 if (execDate > xmlDate)
287 application->setProperties(parser.loadFromXml(xmlFilePath.string()));
294 defaultStopStrategy = strategy;
300 defaultStartStrategy = appStarter;
306 this->stopStrategy[scenario] = strategy;
312 this->starter[scenario] = appStarter;
318 if (starter.count(scenario) == 0)
320 return defaultStartStrategy;
322 return starter[scenario];
328 if (stopStrategy.count(scenario) == 0)
330 return defaultStopStrategy;
332 return stopStrategy[scenario];
338 return defaultStopStrategy;
std::shared_ptr< ScenarioManager::Data_Structure::ApplicationInstance > ApplicationInstancePtr
virtual void startApplication(Data_Structure::ApplicationInstancePtr application, StatusManager statusManager, const std::string &commandLineParameters="", bool printOnly=false)=0
Starts an application.
virtual void startScenario(Data_Structure::ScenarioPtr scenario, StatusManager statusManager, const std::string &commandLineParameters="", bool printOnly=false)=0
StopStrategyPtr getStopStrategy(Data_Structure::ScenarioPtr scenario)
std::future< void > startScenario(Data_Structure::ScenarioPtr scenario, bool printOnly=false, const std::string &commandLineParameters="")
Starts a scenario.
void setDefaultStarter(ApplicationStarterPtr appStarter)
Sets the appStarter this Executor uses to start applications and request statuses.
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.
std::future< void > restartApplication(Data_Structure::ApplicationInstancePtr application, bool printOnly=false)
Restarts an application.
std::future< void > stopScenario(Data_Structure::ScenarioPtr scenario)
Stops a scenario.
std::string getApplicationStatus(Data_Structure::ApplicationInstancePtr application)
Returns the status of an application.
std::future< void > restartScenario(Data_Structure::ScenarioPtr scenario, bool printOnly=false)
Restarts a scenario.
std::future< void > startApplication(Data_Structure::ApplicationInstancePtr application, bool printOnly=false, const std::string &commandLineParameters="")
Starts an application.
Executor(StopStrategyPtr strategy, ApplicationStarterPtr starter)
Constructor that sets StopStrategy and ApplicationStarter.
std::future< void > stopApplication(Data_Structure::ApplicationInstancePtr application)
Stops an application.
void setDefaultStopStrategy(StopStrategyPtr strategy)
Sets the strategy this Executor uses to stop applications.
StopStrategyPtr getDefaultStopStrategy()
void setStarter(ApplicationStarterPtr appStarter, Data_Structure::ScenarioPtr scenario)
ApplicationStarterPtr getStarter(Data_Structure::ScenarioPtr scenario)
void setStopStrategy(StopStrategyPtr strategy, Data_Structure::ScenarioPtr scenario)
virtual void stop(Data_Structure::ApplicationInstancePtr application)=0
Stops an application.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
std::shared_ptr< Scenario > ScenarioPtr
std::shared_ptr< Application > ApplicationPtr
std::shared_ptr< StopStrategy > StopStrategyPtr
std::shared_ptr< ApplicationStarter > ApplicationStarterPtr
static const std::string Stopped
static const std::string Inactive