#!/sbin/openrc-run

extra_started_commands="finish"

name="GitLab Sidekiq"
description="GitLab backgroud workers"
description_finish="Stop fetching new jobs and finish current ones"

: ${gitlab_base:="/usr/lib/bundles/gitlab"}
: ${metrics_dir:="/dev/shm/gitlab"}

: ${sidekiq_logfile:="/var/log/gitlab/sidekiq.log"}
: ${sidekiq_memkiller_max_rss:="2000"}  # default per Omnibus
: ${sidekiq_metrics_dir:="$metrics_dir/sidekiq"}
: ${sidekiq_negate:="no"}
: ${sidekiq_queue_groups:="*"}
: ${sidekiq_queue_selector:="no"}

command="$gitlab_base/bin/sidekiq-cluster"
# Note: The rest of the options is set in start_pre().
command_args="-r $gitlab_base -e production ${command_args:-}"
command_background="yes"
command_user="git"

directory="$gitlab_base"
error_log="$sidekiq_logfile"
output_log="$sidekiq_logfile"

supervise_daemon_args="
	--env RAILS_ENV=production
	--env NODE_ENV=production
	--env EXECJS_RUNTIME=Disabled
	--env TZ=:/etc/localtime
	--env SIDEKIQ_MEMORY_KILLER_MAX_RSS=$(( sidekiq_memkiller_max_rss * 1024 ))
	--env prometheus_multiproc_dir=$sidekiq_metrics_dir
	"
start_stop_daemon_args="--interpreted $supervise_daemon_args"
pidfile="/run/gitlab/sidekiq.pid"

depend() {
	need redis
	use net postgresql
}

start_pre() {
	yesno "$sidekiq_queue_selector" && command_args="$command_args --queue-selector"

	command_args="$command_args
		$(optif --max-concurrency ${sidekiq_max_concurrency:-})
		$(optif --min-concurrency ${sidekiq_min_concurrency:-})
		$(optif --interval ${sidekiq_interval:-})
		$(optif --timeout ${sidekiq_shutdown_timeout:-})
		$(set -f; printf "'%s' " $sidekiq_queue_groups)
		"
	yesno "$sidekiq_negate" && command_args="$command_args --negate"

	checkpath -d -m 755 -o $command_user -q "${pidfile%/*}" || return 1
	checkpath -d -m 700 -o $command_user -q "$metrics_dir" || return 1
	checkpath -d -m 700 -o $command_user --directory-truncate "$sidekiq_metrics_dir" || return 1
	checkpath -f -m 644 -o $command_user "$sidekiq_logfile"
}

finish() {
	ebegin "Telling $name to stop fetching new jobs"

	if [ "$supervisor" ]; then
		$supervisor "$RC_SVCNAME" --signal TSTP
	else
		start-stop-daemon --pidfile "$pidfile" --signal TSTP
	fi
	eend $?
}

optif() {
	test -n "$2" && printf '%s/n' "$1=$2" || true
}
