diff --git a/main.go b/main.go index 4651454..226c891 100644 --- a/main.go +++ b/main.go @@ -153,7 +153,9 @@ func moveBuildArtifacts(outDirectory, romDirectory string) bool { newROMs = true log.Printf("new build found - moving file %s", v.Name()) + romCache.Lock() // lock to prevent multiple concurrent goroutines moving the same file err := moveBuildFile(fmt.Sprintf("%s/%s", outDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name())) + romCache.Unlock() if err != nil { log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err) continue @@ -182,11 +184,16 @@ func isLineageROMZip(v fs.DirEntry) (bool, []string) { // http - GET / // Writes JSON response for the updater app to know what versions are available to download func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { - romCache.Lock() - lineageOSROMs := romCache.ROMs - romCache.Unlock() + go func() { + newBuilds := moveBuildArtifacts("out", "public") + if newBuilds { + updateROMCache("public") + } + }() - httpResponseJSON := &HTTPResponseJSON{Response: lineageOSROMs} + romCache.Lock() + httpResponseJSON := &HTTPResponseJSON{Response: romCache.ROMs} + romCache.Unlock() b, err := json.Marshal(httpResponseJSON) if err != nil { @@ -196,13 +203,6 @@ func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { } w.Write(b) - - go func() { - newBuilds := moveBuildArtifacts("out", "public") - if newBuilds { - updateROMCache("public") - } - }() } // Returns a sha256 hash of a file located at the path provided