diff --git a/repmgrd-bdr.c b/repmgrd-bdr.c index 038ecb12..85c125d8 100644 --- a/repmgrd-bdr.c +++ b/repmgrd-bdr.c @@ -35,6 +35,29 @@ do_bdr_node_check(void) /* nothing to do at the moment */ } +void +handle_sigint_bdr(SIGNAL_ARGS) +{ + PQExpBufferData event_details; + + initPQExpBuffer(&event_details); + + appendPQExpBuffer(&event_details, + "%s signal received", + postgres_signal_arg == SIGTERM + ? "TERM" : "INT"); + + create_event_notification(local_conn, + &config_file_options, + config_file_options.node_id, + "repmgrd_shutdown", + true, + event_details.data); + termPQExpBuffer(&event_details); + + terminate(SUCCESS); +} + void monitor_bdr(void) diff --git a/repmgrd-bdr.h b/repmgrd-bdr.h index 64aaedc0..3cc9b64a 100644 --- a/repmgrd-bdr.h +++ b/repmgrd-bdr.h @@ -22,4 +22,5 @@ extern void do_bdr_node_check(void); extern void monitor_bdr(void); +extern void handle_sigint_bdr(SIGNAL_ARGS); #endif /* _REPMGRD_BDR_H_ */ diff --git a/repmgrd-physical.c b/repmgrd-physical.c index 2282e3a9..841d97a8 100644 --- a/repmgrd-physical.c +++ b/repmgrd-physical.c @@ -85,6 +85,36 @@ static void update_monitoring_history(void); static const char * format_failover_state(FailoverState failover_state); +void +handle_sigint_physical(SIGNAL_ARGS) +{ + PGconn *writeable_conn; + PQExpBufferData event_details; + + initPQExpBuffer(&event_details); + + appendPQExpBuffer(&event_details, + "%s signal received", + postgres_signal_arg == SIGTERM + ? "TERM" : "INT"); + + if (local_node_info.type == PRIMARY) + writeable_conn = local_conn; + else + writeable_conn = primary_conn; + + create_event_notification(writeable_conn, + &config_file_options, + config_file_options.node_id, + "repmgrd_shutdown", + true, + event_details.data); + + termPQExpBuffer(&event_details); + + terminate(SUCCESS); +} + /* perform some sanity checks on the node's configuration */ void @@ -113,6 +143,14 @@ do_physical_node_check(void) log_error(_("this node is marked as inactive and cannot be used as a failover target")); log_hint(_("%s"), hint); PQfinish(local_conn); + + create_event_notification(NULL, + &config_file_options, + config_file_options.node_id, + "repmgrd_shutdown", + false, + "node is inactive and cannot be used as a failover target"); + terminate(ERR_BAD_CONFIG); case FAILOVER_MANUAL: @@ -303,7 +341,7 @@ monitor_streaming_primary(void) create_event_notification(NULL, &config_file_options, config_file_options.node_id, - "repmgrd_terminate", + "repmgrd_shutdown", true, event_details.data); @@ -414,15 +452,28 @@ monitor_streaming_primary(void) } else if (record_status == RECORD_NOT_FOUND) { - log_error(_("no metadata record found for this node on current primary %i"), primary_node_id); + PQExpBufferData event_details; + initPQExpBuffer(&event_details); + + appendPQExpBuffer(&event_details, + _("no metadata record found for this node on current primary %i"), + primary_node_id); + + log_error("%s", event_details.data); log_hint(_("check that 'repmgr (primary|standby) register' was executed for this node")); PQfinish(new_primary_conn); - /* add event notification */ + create_event_notification(NULL, + &config_file_options, + config_file_options.node_id, + "repmgrd_shutdown", + false, + event_details.data); + termPQExpBuffer(&event_details); + terminate(ERR_BAD_CONFIG); } - } } else @@ -1058,9 +1109,26 @@ monitor_streaming_witness(void) if (get_primary_node_record(local_conn, &upstream_node_info) == false) { - log_error(_("unable to retrieve record for primary node")); + PQExpBufferData event_details; + + initPQExpBuffer(&event_details); + + appendPQExpBuffer(&event_details, + _("unable to retrieve record for primary node")); + + log_error("%s", event_details.data); log_hint(_("execute \"repmgr witness register --force\" to update the witness node ")); PQfinish(local_conn); + + create_event_notification(NULL, + &config_file_options, + config_file_options.node_id, + "repmgrd_shutdown", + false, + event_details.data); + + termPQExpBuffer(&event_details); + terminate(ERR_BAD_CONFIG); } diff --git a/repmgrd-physical.h b/repmgrd-physical.h index 391170b3..31a4ef14 100644 --- a/repmgrd-physical.h +++ b/repmgrd-physical.h @@ -26,4 +26,6 @@ void monitor_streaming_standby(void); void monitor_streaming_witness(void); void close_connections_physical(void); +void handle_sigint_physical(SIGNAL_ARGS); + #endif /* _REPMGRD_PHYSICAL_H_ */ diff --git a/repmgrd.c b/repmgrd.c index dcfba444..45a03dfe 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -73,7 +73,6 @@ static void start_monitoring(void); #ifndef WIN32 static void setup_event_handlers(void); static void handle_sighup(SIGNAL_ARGS); -static void handle_sigint(SIGNAL_ARGS); #endif int calculate_elapsed(instr_time start_time); @@ -613,11 +612,6 @@ check_and_create_pid_file(const char *pid_file) #ifndef WIN32 -static void -handle_sigint(SIGNAL_ARGS) -{ - terminate(SUCCESS); -} /* SIGHUP: set flag to re-read config file at next convenient time */ static void @@ -630,8 +624,23 @@ static void setup_event_handlers(void) { pqsignal(SIGHUP, handle_sighup); - pqsignal(SIGINT, handle_sigint); - pqsignal(SIGTERM, handle_sigint); + + /* + * we want to be able to write a "repmgrd_shutdown" event, so delegate + * signal handling to the respective replication type handler, as it + * will know best which database connection to use + */ + switch (config_file_options.replication_type) + { + case REPLICATION_TYPE_BDR: + pqsignal(SIGINT, handle_sigint_bdr); + pqsignal(SIGTERM, handle_sigint_bdr); + break; + case REPLICATION_TYPE_PHYSICAL: + pqsignal(SIGINT, handle_sigint_physical); + pqsignal(SIGTERM, handle_sigint_physical); + break; + } } #endif