35 const std::string & exec_error_format ,
const G::Identity &
id ) ;
49 const std::string & exec_error_format ,
const G::Identity &
id ) ;
60 void operator=(
const TaskImp & ) ;
61 virtual void onFutureEvent()
override ;
62 static void wait(
TaskImp * , FutureEvent::handle_type ) ;
68 G::threading::thread_type m_thread ;
74 const std::string & exec_error_format ,
const G::Identity &
id ) :
76 m_future_event(*this,eh) ,
77 m_process(commandline.exe(),commandline.args(),1,true,true,id,true,127,exec_error_format)
79 if( ! G::threading::works() )
81 if( G::threading::using_std_thread )
82 G_WARNING_ONCE(
"GNet::TaskImp::TaskImp: multi-threading disabled: running tasks synchronously" ) ;
83 wait(
this , m_future_event.
handle() ) ;
87 m_thread = G::threading::thread_type( TaskImp::wait ,
this , m_future_event.
handle() ) ;
91 GNet::TaskImp::~TaskImp()
93 if( m_thread.joinable() )
96 G_WARNING(
"TaskImp::dtor: blocked waiting for process " << m_process.id() <<
" to terminate" ) ;
98 G_LOG(
"TaskImp::dtor: process " << m_process.id() <<
" terminated" ) ;
102 void GNet::TaskImp::wait( TaskImp * This , FutureEvent::handle_type handle )
107 This->m_process.wait().run() ;
116 void GNet::TaskImp::onFutureEvent()
120 G_DEBUG(
"GNet::TaskImp::onFutureEvent: future event" ) ;
121 if( G::threading::works() )
124 G_DEBUG(
"GNet::TaskImp::onFutureEvent: thread joined" ) ;
127 int exit_code = m_process.wait().get() ;
128 G_DEBUG(
"GNet::TaskImp::onFutureEvent: exit code " << exit_code ) ;
130 std::string output = m_process.wait().output() ;
131 G_DEBUG(
"GNet::TaskImp::onFutureEvent: output: [" <<
G::Str::printable(output) <<
"]" ) ;
133 m_task.done( exit_code , output ) ;
135 catch( std::exception & e )
137 throw Task::Failed( e.what() ) ;
141 void GNet::TaskImp::kill()
149 const std::string & exec_error_format ,
const G::Identity &
id ) :
150 m_callback(callback) ,
152 m_exec_error_format(exec_error_format) ,
168 m_imp.reset(
new TaskImp( *
this , m_eh , commandline , m_exec_error_format , m_id ) ) ;
178 void GNet::Task::done(
int exit_code , std::string output )
181 m_callback.onTaskDone( exit_code , output ) ;
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
A callback interface for GNet::FutureEvent.
An object that hooks into the event loop and calls back to the client code.
Task(TaskCallback &, ExceptionHandler &eh, const std::string &exec_error_format=std::string(), const G::Identity &=G::Identity::invalid())
Constructor for a start()able object.
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
void start(const G::ExecutableCommand &commandline)
Starts the task by spawning a new process with the given command-line and also starting a thread to w...
A private implementation class used by GNet::Task.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
void stop()
Attempts to kill the spawned process and waits for it to terminate.
A class for running an exectuable in a separate process with an asychronous completion callback...
A structure representing an external program, holding a path and a set of arguments.
static bool send(handle_type handle) g__noexcept
Pokes an event into the main event loop so that the FutureEventHandler callback is called asynchronou...
handle_type handle()
Returns a handle that can be passed between threads and used in send().
An abstract interface for callbacks from GNet::Task.
A class for creating new processes.
virtual ~TaskCallback()
Destructor.