mirror of
https://github.com/haproxy/haproxy.git
synced 2026-02-03 20:39:41 -05:00
MINOR: mworker/cli: extract worker "show proc" row printer
Introduce cli_append_worker_row() to centralize formatting of a single worker row. Also, replace duplicated row-printing code in both current and old workers loops with the helper. Motivation: Reduces LOC and improves readability by removing duplication.
This commit is contained in:
parent
4c10d9c70c
commit
e241144e70
1 changed files with 19 additions and 20 deletions
|
|
@ -810,6 +810,23 @@ struct cli_showproc_ctx {
|
|||
int next_reload; /* reload number to resume from, 0 = from the beginning */
|
||||
};
|
||||
|
||||
/* Append a single worker row to trash (shared between current/old sections) */
|
||||
static void cli_append_worker_row(struct cli_showproc_ctx *ctx, struct mworker_proc *child, time_t tv_sec)
|
||||
{
|
||||
char *uptime = NULL;
|
||||
int up = tv_sec - child->timestamp;
|
||||
|
||||
if (up < 0) /* must never be negative because of clock drift */
|
||||
up = 0;
|
||||
|
||||
memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
|
||||
chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s", child->pid, "worker", child->reloads, uptime, child->version);
|
||||
if (ctx->debug)
|
||||
chunk_appendf(&trash, "\t\t %-15d %-15d", child->ipc_fd[0], child->ipc_fd[1]);
|
||||
chunk_appendf(&trash, "\n");
|
||||
ha_free(&uptime);
|
||||
}
|
||||
|
||||
/* Displays workers and processes */
|
||||
static int cli_io_handler_show_proc(struct appctx *appctx)
|
||||
{
|
||||
|
|
@ -851,10 +868,6 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
|
|||
if (ctx->next_reload != 0)
|
||||
continue;
|
||||
|
||||
up = date.tv_sec - child->timestamp;
|
||||
if (up < 0) /* must never be negative because of clock drift */
|
||||
up = 0;
|
||||
|
||||
if (!(child->options & PROC_O_TYPE_WORKER))
|
||||
continue;
|
||||
|
||||
|
|
@ -862,12 +875,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
|
|||
old++;
|
||||
continue;
|
||||
}
|
||||
memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
|
||||
chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s", child->pid, "worker", child->reloads, uptime, child->version);
|
||||
if (ctx->debug)
|
||||
chunk_appendf(&trash, "\t\t %-15d %-15d", child->ipc_fd[0], child->ipc_fd[1]);
|
||||
chunk_appendf(&trash, "\n");
|
||||
ha_free(&uptime);
|
||||
cli_append_worker_row(ctx, child, date.tv_sec);
|
||||
}
|
||||
|
||||
if (applet_putchk(appctx, &trash) == -1)
|
||||
|
|
@ -878,10 +886,6 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
|
|||
if (ctx->next_reload == 0)
|
||||
chunk_appendf(&trash, "# old workers\n");
|
||||
list_for_each_entry(child, &proc_list, list) {
|
||||
up = date.tv_sec - child->timestamp;
|
||||
if (up <= 0) /* must never be negative because of clock drift */
|
||||
up = 0;
|
||||
|
||||
/* If we're resuming, skip entries that were already printed (reload >= ctx->next_reload) */
|
||||
if (ctx->next_reload && child->reloads >= ctx->next_reload)
|
||||
continue;
|
||||
|
|
@ -890,12 +894,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
|
|||
continue;
|
||||
|
||||
if (child->options & PROC_O_LEAVING) {
|
||||
memprintf(&uptime, "%dd%02dh%02dm%02ds", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
|
||||
chunk_appendf(&trash, "%-15u %-15s %-15d %-15s %-15s", child->pid, "worker", child->reloads, uptime, child->version);
|
||||
if (ctx->debug)
|
||||
chunk_appendf(&trash, "\t\t %-15d %-15d", child->ipc_fd[0], child->ipc_fd[1]);
|
||||
chunk_appendf(&trash, "\n");
|
||||
ha_free(&uptime);
|
||||
cli_append_worker_row(ctx, child, date.tv_sec);
|
||||
|
||||
/* Try to flush so we can resume after this reload on next page if the buffer is full. */
|
||||
if (applet_putchk(appctx, &trash) == -1) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue