summaryrefslogtreecommitdiff
path: root/main.go
blob: 047f4165e57d365ee1727d3e6f2c66e682f3a901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"embed"
	"os"
	"time"

	"go.wit.com/dev/alexflint/arg"
	"go.wit.com/lib/protobuf/zoopb"
	"go.wit.com/log"
)

var VERSION string
var BUILDTIME string

//go:embed resources/*
var resources embed.FS

func main() {
	me = new(zoodStruct)
	me.pp = arg.MustParse(&argv)

	if me.pp == nil {
		me.pp.WriteHelp(os.Stdout)
		os.Exit(0)
	}

	if argv.Daemon {
		// turn off timestamps for STDOUT (systemd adds them)
		log.DaemonMode(true)
		me.machine, me.fullpath = zoopb.InitDaemon()
	} else {
		me.machine, me.fullpath = zoopb.InitMachine()

	}

	me.urlbase = "http://zookeeper.chap.wit.com:8080"
	if argv.URL != "" {
		log.Info("USING ARGV URL:", argv.URL)
		me.urlbase = argv.URL
	}

	me.pollDelay = 3 * time.Second
	me.failcountmax = 20 // die every minute if zookeeper can't be found

	if argv.Test != nil {
		testURL(me.urlbase, me.machine)
		os.Exit(0)
	}

	zood() // talks to zookeeper
}

func testURL(urlbase string, pb *zoopb.Machine) error {
	newpb, wsPB, err := pb.HttpPost(urlbase, "test")
	if err != nil {
		log.Info("got error:", err)
		return err
	}
	wsPB.DumpClient()
	newpb.Dump()
	log.Info("got error:", err)
	return err
}