pgbouncer.initd 1.93 KB
Newer Older
1 2 3 4
#!/sbin/openrc-run

extra_started_commands="reload"

5 6 7 8 9 10
: ${user:="pgbouncer"}
: ${group:="postgresql"}
: ${cfgfile:="/etc/pgbouncer/pgbouncer.ini"}
: ${nice_timeout:=60}
: ${force_quit:="no"}
: ${force_quit_timeout:=2}
11

12 13 14 15
name="PgBouncer"
command="/usr/bin/pgbouncer"
command_args="-q $cfgfile"
command_background="yes"
16

17 18 19 20
pidfile="/run/$RC_SVCNAME.pid"
start_stop_daemon_args="
	--user $user
	--group $group"
21

22 23 24 25 26
required_files="$cfgfile"

depend() {
	use net
	after postgresql
27 28
}

29 30 31 32 33 34 35 36 37 38
start_pre() {
	local socket_dir=$(get_config unix_socket_dir)
	if [ -n "$socket_dir" ]; then
		checkpath -d -m 0755 -o postgres:postgres "$socket_dir" || return 1
	fi

	local logfile="$(get_config logfile)"
	if [ -n "$logfile" ]; then
		checkpath -f -m 0640 -o $user:$group "$logfile" || return 1
	fi
39 40 41
}

stop() {
42 43 44 45
	local retry="SIGINT/$nice_timeout"
	yesno "$force_quit" \
		&& retry="$retry/SIGTERM/$force_quit_timeout" \
		|| force_quit_timeout=0
46

47
	local seconds=$(( $nice_timeout + $force_quit_timeout ))
48

49
	ebegin "Stopping $seconds (this can take up to $seconds seconds)"
50

51 52 53 54 55 56
	start-stop-daemon --stop \
		--pidfile "$pidfile" \
		--retry "$retry" \
		--progress \
		--exec "$command"
	eend $?
57 58 59
}

restart() {
60 61 62 63 64 65 66 67 68
	local socket_dir=$(get_config unix_socket_dir)

	if [ -n "$socket_dir" ]; then
		ebegin "Performing online restart of $name"
		"$command" -R "$command_args"
		eend $?
	else
		stop && start
	fi
69 70 71
}

reload() {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	ebegin "Reloading $name configuration"
	start-stop-daemon --signal HUP --pidfile "$pidfile"
	eend $?
}

get_config() {
	local name="$1"
	local default="${2:-}"

	if [ ! -f "$conffile" ]; then
		printf '%s\n' "$default"
		return 1
	fi
	sed -En "/^\s*${name}\b/{                      # find line starting with the name
		  s/^\s*${name}\s*=?\s*([^#]+).*/\1/;  # capture the value
		  s/\s*$//;                            # trim trailing whitespaces
		  s/^['\"](.*)['\"]$/\1/;              # remove delimiting quotes
		  p
		}" "$conffile" \
		| grep . || printf '%s\n' "$default"
92
}