diff --git a/cmd/vidforward/Makefile b/cmd/vidforward/Makefile index b6a14da5..5839ba13 100644 --- a/cmd/vidforward/Makefile +++ b/cmd/vidforward/Makefile @@ -3,7 +3,7 @@ USER := $(shell whoami) PATH := /usr/local/go/bin:$(PATH) BIN_NAME := vidforward BIN_DIR := /src/bitbucket.org/ausocean/av/cmd/$(BIN_NAME) -RUN_SCRIPT_DIR := $(BIN_DIR) +RUN_SCRIPT_DIR := $(BIN_DIR)/run.sh .SILENT:soft_copy_files .SILENT:hard_copy_files @@ -11,12 +11,12 @@ RUN_SCRIPT_DIR := $(BIN_DIR) rebuild: chmod +x run.sh - cd ../cmd/$(BIN_NAME); go build + go build -tags nocv,debug install: as_root soft_copy_files rebuild @echo "Install complete" -install_hard: as_root hard_copy_files set_mac rebuild +install_hard: as_root hard_copy_files rebuild @echo "Hard install complete" as_root: diff --git a/cmd/vidforward/create_service.sh b/cmd/vidforward/create_service.sh index 609d2de0..a219b480 100644 --- a/cmd/vidforward/create_service.sh +++ b/cmd/vidforward/create_service.sh @@ -15,6 +15,9 @@ run_script_dir=$1 # This corresponds to the binary dir. e.g. /src/bitbucket.org/ausocean/av/cmd/vidforward. bin_dir=$2 +# This is the IP we'll run the host on. +host=$(hostname -I | awk '{print $1}') + # Get the bin name (assuming this is at the end of the bin_dir). bin_name=$(basename $bin_dir) @@ -27,6 +30,8 @@ gopath_user=${arr_in[1]} # We can now form the gopath from the obtained user. gopath="/home/$gopath_user/go" +echo "$gopath$run_script_dir $gopath_user $bin_dir $host" + # Here are the lines that will go into the rv.service file. We'll set the # ExecStart field as the GOPATH we've obtained + the passed run script dir. service=" @@ -34,8 +39,8 @@ service=" Description=vidforward service for forwarding video to youtube [Service] -Type=simple -ExecStart=$gopath$run_script_dir $gopath_user $bin_dir +Type=notify +ExecStart=$gopath$bin_dir/vidforward -host $host WatchdogSec=30s Restart=on-failure diff --git a/cmd/vidforward/file_test.go b/cmd/vidforward/file_test.go index cc7083d3..891f241b 100644 --- a/cmd/vidforward/file_test.go +++ b/cmd/vidforward/file_test.go @@ -22,7 +22,6 @@ LICENSE along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. */ - package main import ( @@ -42,7 +41,7 @@ const ( testMAC = "78:90:AE:7B:2C:76" ) -func init(){ +func init() { inTest = true } @@ -203,17 +202,17 @@ func broadcastManagersEqual(m1, m2 broadcastManager) bool { } func broadcastMapsEqual(m1, m2 map[MAC]Broadcast) bool { - return mapsEqual(m1,m2,broadcastsEqual) + return mapsEqual(m1, m2, broadcastsEqual) } func slateExitSignalMapsEqual(m1, m2 map[MAC]chan struct{}) bool { - return mapsEqual(m1,m2,func(v1, v2 chan struct{}) bool { - return ((v1 == nil || v2 == nil ) && v1 == v2) || (v1 != nil && v2 != nil) + return mapsEqual(m1, m2, func(v1, v2 chan struct{}) bool { + return ((v1 == nil || v2 == nil) && v1 == v2) || (v1 != nil && v2 != nil) }) } func activeHandlersMapEqual(m1, m2 map[int]handlerInfo) bool { - return mapsEqual(m1,m2, func(v1, v2 handlerInfo) bool { return v1.name == v2.name }) + return mapsEqual(m1, m2, func(v1, v2 handlerInfo) bool { return v1.name == v2.name }) } // mapsEqual is a generic function to check that any two maps are equal based on @@ -224,7 +223,7 @@ func mapsEqual[K comparable, V any](m1, m2 map[K]V, cmp func(v1, v2 V) bool) boo } for k, v1 := range m1 { v2, ok := m2[k] - if !ok || !cmp(v1,v2) { + if !ok || !cmp(v1, v2) { return false } } diff --git a/cmd/vidforward/notifier.go b/cmd/vidforward/notifier.go index cb0d87ea..0eed5532 100644 --- a/cmd/vidforward/notifier.go +++ b/cmd/vidforward/notifier.go @@ -51,6 +51,7 @@ type watchdogNotifier struct { termCallback func() log logging.Logger mu sync.Mutex + haveRun bool } // handlerInfo keeps track of a handlers name (for any logging purposes) and @@ -65,18 +66,7 @@ type handlerInfo struct { // and termination callback that is called if a SIGINT or SIGTERM signal is // received. Recommended use of this is an attempted state save. func newWatchdogNotifier(l logging.Logger, termCallback func()) (*watchdogNotifier, error) { - var ( - interval = 1 * time.Minute - err error - ) - - if notifyWatchdog { - const clearEnvVars = false - interval, err = daemon.SdWatchdogEnabled(clearEnvVars) - if err != nil { - return nil, err - } - } + interval := 1 * time.Minute return &watchdogNotifier{ activeHandlers: make(map[int]handlerInfo), @@ -93,7 +83,7 @@ func newWatchdogNotifier(l logging.Logger, termCallback func()) (*watchdogNotifi // notify also starts a routine to monitor for any SIGINT or SIGTERM, upon which // a callback that's provided at initialisation is called. func (n *watchdogNotifier) notify() { - notifyTicker := time.NewTicker(n.watchdogInterval / 2) + notifyTicker := time.NewTicker(n.watchdogInterval / 2.0) go func() { sigs := make(chan os.Signal, 1) @@ -116,6 +106,30 @@ func (n *watchdogNotifier) notify() { continue } + if !n.haveRun { + n.haveRun = true + + const clearEnvVars = false + ok, err := daemon.SdNotify(clearEnvVars, daemon.SdNotifyReady) + if err != nil { + n.log.Fatal("unexpected watchog notify read error", "error", err) + } + + if !ok { + n.log.Fatal("watchdog notification not supported") + } + + n.watchdogInterval, err = daemon.SdWatchdogEnabled(clearEnvVars) + if err != nil { + n.log.Fatal("unexpected watchdog error", "error", err) + } + + if n.watchdogInterval == 0 { + n.log.Fatal("Watchdog not enabled or this is the wrong PID") + } + + } + // If this fails for any reason it indicates a systemd service configuration // issue, and therefore programmer error, so do fatal log to cause crash. supported, err := daemon.SdNotify(false, daemon.SdNotifyWatchdog) diff --git a/cmd/vidforward/run.sh b/cmd/vidforward/run.sh deleted file mode 100644 index c10ab639..00000000 --- a/cmd/vidforward/run.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash -e -# This script launches vidforward. This is used by the service file. - -# Check that we have the correct number of arguments passed. -if [ $# -ne 2 ]; then - echo "incorrect number of arguments, expected gopath user and binary directory" - exit 1 -fi - -# This is the user in the GOPATH e.g. for /home/foo/go the user is foo. -gopath_user=$1 - -# This is the dir of the binary from the GOPATH e.g. /src/bitbucket.org/ausocean/av/cmd/vidforward. -bin_dir=$2 - -# We'll get the bin name from the bin dir (assuming this is same as the bin dir name). -bin_name=$(basename $bin_dir) - -# the following required directories _should_ already exist -if [ ! -d /var/log/vidforward ]; then - sudo mkdir /var/log/vidforward - chmod guo+rwx /var/log/vidforward -fi - -# show IP addresses -echo Our IP addresses: -sudo ip addr show | grep inet - -# capture stdout and stderr to a secondary log file (just in case) -exec 2> /var/log/vidforward/stream.log -exec 1>&2 - -# Now set all required variables. -HOME=/home/$gopath_user -GOPATH=$HOME/go -VIDFORWARD_PATH=$GOPATH$bin_dir -PATH=$PATH:/usr/local/go/bin:$VIDFORWARD_PATH -cd $VIDFORWARD_PATH -sudo -u $gopath_user HOME=$HOME GOPATH=$GOPATH PATH=$PATH ./$bin_name -if [ $? -eq 0 ] -then - echo "Successfully exited vidforward" - exit 0 -else - echo "vidforward exited with code: $?" >&2 - exit 1 -fi