run-command: create start_bg_command

Create a variation of `run_command()` and `start_command()` to launch a command
into the background and optionally wait for it to become "ready" before returning.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff Hostetler
2021-09-20 15:36:17 +00:00
committed by Junio C Hamano
parent 8750249053
commit fdb1322651
2 changed files with 186 additions and 0 deletions

View File

@ -496,4 +496,61 @@ int run_processes_parallel_tr2(int n, get_next_task_fn, start_failure_fn,
*/
void prepare_other_repo_env(struct strvec *env_array, const char *new_git_dir);
/**
* Possible return values for start_bg_command().
*/
enum start_bg_result {
/* child process is "ready" */
SBGR_READY = 0,
/* child process could not be started */
SBGR_ERROR,
/* callback error when testing for "ready" */
SBGR_CB_ERROR,
/* timeout expired waiting for child to become "ready" */
SBGR_TIMEOUT,
/* child process exited or was signalled before becomming "ready" */
SBGR_DIED,
};
/**
* Callback used by start_bg_command() to ask whether the
* child process is ready or needs more time to become "ready".
*
* The callback will receive the cmd and cb_data arguments given to
* start_bg_command().
*
* Returns 1 is child needs more time (subject to the requested timeout).
* Returns 0 if child is "ready".
* Returns -1 on any error and cause start_bg_command() to also error out.
*/
typedef int(start_bg_wait_cb)(const struct child_process *cmd, void *cb_data);
/**
* Start a command in the background. Wait long enough for the child
* to become "ready" (as defined by the provided callback). Capture
* immediate errors (like failure to start) and any immediate exit
* status (such as a shutdown/signal before the child became "ready")
* and return this like start_command().
*
* We run a custom wait loop using the provided callback to wait for
* the child to start and become "ready". This is limited by the given
* timeout value.
*
* If the child does successfully start and become "ready", we orphan
* it into the background.
*
* The caller must not call finish_command().
*
* The opaque cb_data argument will be forwarded to the callback for
* any instance data that it might require. This may be NULL.
*/
enum start_bg_result start_bg_command(struct child_process *cmd,
start_bg_wait_cb *wait_cb,
void *cb_data,
unsigned int timeout_sec);
#endif