mirror of
https://github.com/glanceapp/glance.git
synced 2025-06-21 18:31:24 +02:00
Rework getting host info (#340)
This commit is contained in:
parent
232cab01f8
commit
3043a0bd15
@ -3,6 +3,7 @@ package sysinfo
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -85,12 +86,39 @@ type MointpointRequest struct {
|
|||||||
// Currently caches hostname indefinitely which isn't ideal
|
// Currently caches hostname indefinitely which isn't ideal
|
||||||
// Potential issue with caching boot time as it may not initially get reported correctly:
|
// Potential issue with caching boot time as it may not initially get reported correctly:
|
||||||
// https://github.com/shirou/gopsutil/issues/842#issuecomment-1908972344
|
// https://github.com/shirou/gopsutil/issues/842#issuecomment-1908972344
|
||||||
var cachedHostInfo = struct {
|
type cacheableHostInfo struct {
|
||||||
available bool
|
available bool
|
||||||
hostname string
|
hostname string
|
||||||
platform string
|
platform string
|
||||||
bootTime timestampJSON
|
bootTime timestampJSON
|
||||||
}{}
|
}
|
||||||
|
|
||||||
|
var cachedHostInfo cacheableHostInfo
|
||||||
|
|
||||||
|
func getHostInfo() (cacheableHostInfo, error) {
|
||||||
|
var err error
|
||||||
|
info := cacheableHostInfo{}
|
||||||
|
|
||||||
|
info.hostname, err = os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
return info, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info.platform, _, _, err = host.PlatformInformation()
|
||||||
|
if err != nil {
|
||||||
|
return info, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bootTime, err := host.BootTime()
|
||||||
|
if err != nil {
|
||||||
|
return info, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info.bootTime = timestampJSON{time.Unix(int64(bootTime), 0)}
|
||||||
|
info.available = true
|
||||||
|
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
func Collect(req *SystemInfoRequest) (*SystemInfo, []error) {
|
func Collect(req *SystemInfoRequest) (*SystemInfo, []error) {
|
||||||
if req == nil {
|
if req == nil {
|
||||||
@ -117,13 +145,9 @@ func Collect(req *SystemInfoRequest) (*SystemInfo, []error) {
|
|||||||
if cachedHostInfo.available {
|
if cachedHostInfo.available {
|
||||||
applyCachedHostInfo()
|
applyCachedHostInfo()
|
||||||
} else {
|
} else {
|
||||||
hostInfo, err := host.Info()
|
hostInfo, err := getHostInfo()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cachedHostInfo.available = true
|
cachedHostInfo = hostInfo
|
||||||
cachedHostInfo.bootTime = timestampJSON{time.Unix(int64(hostInfo.BootTime), 0)}
|
|
||||||
cachedHostInfo.hostname = hostInfo.Hostname
|
|
||||||
cachedHostInfo.platform = hostInfo.Platform
|
|
||||||
|
|
||||||
applyCachedHostInfo()
|
applyCachedHostInfo()
|
||||||
} else {
|
} else {
|
||||||
addErr(fmt.Errorf("getting host info: %v", err))
|
addErr(fmt.Errorf("getting host info: %v", err))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user