27
27
#include "service_keeper.h"
28
28
#include "service_monitor.h"
29
29
#include "service_postgres_ctl.h"
30
+ #include "service_run_hooks.h"
30
31
#include "signals.h"
31
32
#include "supervisor.h"
32
33
@@ -39,14 +40,17 @@ static void cli_do_service_getpid(const char *serviceName);
39
40
static void cli_do_service_getpid_postgres (int argc , char * * argv );
40
41
static void cli_do_service_getpid_listener (int argc , char * * argv );
41
42
static void cli_do_service_getpid_node_active (int argc , char * * argv );
43
+ static void cli_do_service_getpid_run_hooks (int argc , char * * argv );
42
44
43
45
static void cli_do_service_restart (const char * serviceName );
44
46
static void cli_do_service_restart_postgres (int argc , char * * argv );
45
47
static void cli_do_service_restart_listener (int argc , char * * argv );
46
48
static void cli_do_service_restart_node_active (int argc , char * * argv );
49
+ static void cli_do_service_restart_run_hooks (int argc , char * * argv );
47
50
48
51
static void cli_do_service_monitor_listener (int argc , char * * argv );
49
52
static void cli_do_service_node_active (int argc , char * * argv );
53
+ static void cli_do_service_run_hooks (int argc , char * * argv );
50
54
51
55
CommandLine service_pgcontroller =
52
56
make_command ("pgcontroller" ,
@@ -80,6 +84,14 @@ CommandLine service_node_active =
80
84
cli_getopt_pgdata ,
81
85
cli_do_service_node_active );
82
86
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
+
83
95
CommandLine service_getpid_postgres =
84
96
make_command ("postgres" ,
85
97
"Get the pid of the pg_autoctl postgres controller service" ,
@@ -104,10 +116,19 @@ CommandLine service_getpid_node_active =
104
116
cli_getopt_pgdata ,
105
117
cli_do_service_getpid_node_active );
106
118
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
+
107
127
static CommandLine * service_getpid [] = {
108
128
& service_getpid_postgres ,
109
129
& service_getpid_listener ,
110
130
& service_getpid_node_active ,
131
+ & service_getpid_run_hooks ,
111
132
NULL
112
133
};
113
134
@@ -141,10 +162,19 @@ CommandLine service_restart_node_active =
141
162
cli_getopt_pgdata ,
142
163
cli_do_service_restart_node_active );
143
164
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
+
144
173
static CommandLine * service_restart [] = {
145
174
& service_restart_postgres ,
146
175
& service_restart_listener ,
147
176
& service_restart_node_active ,
177
+ & service_restart_run_hooks ,
148
178
NULL
149
179
};
150
180
@@ -160,6 +190,7 @@ static CommandLine *service[] = {
160
190
& service_postgres ,
161
191
& service_monitor_listener ,
162
192
& service_node_active ,
193
+ & service_run_hooks ,
163
194
NULL
164
195
};
165
196
@@ -255,6 +286,16 @@ cli_do_service_getpid_node_active(int argc, char **argv)
255
286
}
256
287
257
288
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
+
258
299
/*
259
300
* cli_do_service_restart sends the TERM signal to the given serviceName, which
260
301
* 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)
352
393
}
353
394
354
395
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
+
355
408
/*
356
409
* cli_do_pgcontroller starts the process controller service within a supervision
357
410
* 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)
588
641
/* Start the node_active() protocol client */
589
642
(void ) keeper_node_active_loop (& keeper , ppid );
590
643
}
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