From 8b851851def1f1a8cddd824f2694584cf37e977a Mon Sep 17 00:00:00 2001
From: Alexander Stephan <alexander.stephan@sap.com>
Date: Tue, 30 Dec 2025 11:04:10 +0000
Subject: [PATCH 4/4] REORG/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.
---
 src/mworker.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/src/mworker.c b/src/mworker.c
index 83c438c35..cdf9a9332 100644
--- a/src/mworker.c
+++ b/src/mworker.c
@@ -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) {
-- 
2.43.7

