ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
ScriptedInitialisationHandler.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
4
5#include "DeviceModule.h"
6
7#include <boost/process.hpp>
8
9#include <functional>
10#include <utility>
11namespace bp = boost::process;
12
13namespace ChimeraTK {
14
15 /********************************************************************************************************************/
16
17 ScriptedInitHandler::ScriptedInitHandler(ModuleGroup* owner, const std::string& name, const std::string& description,
18 std::string command, DeviceModule& deviceModule, std::string outputName, unsigned int errorGracePeriod)
19 : ApplicationModule(owner, name, description), _command(std::move(command)),
20 _deviceAlias(deviceModule.getDeviceAliasOrURI()), _outputName(std::move(outputName)),
21 _errorGracePeriod(errorGracePeriod) {
22 deviceModule.addInitialisationHandler([this](Device&) { doInit(); });
23 }
24 /********************************************************************************************************************/
25
27 std::string output;
28 _scriptOutput = "";
30
31 try {
32 bp::ipstream out;
33 bp::child initScript(_command, (bp::std_out & bp::std_err) > out);
34 std::string line;
35 // Publish every line that is read from the script. It is appended to the output string
36 // such that a growing message is published.
37 // For debugging it is important to get the intermediate information. In case the script gets stuck
38 // you want to know what has already been printed.
39 while(std::getline(out, line)) {
40 output += line + "\n";
41 _scriptOutput = output;
43 }
44 initScript.wait();
45
46 if(initScript.exit_code() != 0) {
47 output += "!!! " + _deviceAlias + " initialisation FAILED!";
48 _scriptOutput = output;
50 if(!_lastFailed) {
51 ChimeraTK::logger(Logger::Severity::error, "Device " + _deviceAlias) << output << std::endl;
52 }
53 _lastFailed = true;
54 std::this_thread::sleep_for(std::chrono::seconds(_errorGracePeriod));
55 throw ChimeraTK::runtime_error(_deviceAlias + " initialisation failed.");
56 }
57 output += _deviceAlias + " initialisation SUCCESS!";
58 _scriptOutput = output;
60 ChimeraTK::logger(Logger::Severity::info, "Device " + _deviceAlias) << output << std::endl;
61 _lastFailed = false;
62 }
63 catch(bp::process_error& e) {
64 // this
65 throw ChimeraTK::logic_error("Caught boost::process::process_error while executing \"" + _command +
66 "\" for device " + _deviceAlias + ": " + e.what());
67 }
68 }
69
70 /********************************************************************************************************************/
71} // namespace ChimeraTK
void addInitialisationHandler(std::function< void(ChimeraTK::Device &)> initialisationHandler)
bool write(ChimeraTK::VersionNumber versionNumber)=delete
InvalidityTracer application module.
Logger::StreamProxy logger(Logger::Severity severity, std::string context)
Convenience function to obtain the logger stream.
Definition Logger.h:124
ScriptedInitHandler(ModuleGroup *owner, const std::string &name, const std::string &description, std::string command, DeviceModule &deviceModule, std::string outputName="initScriptOutput", unsigned int errorGracePeriod=10)
Constructor.