move some common things to forgepb

This commit is contained in:
Jeff Carr 2025-02-15 12:16:27 -06:00
parent 46e329f419
commit 5e837be94b
5 changed files with 21 additions and 63 deletions

View File

@ -8,6 +8,7 @@ import (
"time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
@ -41,7 +42,10 @@ func main() {
me.pollDelay = 3 * time.Second
me.failcountmax = 20 // die every minute if zookeeper can't be found
me.machine.ConfigLoad()
// me.machine.ConfigLoad()
me.forge = forgepb.InitPB()
me.forge.InitMachine()
go NewWatchdog()

48
post.go
View File

@ -1,48 +0,0 @@
package main
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"os/user"
"go.wit.com/log"
)
func httpPost(url string, data []byte) ([]byte, error) {
var err error
var req *http.Request
// data := []byte("some junk")
// url := "https://go.wit.com/register/"
req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
usr, _ := user.Current()
req.Header.Set("author", usr.Username)
hostname, _ := os.Hostname()
req.Header.Set("hostname", hostname)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error(err)
return []byte("client.Do(req) error"), err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
return body, err
}
// test := strings.TrimSpace(string(body))
// log.Info("go.wit.com returned body:", test)
// if test == "OK" {
// return body, nil
// }
return body, nil
}

12
send.go
View File

@ -12,14 +12,14 @@ import (
func pingStatus() error {
var url string
url = me.urlbase + "/status?hostname=" + me.machine.Hostname
msg, err := me.machine.Packages.Marshal()
url = me.urlbase + "/status?hostname=" + me.forge.Machine.Hostname
msg, err := me.forge.Machine.Packages.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
return err
}
log.Info("proto Marshal len =", len(msg))
body, err := httpPost(url, msg)
body, err := me.forge.HttpPost(url, msg)
if err != nil {
log.Info("httpPost() failed:", err)
return err
@ -41,12 +41,12 @@ func pingStatus() error {
func sendMachine(s string) error {
var url string
url = me.urlbase + "/machine"
msg, err := me.machine.Marshal()
msg, err := me.forge.Machine.Marshal()
if err != nil {
log.Info("proto.Marshal() failed:", err)
return err
}
body, err := httpPost(url, msg)
body, err := me.forge.HttpPost(url, msg)
if err != nil {
log.Info("httpPost() failed: url", url)
log.Info("httpPost() failed:", err)
@ -69,7 +69,7 @@ func sendMachine(s string) error {
os.Exit(0)
} else {
log.Info(me.urlbase, "is maybe not working GOT:", line)
log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.machine.Hostname)
log.Info(me.urlbase, "fail count", me.failcount, "from hostname", me.forge.Machine.Hostname)
}
}
return nil

View File

@ -3,17 +3,19 @@ package main
import (
"time"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/lib/protobuf/forgepb"
)
var me *stuff
// this app's variables
type stuff struct {
urlbase string // the dns name for the zookeeper
pollDelay time.Duration // how often to report our status
dog *time.Ticker // the watchdog timer
machine zoopb.Machine // populated from protobuf based zoopb
failcount int // how many times we've failed to contact the zookeeper
failcountmax int // after this, exit and let systemd restart the daemon
urlbase string // the dns name for the zookeeper
hostname string // my hostname
pollDelay time.Duration // how often to report our status
dog *time.Ticker // the watchdog timer
// machine zoopb.Machine // populated from protobuf based zoopb
forge *forgepb.Forge // handle to forge
failcount int // how many times we've failed to contact the zookeeper
failcountmax int // after this, exit and let systemd restart the daemon
}

View File

@ -31,7 +31,7 @@ func NewWatchdog() {
return
case _ = <-me.dog.C:
// log.Info("Watchdog() ticked", me.zookeeper, "Current time: ", t)
s := me.machine.UpdatePackages()
s := me.forge.Machine.UpdatePackages()
// pingStatus()
me.failcount += 1
sendMachine(s)