diff options
author | Brian Woods | 2023-03-19 15:34:13 -0400 |
---|---|---|
committer | Brian Woods | 2023-03-19 15:38:17 -0400 |
commit | fd95baa09a45fa657cd83144d63e0110f8d75a59 (patch) | |
tree | 7ad5bfc2f36ccbe7dac45ab006450ad5b78bbd37 /queue/queue_add.sh | |
parent | c35ea7d2a4f72eec5b730cee90cec3819887c174 (diff) |
Add a simple command queuing system. Simple but useful for adding
multiple commands to a queue to run sequentially.
Diffstat (limited to 'queue/queue_add.sh')
-rwxr-xr-x | queue/queue_add.sh | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/queue/queue_add.sh b/queue/queue_add.sh new file mode 100755 index 0000000..74fb2df --- /dev/null +++ b/queue/queue_add.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# SPDX-FileCopyrightText: 2023 Brian Woods +# SPDX-License-Identifier: GPL-2.0-or-later + +# paths +LOCK="queue.lock" +QUEUE="queue.file" +WORKER="./queue_worker.sh" +WORKER_PID="queue_worker.pid" +WORKER_LOG="queue_worker.log" + +queue_push () +{ + local lock_fd="3" + # aquire lock + if ! exec {lock_fd}>"$LOCK" ; then + echo "Can't get lockfile fd" + return + fi + + if ! flock -w 10 "$lock_fd" ; then + echo "couldn't get lock" + return + fi + + # actually write it out + echo "$@" >> "$QUEUE" + + # release lock + flock -u "$lock_fd" + exec {lock_fd}>&- +} + +spawn_worker () +{ + nohup "$WORKER" >> "$WORKER_LOG" & + + # 5 seconds of waiting + local max=50 + local tries=0 + while [ "$tries" -lt "$max" ] ; do + sleep .1 + tries=$(( tries + 1 )) + + if [ -f "$WORKER_PID" ] ; then + break + fi + done + if [ "$tries" = "$max" ] ; then + echo "warning waiting period without worker PID created" + fi +} + +queue_push $@ + +# need to do some locking if we want this to be actually safe +# 1. get lock, 2. spin off worker, 3. wait until pid, 4. release lock +if [ ! -f "$WORKER_PID" ] ; then + spawn_worker +fi |