57 const std::map< const string, const string > plugin_script_config_defaults =
62 inline static const char *plugin_prefix =
"ADC_SCRIPT_PLUGIN_";
64 const std::vector<string> tags;
72 int config(
const string& dir,
string_view sprog,
const string &sdebug) {
73 if (mode != pi_config)
76 fdir = dir +
string(
"/") + std::to_string(ui);
79 std::stringstream ss(sdebug);
85 std::cout<<
"script plugin configured" <<std::endl;
91 const string get(
const std::map< string, string >& m,
94 auto it = m.find(field);
98 string en =
string(env_prefix) += field;
99 char *ec = getenv(en.c_str());
101 return plugin_script_config_defaults.at(field);
107 string get_temp_file_name() {
108 string tplt = fdir +
"/json-sh-msg-XXXXXX";
109 char * cstr =
new char [tplt.length()+1];
110 std::strcpy (cstr, tplt.c_str());
111 int fd = mkstemp(cstr);
119 std::cout<<
"'script' plugin: mkstemp failed" <<std::endl;
126 int script_send(
string& f)
128 string qcmd =
"(" + prog +
" " + f +
129 "> /dev/null 2>&1 || /bin/rm " + f +
") &";
130 int err2 = std::system(qcmd.c_str());
132 std::cout <<
"script plugin file: " << f << std::endl;
133 std::cout <<
"script plugin err: " << err2 << std::endl;
139 script_plugin() : vers(
"1.0.0") , tags({
"none"}), debug(0), state(ok), paused(
false), mode(pi_config) { }
146 if (mode != pi_pub_or_final)
151 string fname = get_temp_file_name();
153 std::cout <<
"script plugin temp name: " << fname <<std::endl;
156 std::ofstream out(fname);
158 out << b->serialize() << std::endl;
161 std::cout <<
"plugin 'script' wrote" << std::endl;
167 std::cout <<
"script plugin failed write" << std::endl;
171 std::filesystem::remove(fname);
172 std::cout <<
"script plugin failed open " <<fname<< std::endl;
176 int config(
const std::map< std::string, std::string >& m) {
177 return config(m, plugin_prefix );
180 int config(
const std::map< std::string, std::string >& m, std::string_view env_prefix) {
181 string d = get(m,
"DIRECTORY", env_prefix);
182 string prog = get(m,
"PROG", env_prefix);
183 string sdebug = get(m,
"DEBUG", env_prefix);
184 return config(d, prog, sdebug);
188 return plugin_script_config_defaults;
192 std::map <string, string >m;
196 if (mode != pi_init) {
199 if ( state == err ) {
200 std::cout <<
"script plugin initialize found pre-existing error" << std::endl;
204 std::filesystem::create_directories(fdir, ec);
205 if (ec.value() != 0 && ec.value() != EEXIST ) {
207 std::cout <<
"unable to create scratch directory for plugin 'script'; "
208 << fdir <<
" : " << ec.message() << std::endl;
212 std::cout <<
"created " << fdir <<std::endl;
214 mode = pi_pub_or_final;
220 if (mode == pi_pub_or_final) {
226 std::cout <<
"script plugin finalize on non-running plugin" << std::endl;
249 std::cout <<
"Destructing script_plugin" << std::endl;
Publisher plugin interface.
User script publisher plugin. The program specified by environment variable is used to asynchronously...
string_view version() const
int config(const std::map< std::string, std::string > &m)
Configure the plugin with the options given.
int publish(std::shared_ptr< builder_api > b)
Publish the content of the builder.
const std::map< const std::string, const std::string > & get_option_defaults()
Look up the settable options and their defaults.
static const char * adc_script_plugin_debug_default
ADC script plugin enable debug messages; (default "0": none) "2" provides message send debugging "1" ...
void pause()
Pause publishing until a call to resume. Duplicate calls are allowed.
static const char * adc_script_plugin_prog_default
The default path to a user script or program. This program should be user accessible....
static const char * adc_script_plugin_dir_default
The default scratch directory for user script messages. This directory must be globally writable and ...
void finalize()
Stop publishing and release any resources held for managing publication.
int config(const std::map< std::string, std::string > &m, std::string_view env_prefix)
Configure the plugin with the options given and the corresponding environment variables.
int initialize()
Ready the plugin to publish following the configuration options set or defaulted.
void resume()
Resume publishing Duplicate calls are allowed.
std::string_view string_view