Skip to content

Commit 1a178a3

Browse files
committed
Merge branch '316-revert-failed-session' into 'master'
fix: clean up a failed clone session (#316) * clean up the data clone of a failed clone * destroy the container of a failed clone Closes #316 See merge request postgres-ai/database-lab!404
2 parents 2b503be + c4da8d1 commit 1a178a3

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

internal/provision/mode_local.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,26 @@ func (p *Provisioner) StartSession(snapshotID string, user resources.EphemeralUs
171171

172172
defer func() {
173173
if err != nil {
174-
p.revertSession(name)
174+
p.revertSession(fsm, name)
175175

176176
if portErr := p.FreePort(port); portErr != nil {
177177
log.Err(portErr)
178178
}
179179
}
180180
}()
181181

182-
if err := fsm.CreateClone(name, snapshot.ID); err != nil {
182+
if err = fsm.CreateClone(name, snapshot.ID); err != nil {
183183
return nil, errors.Wrap(err, "failed to create clone")
184184
}
185185

186186
appConfig := p.getAppConfig(fsm.Pool(), name, port)
187187
appConfig.SetExtraConf(extraConfig)
188188

189-
if err := postgres.Start(p.runner, appConfig); err != nil {
189+
if err = postgres.Start(p.runner, appConfig); err != nil {
190190
return nil, errors.Wrap(err, "failed to start a container")
191191
}
192192

193-
if err := p.prepareDB(appConfig, user); err != nil {
193+
if err = p.prepareDB(appConfig, user); err != nil {
194194
return nil, errors.Wrap(err, "failed to prepare a database")
195195
}
196196

@@ -263,30 +263,30 @@ func (p *Provisioner) ResetSession(session *resources.Session, snapshotID string
263263

264264
defer func() {
265265
if err != nil {
266-
p.revertSession(name)
266+
p.revertSession(newFSManager, name)
267267
}
268268
}()
269269

270-
if err := postgres.Stop(p.runner, fsm.Pool(), name); err != nil {
270+
if err = postgres.Stop(p.runner, fsm.Pool(), name); err != nil {
271271
return nil, errors.Wrap(err, "failed to stop container")
272272
}
273273

274-
if err := fsm.DestroyClone(name); err != nil {
274+
if err = fsm.DestroyClone(name); err != nil {
275275
return nil, errors.Wrap(err, "failed to destroy clone")
276276
}
277277

278-
if err := newFSManager.CreateClone(name, snapshot.ID); err != nil {
278+
if err = newFSManager.CreateClone(name, snapshot.ID); err != nil {
279279
return nil, errors.Wrap(err, "failed to create clone")
280280
}
281281

282282
appConfig := p.getAppConfig(newFSManager.Pool(), name, session.Port)
283283
appConfig.SetExtraConf(session.ExtraConfig)
284284

285-
if err := postgres.Start(p.runner, appConfig); err != nil {
285+
if err = postgres.Start(p.runner, appConfig); err != nil {
286286
return nil, errors.Wrap(err, "failed to start container")
287287
}
288288

289-
if err := p.prepareDB(appConfig, session.EphemeralUser); err != nil {
289+
if err = p.prepareDB(appConfig, session.EphemeralUser); err != nil {
290290
return nil, errors.Wrap(err, "failed to prepare database")
291291
}
292292

@@ -383,15 +383,15 @@ func buildPoolEntry(fsm pool.FSManager) (models.PoolEntry, error) {
383383
}
384384

385385
// Other methods.
386-
func (p *Provisioner) revertSession(name string) {
386+
func (p *Provisioner) revertSession(fsm pool.FSManager, name string) {
387387
log.Dbg(`Reverting start of a session...`)
388388

389-
if runnerErr := postgres.Stop(p.runner, p.pm.First().Pool(), name); runnerErr != nil {
390-
log.Err(`Revert:`, runnerErr)
389+
if runnerErr := postgres.Stop(p.runner, fsm.Pool(), name); runnerErr != nil {
390+
log.Err("Stop Postgres:", runnerErr)
391391
}
392392

393-
if runnerErr := p.pm.First().DestroyClone(name); runnerErr != nil {
394-
log.Err(`Revert:`, runnerErr)
393+
if runnerErr := fsm.DestroyClone(name); runnerErr != nil {
394+
log.Err("Destroy clone:", runnerErr)
395395
}
396396
}
397397

0 commit comments

Comments
 (0)