@@ -14,6 +14,7 @@ import (
14
14
const (
15
15
pathToHBAFile = "/data/postgresql/pg_hba.conf"
16
16
pathToHBABackup = "/data/postgresql/pg_hba.conf.bak"
17
+ postmasterPath = "/data/postgresql/postmaster.pid"
17
18
restoreLockFile = "/data/restore.lock"
18
19
)
19
20
@@ -85,6 +86,12 @@ func prepareRemoteRestore(ctx context.Context, node *Node) error {
85
86
86
87
svisor .Stop ()
87
88
89
+ // Wait for the postmaster to exit
90
+ // TODO - This should be done in the supervisor
91
+ if err := waitForPostmasterExit (ctx ); err != nil {
92
+ return fmt .Errorf ("failed to wait for postmaster to exit: %s" , err )
93
+ }
94
+
88
95
// Set the lock file so the init process knows not to restart
89
96
// the restore process.
90
97
if err := setRestoreLock (); err != nil {
@@ -98,6 +105,27 @@ func prepareRemoteRestore(ctx context.Context, node *Node) error {
98
105
return nil
99
106
}
100
107
108
+ func waitForPostmasterExit (ctx context.Context ) error {
109
+ ticker := time .NewTicker (1 * time .Second )
110
+ timeout := time .After (10 * time .Second )
111
+ defer ticker .Stop ()
112
+ for {
113
+ select {
114
+ case <- ticker .C :
115
+ switch _ , err := os .Stat (postmasterPath ); {
116
+ case os .IsNotExist (err ):
117
+ return nil
118
+ case err != nil :
119
+ return fmt .Errorf ("error checking postmaster file: %v" , err )
120
+ }
121
+ case <- timeout :
122
+ return fmt .Errorf ("timed out waiting for postmaster to exit" )
123
+ case <- ctx .Done ():
124
+ return ctx .Err ()
125
+ }
126
+ }
127
+ }
128
+
101
129
func isRestoreActive () (bool , error ) {
102
130
if _ , err := os .Stat (restoreLockFile ); err == nil {
103
131
val , err := os .ReadFile (restoreLockFile )
0 commit comments