Checking for online working

This commit is contained in:
Daan Selen
2025-05-27 10:24:33 +02:00
parent 6c191910d2
commit 78d127bd46
8 changed files with 70 additions and 20 deletions

Binary file not shown.

3
server/python.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
./../runner/venv/bin/python3 ./../runner/runner.py

View File

@@ -7,7 +7,6 @@ import (
"ghostrunner-server/modules/restapi"
"ghostrunner-server/modules/timekeeper"
"ghostrunner-server/modules/utilities"
"ghostrunner-server/modules/wrapper"
"log"
)
@@ -31,15 +30,5 @@ func main() {
log.Println(utilities.InfoTag, "Letting TimeKeeper take over...")
log.Println(utilities.InfoTag, fmt.Sprintf("Interval set at: %d seconds.", cfg.Interval))
onDevices, err := wrapper.PyListOnline(cfg.PyVenvName)
if err != nil {
log.Println(utilities.ErrTag, err)
}
for index, device := range onDevices.OnlineDevices {
log.Println(index, device)
}
log.Println(len(onDevices.OnlineDevices))
timekeeper.KeepTime(cfg.Interval)
timekeeper.KeepTime(cfg.Interval, cfg)
}

View File

@@ -0,0 +1,35 @@
package timekeeper
import (
"ghostrunner-server/modules/database"
"ghostrunner-server/modules/utilities"
"log"
)
func routine(cfg utilities.ConfigStruct, pyListArgs []string) {
d := listDevices(cfg, pyListArgs) // Retrieve the Online devices.
curTasks := database.RetrieveTasks()
for index, device := range d.OnlineDevices {
log.Println(index, device)
}
for index, task := range curTasks {
log.Println(index, task)
for _, nodeid := range task.Nodeids {
if isNodeOnline(nodeid, d.OnlineDevices) {
log.Printf("NodeID %s is online\n", nodeid)
}
}
}
}
func isNodeOnline(nodeid string, onlineDevices []utilities.Device) bool {
for _, device := range onlineDevices {
if device.NodeID == nodeid {
return true
}
}
return false
}

View File

@@ -2,11 +2,17 @@ package timekeeper
import (
"ghostrunner-server/modules/utilities"
"ghostrunner-server/modules/wrapper"
"log"
"strings"
"time"
)
func KeepTime(interval int) {
var ( // Debugging
pyListArgs = strings.Fields("-lo")
)
func KeepTime(interval int, cfg utilities.ConfigStruct) {
transInterval := time.Duration(interval) * time.Second
ticker := time.NewTicker(transInterval)
@@ -14,5 +20,16 @@ func KeepTime(interval int) {
for t := range ticker.C {
log.Println(utilities.InfoTag, "Tick at:", t)
log.Println(utilities.InfoTag, "Starting Routine.")
routine(cfg, pyListArgs)
}
}
func listDevices(cfg utilities.ConfigStruct, pyArgs []string) utilities.PyOnlineDevices {
onDevices, err := wrapper.PyListOnline(cfg.PyVenvName, pyArgs)
if err != nil {
log.Println(utilities.ErrTag, err)
}
return onDevices
}

View File

@@ -55,6 +55,7 @@ type Device struct {
}
type PyOnlineDevices struct {
OnlineDevices []Device `json:"online_devices"`
TotalDevices int `json:"total_devices"`
OnlineDevices []Device `json:"online_devices"`
OfflineDevices []Device `json:"offline_devices"`
TotalDevices int `json:"total_devices"`
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"ghostrunner-server/modules/utilities"
"log"
"os"
"os/exec"
)
@@ -12,11 +13,14 @@ const (
pyFile = "./../runner/runner.py"
)
func PyListOnline(venvName string) (utilities.PyOnlineDevices, error) {
func PyListOnline(venvName string, pyArgs []string) (utilities.PyOnlineDevices, error) {
pyBin := fmt.Sprintf("./../runner/%s/bin/python", venvName)
cmd := exec.Command(pyBin, pyFile, "-lo")
runtimeArgs := append([]string{pyFile}, pyArgs...)
cmd := exec.Command(pyBin, runtimeArgs...)
rawData, err := cmd.CombinedOutput()
log.Println(string(rawData))
if err != nil {
cwd, _ := os.Getwd()
return utilities.PyOnlineDevices{}, fmt.Errorf("python execution failed, working directory: %s", cwd)