Skip to content

Commit

Permalink
use registry for status
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Nov 11, 2024
1 parent bd6205a commit 40c619b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
12 changes: 6 additions & 6 deletions cmd/windows_agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (p *agentService) startPing() {
var pingResp PingResp
pingErr := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/ping", nil, &pingResp)
if pingErr != nil {
utils.SetEnvironment("PBS_AGENT_STATUS", fmt.Sprintf("Error - (%s)", pingErr.Error()))
agent.SetStatus(fmt.Sprintf("Error - (%s)", pingErr.Error()))
} else if !pingResp.Data.Pong {
utils.SetEnvironment("PBS_AGENT_STATUS", "Error - server did not return expected data")
agent.SetStatus("Error - server did not return expected data")
} else {
utils.SetEnvironment("PBS_AGENT_STATUS", "Connected")
agent.SetStatus("Connected")
}
}

Expand All @@ -60,7 +60,7 @@ func (p *agentService) startPing() {
retryWait := utils.WaitChan(time.Second * 5)
select {
case <-p.ctx.Done():
utils.SetEnvironment("PBS_AGENT_STATUS", "Agent service is not running")
agent.SetStatus("Agent service is not running")
return
case <-retryWait:
ping()
Expand All @@ -69,10 +69,10 @@ func (p *agentService) startPing() {
}

func (p *agentService) run() {
utils.SetEnvironment("PBS_AGENT_STATUS", "Starting")
agent.SetStatus("Starting")
logger, err := syslog.InitializeLogger(p.svc)
if err != nil {
utils.SetEnvironment("PBS_AGENT_STATUS", fmt.Sprintf("Failed to initialize logger -> %s", err.Error()))
agent.SetStatus(fmt.Sprintf("Failed to initialize logger -> %s", err.Error()))
return
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/windows_agent/systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/getlantern/systray"
"github.com/kardianos/service"
"github.com/sonroyaalmerol/pbs-plus/internal/agent"
"github.com/sonroyaalmerol/pbs-plus/internal/syslog"
"github.com/sonroyaalmerol/pbs-plus/internal/utils"
"golang.org/x/sys/windows"
Expand Down Expand Up @@ -107,8 +108,8 @@ func (p *agentTray) onReady(url string) func() {
go func(ctx context.Context, status *systray.MenuItem, url *string) {
setStatus := func() {
if url != nil && *url != "" {
svcStatus, ok := os.LookupEnv("PBS_AGENT_STATUS")
if !ok {
svcStatus, err := agent.GetStatus()
if err != nil {
status.SetTitle("Status: Agent service not running")
} else {
status.SetTitle(fmt.Sprintf("Status: %s", svcStatus))
Expand Down
26 changes: 26 additions & 0 deletions internal/agent/systray_comm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package agent

import (
"golang.org/x/sys/windows/registry"
)

func SetStatus(status string) error {
key, _, err := registry.CreateKey(registry.LOCAL_MACHINE, `Software\PBSPlus`, registry.ALL_ACCESS)
if err == nil {
defer key.Close()
err := key.SetStringValue("Status", status)
return err
}
return err
}

func GetStatus() (string, error) {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `Software\PBSPlus`, registry.QUERY_VALUE)
if err == nil {
defer key.Close()
regStatus, _, err := key.GetStringValue("Status")
return regStatus, err
}

return "", err
}

0 comments on commit 40c619b

Please sign in to comment.