Skip to content

Commit

Permalink
fix(api): Clean up database locks on SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
iofq committed Sep 3, 2024
1 parent 372ce3c commit 6d7f78b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
15 changes: 10 additions & 5 deletions api/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,20 @@ func apiMirrorsUpdate(c *gin.Context) {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to update: %s", err)
}

defer func() {
cleanupDB := func() {
// on any interruption, unlock the mirror
e := context.ReOpenDatabase()
if e == nil {
err := context.ReOpenDatabase()
if err == nil {
remote.MarkAsIdle()
collection.Update(remote)
}
}
defer cleanupDB()

context.GoContextHandleSignals()
go func() {
<-context.Done()
cleanupDB()
}()

remote.MarkAsUpdating()
Expand All @@ -408,8 +415,6 @@ func apiMirrorsUpdate(c *gin.Context) {
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to update: %s", err)
}

context.GoContextHandleSignals()

count := len(queue)
taskDetail := struct {
TotalDownloadSize int64
Expand Down
12 changes: 8 additions & 4 deletions cmd/mirror_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,24 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
context.Progress().Printf("Building download queue...\n")
queue, downloadSize, err = repo.BuildDownloadQueue(context.PackagePool(), collectionFactory.PackageCollection(),
collectionFactory.ChecksumCollection(nil), skipExistingPackages)

if err != nil {
return fmt.Errorf("unable to update: %s", err)
}

defer func() {
cleanupDB := func() {
// on any interruption, unlock the mirror
err = context.ReOpenDatabase()
if err == nil {
repo.MarkAsIdle()
collectionFactory.RemoteRepoCollection().Update(repo)
}
}
defer cleanupDB()

context.GoContextHandleSignals()
go func() {
<-context.Done()
cleanupDB()
}()

repo.MarkAsUpdating()
Expand All @@ -116,8 +122,6 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
return fmt.Errorf("unable to update: %s", err)
}

context.GoContextHandleSignals()

count := len(queue)
context.Progress().Printf("Download queue: %d items (%s)\n", count, utils.HumanBytes(downloadSize))

Expand Down
3 changes: 2 additions & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime/pprof"
"strings"
"sync"
"syscall"
"time"

"github.com/aptly-dev/aptly/aptly"
Expand Down Expand Up @@ -558,7 +559,7 @@ func (context *AptlyContext) GoContextHandleSignals() {

// Catch ^C
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
signal.Notify(sigch, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

var cancel gocontext.CancelFunc

Expand Down

0 comments on commit 6d7f78b

Please sign in to comment.