mirror of
https://github.com/matrix-construct/construct
synced 2024-12-25 23:14:13 +01:00
ircd:Ⓜ️:exec: Add options structure; add logging/detach related options.
This commit is contained in:
parent
e5b0fe25c7
commit
86a8509b26
2 changed files with 41 additions and 4 deletions
|
@ -34,6 +34,7 @@ namespace boost::process
|
|||
struct ircd::exec
|
||||
:instance_list<ircd::exec>
|
||||
{
|
||||
struct opts;
|
||||
using args = vector_view<const string_view>;
|
||||
using const_buffers = vector_view<const const_buffer>;
|
||||
using mutable_buffers = vector_view<const mutable_buffer>;
|
||||
|
@ -42,6 +43,7 @@ struct ircd::exec
|
|||
static uint64_t id_ctr;
|
||||
|
||||
uint64_t id {0};
|
||||
std::unique_ptr<opts> opt;
|
||||
std::string path;
|
||||
std::vector<std::string> argv;
|
||||
std::unique_ptr<pair<boost::process::async_pipe>> pipe;
|
||||
|
@ -55,6 +57,7 @@ struct ircd::exec
|
|||
bool signal(const int &sig);
|
||||
long join(const int &sig = 0);
|
||||
|
||||
exec(const args &, const opts &);
|
||||
exec(const args &);
|
||||
exec(exec &&) = delete;
|
||||
exec(const exec &) = delete;
|
||||
|
@ -63,6 +66,29 @@ struct ircd::exec
|
|||
~exec() noexcept;
|
||||
};
|
||||
|
||||
/// Exec options
|
||||
///
|
||||
struct ircd::exec::opts
|
||||
{
|
||||
/// When false (default) the child is terminated by the dtor of
|
||||
/// ircd::exec. Note setting this to true does not detach the boost
|
||||
/// handles until destruction time; it also does not affect calling
|
||||
/// the interface join() manually.
|
||||
bool detach {false};
|
||||
|
||||
/// Child executions will be logged at this level (use DEBUG to quiet)
|
||||
ircd::log::level exec_log_level = ircd::log::level::NOTICE;
|
||||
|
||||
/// Child exits will be logged at this level (use DEBUG to quiet); note
|
||||
/// non-zero exits are still logged with level::ERROR.
|
||||
ircd::log::level exit_log_level = ircd::log::level::INFO;
|
||||
};
|
||||
|
||||
inline
|
||||
ircd::exec::exec(const args &args)
|
||||
:exec{args, opts{}}
|
||||
{}
|
||||
|
||||
inline ircd::const_buffer
|
||||
ircd::write(exec &p,
|
||||
const const_buffer &buf)
|
||||
|
|
19
ircd/exec.cc
19
ircd/exec.cc
|
@ -30,11 +30,16 @@ ircd::util::instance_list<ircd::exec>::list
|
|||
allocator
|
||||
};
|
||||
|
||||
ircd::exec::exec(const args &args)
|
||||
ircd::exec::exec(const args &args,
|
||||
const opts &opt)
|
||||
:id
|
||||
{
|
||||
++id_ctr
|
||||
}
|
||||
,opt
|
||||
{
|
||||
std::make_unique<opts>(opt)
|
||||
}
|
||||
,path
|
||||
(
|
||||
args.at(0)
|
||||
|
@ -66,9 +71,10 @@ ircd::exec::exec(const args &args)
|
|||
child->id()
|
||||
}
|
||||
{
|
||||
log::notice
|
||||
log::logf
|
||||
{
|
||||
log, "id:%lu pid:%ld `%s' exec argc:%zu",
|
||||
log, this->opt->exec_log_level,
|
||||
"id:%lu pid:%ld `%s' exec argc:%zu",
|
||||
id,
|
||||
pid,
|
||||
path,
|
||||
|
@ -79,6 +85,10 @@ ircd::exec::exec(const args &args)
|
|||
ircd::exec::~exec()
|
||||
noexcept try
|
||||
{
|
||||
assert(opt);
|
||||
if(opt->detach)
|
||||
return;
|
||||
|
||||
join(SIGKILL);
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
|
@ -126,10 +136,11 @@ try
|
|||
assert(!child->running());
|
||||
code = child->exit_code();
|
||||
|
||||
assert(opt);
|
||||
const auto &level
|
||||
{
|
||||
code == 0?
|
||||
log::level::INFO:
|
||||
opt->exit_log_level:
|
||||
log::level::ERROR
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue