StatusManager.cpp
Go to the documentation of this file.
1#include "StatusManager.h"
2
3#include <filesystem>
4#include <fstream>
5
9
10using namespace ScenarioManager;
11using namespace Exec;
12using namespace armarx;
13
17
18int
20{
21 std::string cachePath = ArmarXDataPath::GetCachePath();
22 if (cachePath.empty())
23 {
24 return -1;
25 }
26
27 cachePath.append("/pids/");
28 auto dir = remove_trailing_separator(cachePath);
29
30 if (!std::filesystem::exists(dir) && !std::filesystem::create_directories(dir))
31 {
32 ARMARX_WARNING_S << "Unable to create Cache directory for the ScenarioManager plugin at "
33 << cachePath;
34 }
35
36
37 std::string filename = cachePath + app->getPackageName() + "." + app->getScenario()->getName() +
38 "." + app->getName() + (app->getInstanceName().empty() ? "" : ".") +
39 app->getInstanceName() + ".pids";
40
41 if (!std::filesystem::exists(filename))
42 {
43 return -1;
44 }
45
46 std::ifstream input(filename);
47
48 int pid;
49 input >> pid;
50
51 return pid;
52}
53
54void
56{
57 std::string cachePath = ArmarXDataPath::GetCachePath();
58 if (cachePath.empty())
59 {
60 // Without a cache path loadPid() can never find anything again, so silently "saving" to a
61 // path relative to the filesystem root would just lose every pid across restarts.
62 ARMARX_WARNING_S << "No cache path configured - the pid of " << app->getName()
63 << " cannot be persisted, and the application will be reported as "
64 "stopped once its pid is lost.";
65 return;
66 }
67
68 cachePath.append("/pids/");
69 auto dir = remove_trailing_separator(cachePath);
70
71 if (!std::filesystem::exists(dir) && !std::filesystem::create_directories(dir))
72 {
73 ARMARX_WARNING_S << "Unable to create Cache directory for the ScenarioManager plugin at "
74 << cachePath;
75 }
76
77 std::string filename = cachePath + app->getPackageName() + "." + app->getScenario()->getName() +
78 "." + app->getName() + (app->getInstanceName().empty() ? "" : ".") +
79 app->getInstanceName() + ".pids";
80
81 if (app->getPid() == -1)
82 {
83 std::filesystem::remove(std::filesystem::path(filename));
84 return;
85 }
86
87 std::ofstream output(filename);
88
89 output << app->getPid();
90 output.close();
91
92 if (!output)
93 {
94 ARMARX_WARNING_S << "Failed to write the pid of " << app->getName() << " to " << filename
95 << ". The application will be reported as stopped once its pid is lost.";
96 }
97}
98
99void
101{
102 std::string cachePath = ArmarXDataPath::GetCachePath();
103 cachePath.append("/pids/");
104
105 std::filesystem::remove_all(std::filesystem::path(cachePath));
106}
void savePid(Data_Structure::ApplicationInstancePtr app)
int loadPid(Data_Structure::ApplicationInstancePtr app)
static std::string GetCachePath()
The base Cache directory of ArmarX.
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:211
std::shared_ptr< ApplicationInstance > ApplicationInstancePtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
fs::path remove_trailing_separator(fs::path p)
Definition filesystem.h:34