lock cache during ROM move operation
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-04 15:58:16 -06:00
parent 449d48258d
commit 9a2e992964

22
main.go
View File

@ -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