53 const std::map< const string, const string > plugin_script_config_defaults =
58 inline static const char *plugin_prefix =
"ADC_SCRIPT_PLUGIN_";
60 const std::vector<string> tags;
68 int config(
const string& dir,
string_view sprog,
const string &sdebug) {
69 if (mode != pi_config)
72 fdir = dir +
string(
"/") + std::to_string(ui);
75 std::stringstream ss(sdebug);
81 std::cout<<
"script plugin configured" <<std::endl;
87 const string get(
const std::map< string, string >& m,
90 auto it = m.find(field);
94 string en =
string(env_prefix) += field;
95 char *ec = getenv(en.c_str());
97 return plugin_script_config_defaults.at(field);
103 string get_temp_file_name() {
104 string tplt = fdir +
"/json-sh-msg-XXXXXX";
105 char * cstr =
new char [tplt.length()+1];
106 std::strcpy (cstr, tplt.c_str());
107 int fd = mkstemp(cstr);
115 std::cout<<
"'script' plugin: mkstemp failed" <<std::endl;
122 int script_send(
string& f)
124 string qcmd =
"(" + prog +
" " + f +
125 "> /dev/null 2>&1 || /bin/rm " + f +
") &";
126 int err2 = std::system(qcmd.c_str());
128 std::cout <<
"script plugin file: " << f << std::endl;
129 std::cout <<
"script plugin err: " << err2 << std::endl;
135 script_plugin() : vers(
"1.0.0") , tags({
"none"}), debug(0), state(ok), paused(
false), mode(pi_config) { }
142 if (mode != pi_pub_or_final)
147 string fname = get_temp_file_name();
149 std::cout <<
"script plugin temp name: " << fname <<std::endl;
152 std::ofstream out(fname);
154 out << b->serialize() << std::endl;
157 std::cout <<
"plugin 'script' wrote" << std::endl;
163 std::cout <<
"script plugin failed write" << std::endl;
167 std::filesystem::remove(fname);
168 std::cout <<
"script plugin failed open " <<fname<< std::endl;
172 int config(
const std::map< std::string, std::string >& m) {
173 return config(m, plugin_prefix );
176 int config(
const std::map< std::string, std::string >& m, std::string_view env_prefix) {
177 string d = get(m,
"DIRECTORY", env_prefix);
178 string prog = get(m,
"PROG", env_prefix);
179 string sdebug = get(m,
"DEBUG", env_prefix);
180 return config(d, prog, sdebug);
184 return plugin_script_config_defaults;
188 std::map <string, string >m;
192 if (mode != pi_init) {
195 if ( state == err ) {
196 std::cout <<
"script plugin initialize found pre-existing error" << std::endl;
200 std::filesystem::create_directories(fdir, ec);
201 if (ec.value() != 0 && ec.value() != EEXIST ) {
203 std::cout <<
"unable to create scratch directory for plugin 'script'; "
204 << fdir <<
" : " << ec.message() << std::endl;
208 std::cout <<
"created " << fdir <<std::endl;
210 mode = pi_pub_or_final;
216 if (mode == pi_pub_or_final) {
222 std::cout <<
"script plugin finalize on non-running plugin" << std::endl;
245 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