orcli/src/run_command.sh

94 lines
3.2 KiB
Bash
Raw Normal View History

2022-10-07 12:57:11 +02:00
# shellcheck shell=bash disable=SC2154 source=/dev/null
2022-04-20 12:27:53 +02:00
2022-10-07 11:02:05 +02:00
# catch args, convert the space delimited string to an array
files=()
eval "files=(${args[file]})"
2022-10-16 23:13:59 +02:00
# check if stdin is present if selected
if ! [[ ${args[--interactive]} ]]; then
if [[ ${args[file]} == '-' ]] || [[ ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
orcli_run_usage
exit 1
fi
fi
fi
2022-10-07 11:02:05 +02:00
# update OPENREFINE_URL env
OPENREFINE_URL="http://localhost:${args[--port]}"
2022-04-20 12:27:53 +02:00
# locate orcli and OpenRefine
2022-10-20 23:09:28 +02:00
scriptpath=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
if [[ -x "${scriptpath}/refine" ]]; then
openrefine="${scriptpath}/refine"
2022-04-20 12:27:53 +02:00
else
error "OpenRefine's startup script (refine) not found!" "Did you put orcli in your OpenRefine app dir?"
fi
# create tmp directory
2022-10-20 23:09:28 +02:00
OPENREFINE_TMPDIR="$(mktemp -d)"
trap '{ rm -rf "$OPENREFINE_TMPDIR"; }' 0 2 3 15
2022-04-20 12:27:53 +02:00
# check if OpenRefine is already running
if curl -fs "${OPENREFINE_URL}" &>/dev/null; then
error "OpenRefine is already running on port ${args[--port]}." "Hint: Stop the other process or use another port."
fi
# start OpenRefine with tmp workspace and autosave period 25 hours
2022-10-20 23:09:28 +02:00
REFINE_AUTOSAVE_PERIOD=1440 $openrefine -d "$OPENREFINE_TMPDIR" -m "${args[--memory]}" -p "${args[--port]}" -x refine.headless=true -v warn &>"$OPENREFINE_TMPDIR/openrefine.log" &
OPENREFINE_PID="$!"
2022-04-20 12:27:53 +02:00
# update trap to kill OpenRefine on error or exit
2022-10-20 23:09:28 +02:00
trap '{ rm -rf "$OPENREFINE_TMPDIR"; kill -9 "$OPENREFINE_PID"; }' 0 2 3 15
2022-04-20 12:27:53 +02:00
# wait until OpenRefine is running (timeout 20s)
if ! curl -fs --retry 20 --retry-connrefused --retry-delay 1 "${OPENREFINE_URL}/command/core/get-version" &>/dev/null; then
error "starting OpenRefine server failed!"
else
2022-10-20 23:09:28 +02:00
log "started OpenRefine" "port: ${args[--port]}" "memory: ${args[--memory]}" "tmpdir: ${OPENREFINE_TMPDIR}" "pid: ${OPENREFINE_PID}"
2022-04-20 12:27:53 +02:00
fi
2022-10-15 22:57:07 +02:00
# execute script(s) in subshell
2022-10-20 23:09:28 +02:00
export OPENREFINE_TMPDIR OPENREFINE_URL OPENREFINE_PID
2022-10-15 22:57:07 +02:00
if [[ ${args[file]} == '-' || ${args[file]} == '"-"' ]]; then
if ! read -u 0 -t 0; then
2022-10-16 23:13:59 +02:00
# case 1: interactive mode if stdin is selected but not present
2022-10-15 22:57:07 +02:00
bash --rcfile <(
cat ~/.bashrc
2022-10-22 23:42:49 +02:00
if ! command -v orcli &>/dev/null; then
echo "alias orcli=${scriptpath}/orcli"
2022-10-20 23:09:28 +02:00
fi
2022-10-15 22:57:07 +02:00
interactive
) -i </dev/tty
2022-10-15 22:57:07 +02:00
exit
fi
fi
2022-10-16 23:13:59 +02:00
if [[ ${args[--interactive]} ]]; then
# case 2: execute scripts and keep shell running
2022-10-07 12:57:11 +02:00
bash --rcfile <(
cat ~/.bashrc
2022-10-22 23:42:49 +02:00
if ! command -v orcli &>/dev/null; then
echo "alias orcli=${scriptpath}/orcli"
2022-10-20 23:09:28 +02:00
fi
for i in "${!files[@]}"; do
2022-10-22 23:42:49 +02:00
log "executing script ${files[$i]}..."
awk 1 "${files[$i]}"
done
2022-10-07 12:57:11 +02:00
interactive
) -i </dev/tty
2022-10-16 23:13:59 +02:00
else
# case 3: just execute scripts
for i in "${!files[@]}"; do
2022-10-22 23:42:49 +02:00
log "executing script ${files[$i]}..."
bash -e <(
if ! command -v orcli &>/dev/null; then
echo "shopt -s expand_aliases"
echo "alias orcli=${scriptpath}/orcli"
fi
awk 1 "${files[$i]}"
)
2022-10-16 23:13:59 +02:00
done
# print stats
log "used $(($(ps --no-headers -o rss -p "$OPENREFINE_PID") / 1024)) MB RAM and $(ps --no-headers -o cputime -p "$OPENREFINE_PID") CPU time"
2022-10-07 12:57:11 +02:00
fi