46 lines
1017 B
Go
46 lines
1017 B
Go
package update
|
|
|
|
import (
|
|
domain "RenewCMS/internal/domain/update"
|
|
"encoding/json"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
type JSONRunningInfoStore struct {
|
|
path string
|
|
mu sync.Mutex
|
|
}
|
|
|
|
var _ domain.RunningInfoStore = &JSONRunningInfoStore{}
|
|
|
|
func NewJSONRunningInfoStore(path string) *JSONRunningInfoStore {
|
|
return &JSONRunningInfoStore{path: path}
|
|
}
|
|
|
|
func (s *JSONRunningInfoStore) Write(info domain.RunningInfo) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
return writeJSONAtomically(s.path, info, readableFilePermissions)
|
|
}
|
|
|
|
func (s *JSONRunningInfoStore) Read() (domain.RunningInfo, bool) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
linkInfo, err := os.Lstat(s.path)
|
|
if err != nil || !linkInfo.Mode().IsRegular() {
|
|
return domain.RunningInfo{}, false
|
|
}
|
|
content, err := os.ReadFile(s.path)
|
|
if err != nil {
|
|
return domain.RunningInfo{}, false
|
|
}
|
|
var info domain.RunningInfo
|
|
if err := json.Unmarshal(content, &info); err != nil || info.Version == "" || info.Pid == 0 {
|
|
return domain.RunningInfo{}, false
|
|
}
|
|
return info, true
|
|
}
|