2727#include "service_keeper.h"
2828#include "service_monitor.h"
2929#include "service_postgres_ctl.h"
30+ #include "service_run_hooks.h"
3031#include "signals.h"
3132#include "supervisor.h"
3233
@@ -39,14 +40,17 @@ static void cli_do_service_getpid(const char *serviceName);
3940static void cli_do_service_getpid_postgres (int argc , char * * argv );
4041static void cli_do_service_getpid_listener (int argc , char * * argv );
4142static void cli_do_service_getpid_node_active (int argc , char * * argv );
43+ static void cli_do_service_getpid_run_hooks (int argc , char * * argv );
4244
4345static void cli_do_service_restart (const char * serviceName );
4446static void cli_do_service_restart_postgres (int argc , char * * argv );
4547static void cli_do_service_restart_listener (int argc , char * * argv );
4648static void cli_do_service_restart_node_active (int argc , char * * argv );
49+ static void cli_do_service_restart_run_hooks (int argc , char * * argv );
4750
4851static void cli_do_service_monitor_listener (int argc , char * * argv );
4952static void cli_do_service_node_active (int argc , char * * argv );
53+ static void cli_do_service_run_hooks (int argc , char * * argv );
5054
5155CommandLine service_pgcontroller =
5256 make_command ("pgcontroller" ,
@@ -80,6 +84,14 @@ CommandLine service_node_active =
8084 cli_getopt_pgdata ,
8185 cli_do_service_node_active );
8286
87+ CommandLine service_run_hooks =
88+ make_command ("run-hooks" ,
89+ "pg_autoctl service that run hooks (scripts)" ,
90+ CLI_PGDATA_USAGE ,
91+ CLI_PGDATA_OPTION ,
92+ cli_getopt_pgdata ,
93+ cli_do_service_run_hooks );
94+
8395CommandLine service_getpid_postgres =
8496 make_command ("postgres" ,
8597 "Get the pid of the pg_autoctl postgres controller service" ,
@@ -104,10 +116,19 @@ CommandLine service_getpid_node_active =
104116 cli_getopt_pgdata ,
105117 cli_do_service_getpid_node_active );
106118
119+ CommandLine service_getpid_run_hooks =
120+ make_command ("run-hooks" ,
121+ "Get the pid of the pg_autoctl run-hooks service" ,
122+ CLI_PGDATA_USAGE ,
123+ CLI_PGDATA_OPTION ,
124+ cli_getopt_pgdata ,
125+ cli_do_service_getpid_run_hooks );
126+
107127static CommandLine * service_getpid [] = {
108128 & service_getpid_postgres ,
109129 & service_getpid_listener ,
110130 & service_getpid_node_active ,
131+ & service_getpid_run_hooks ,
111132 NULL
112133};
113134
@@ -141,10 +162,19 @@ CommandLine service_restart_node_active =
141162 cli_getopt_pgdata ,
142163 cli_do_service_restart_node_active );
143164
165+ CommandLine service_restart_run_hooks =
166+ make_command ("run-hooks" ,
167+ "Restart the pg_autoctl run-hooks service" ,
168+ CLI_PGDATA_USAGE ,
169+ CLI_PGDATA_OPTION ,
170+ cli_getopt_pgdata ,
171+ cli_do_service_restart_run_hooks );
172+
144173static CommandLine * service_restart [] = {
145174 & service_restart_postgres ,
146175 & service_restart_listener ,
147176 & service_restart_node_active ,
177+ & service_restart_run_hooks ,
148178 NULL
149179};
150180
@@ -160,6 +190,7 @@ static CommandLine *service[] = {
160190 & service_postgres ,
161191 & service_monitor_listener ,
162192 & service_node_active ,
193+ & service_run_hooks ,
163194 NULL
164195};
165196
@@ -255,6 +286,16 @@ cli_do_service_getpid_node_active(int argc, char **argv)
255286}
256287
257288
289+ /*
290+ * cli_do_service_getpid_node_active gets the postgres service pid.
291+ */
292+ static void
293+ cli_do_service_getpid_run_hooks (int argc , char * * argv )
294+ {
295+ (void ) cli_do_service_getpid (SERVICE_NAME_RUN_HOOKS );
296+ }
297+
298+
258299/*
259300 * cli_do_service_restart sends the TERM signal to the given serviceName, which
260301 * is known to have the restart policy RP_PERMANENT (that's hard-coded). As a
@@ -352,6 +393,18 @@ cli_do_service_restart_node_active(int argc, char **argv)
352393}
353394
354395
396+ /*
397+ * cli_do_service_restart_run_hooks sends the TERM signal to the run-hooks,
398+ * which is known to have the restart policy RP_PERMANENT (that's hard-coded).
399+ * As a consequence the supervisor will restart the service.
400+ */
401+ static void
402+ cli_do_service_restart_run_hooks (int argc , char * * argv )
403+ {
404+ (void ) cli_do_service_restart (SERVICE_NAME_RUN_HOOKS );
405+ }
406+
407+
355408/*
356409 * cli_do_pgcontroller starts the process controller service within a supervision
357410 * tree. It is used for debug purposes only. When using this entry point we
@@ -588,3 +641,44 @@ cli_do_service_node_active(int argc, char **argv)
588641 /* Start the node_active() protocol client */
589642 (void ) keeper_node_active_loop (& keeper , ppid );
590643}
644+
645+
646+ /*
647+ * cli_do_service_run_hooks starts the run-hooks service.
648+ */
649+ static void
650+ cli_do_service_run_hooks (int argc , char * * argv )
651+ {
652+ Keeper keeper = { 0 };
653+
654+ pid_t ppid = getppid ();
655+
656+ bool exitOnQuit = true;
657+
658+ keeper .config = keeperOptions ;
659+
660+ /* Establish a handler for signals. */
661+ (void ) set_signal_handlers (exitOnQuit );
662+
663+ /* display a user-friendly process name */
664+ (void ) set_ps_title ("pg_autoctl: run-hooks" );
665+
666+ /* Prepare our Keeper and KeeperConfig from the CLI options */
667+ if (!service_run_hooks_init (& keeper ))
668+ {
669+ log_fatal ("Failed to initialize the run-hooks service, "
670+ "see above for details" );
671+ exit (EXIT_CODE_INTERNAL_ERROR );
672+ }
673+
674+ /* create the service pidfile */
675+ if (!create_service_pidfile (keeper .config .pathnames .pid ,
676+ SERVICE_NAME_RUN_HOOKS ))
677+ {
678+ /* errors have already been logged */
679+ exit (EXIT_CODE_INTERNAL_ERROR );
680+ }
681+
682+ /* Start the node_active() protocol client */
683+ (void ) service_run_hooks_loop (& keeper , ppid );
684+ }
0 commit comments