Fix various vidforward issues

This change fixes some bugs relating to the Makefile and the
systemd watchdog notifier system. We have also removed the run.sh
script which was the culprit of latter problem (not sure exactly
how, but probably something to do with PIDs).
This commit is contained in:
Saxon Nelson-Milton 2023-02-08 05:49:05 +00:00
parent 0f0deaa598
commit 683bb923cb
5 changed files with 43 additions and 72 deletions

View File

@ -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:

View File

@ -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

View File

@ -22,7 +22,6 @@ LICENSE
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package main
import (

View File

@ -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)

View File

@ -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