Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/start/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
for range t.C {

if err := node.PostInit(); err != nil {
fmt.Printf("failed post-init: %s", err)
fmt.Printf("failed post-init: %s. Retrying...", err)
continue
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/flypg/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Node struct {
AppName string
PrivateIP string
DataDir string
Region string
PGPort int

SUCredentials Credentials
Expand All @@ -48,6 +49,7 @@ func NewNode() (*Node, error) {
DataDir: "/data/postgresql",
ManagerDatabaseName: "repmgr",
ManagerConfigPath: "/data/repmgr.conf",
Region: os.Getenv("FLY_REGION"),
}

if appName := os.Getenv("FLY_APP_NAME"); appName != "" {
Expand Down Expand Up @@ -149,6 +151,13 @@ func (n *Node) Init() error {
return nil
}

func (n *Node) ValidPrimary() bool {
if n.Region == os.Getenv("PRIMARY_REGION") {
return true
}
return false
}

// PostInit are operations that should be executed against a running Postgres on boot.
func (n *Node) PostInit() error {
client, err := state.NewConsulClient()
Expand All @@ -163,6 +172,11 @@ func (n *Node) PostInit() error {

switch primaryIP {
case "":
// Check if we can be a primary
if !n.ValidPrimary() {
return fmt.Errorf("no primary to follow and can't configure self as primary because primary region is '%s' and we are in '%s'", n.Region, os.Getenv("PRIMARY_REGION"))
}

// Initialize ourselves as the primary.
conn, err := n.NewLocalConnection(context.TODO())
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/flypg/repmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func writeManagerConf(node Node) error {
"follow_command": fmt.Sprintf("'repmgr standby follow -f %s --log-to-file --upstream-node-id=%%n'", node.ManagerConfigPath),
"event_notification_command": fmt.Sprintf("'/usr/local/bin/event_handler -node-id %%n -event %%e -success %%s -details \"%%d\"'"),
"event_notifications": "'repmgrd_failover_promote,standby_promote'",
"location": node.Region,
}

for key, value := range conf {
Expand Down