1
0
Fork 0
mirror of https://github.com/Luzifer/tex-api.git synced 2024-11-08 16:20:04 +00:00

Use atomic operations to update status

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-03-02 21:19:17 +01:00
parent e2da6ba41f
commit 4bcb077fbf
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

20
main.go
View file

@ -42,10 +42,11 @@ const (
) )
const ( const (
filenameInput = "input.zip" filenameInput = "input.zip"
filenameStatus = "status.json" filenameStatus = "status.json"
filenameOutputDir = "output" filenameStatusTemp = "status.tmp.json"
sleepBase = 1.5 filenameOutputDir = "output"
sleepBase = 1.5
) )
type jobStatus struct { type jobStatus struct {
@ -79,13 +80,20 @@ func (s *jobStatus) UpdateStatus(st status) {
func (s jobStatus) Save() error { func (s jobStatus) Save() error {
uid, _ := uuid.FromString(s.UUID) // #nosec G104 uid, _ := uuid.FromString(s.UUID) // #nosec G104
f, err := os.Create(pathFromUUID(uid, filenameStatus)) f, err := os.Create(pathFromUUID(uid, filenameStatusTemp))
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
return json.NewEncoder(f).Encode(s) if err = json.NewEncoder(f).Encode(s); err != nil {
return err
}
return os.Rename(
pathFromUUID(uid, filenameStatusTemp),
pathFromUUID(uid, filenameStatus),
)
} }
func urlMust(u *url.URL, err error) *url.URL { func urlMust(u *url.URL, err error) *url.URL {