Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for delayed_job4 #372

Open
wants to merge 1 commit into
base: next-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions cookbooks/delayed_job4/files/default/dj.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#!/bin/sh
#
# This script starts and stops the Dj daemon
# This script is created by the delayed_job4 recipe
# This script belongs in /engineyard/custom/dj
#
# Updated for Rails 4.x

PATH=/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
CURDIR=`pwd`
export NEW_RELIC_DISPATCHER=delayed_job

usage() {
echo "Usage: $0 <appname> {start|stop} enviroment [-n WORKER_NAME] [-p MIN_PRIORITY] [-P MAX_PRIORITY] [-q comma,separated,queues]"
exit 1
}

die() {
echo -e "fatal error: ${1}" 1>&2
! [ "$(echo ${0} | awk -F/ '{print $NF}')" == "bash" ] && exit 255 || return 255
}

defined() {
[ -n "${1}" ]
}

exists() {
defined "${1}" || die "exists - No path given."
[ -e "${1}" ]
}

is_file() {
defined "${1}" || die "is_file - No path given."
exists "${1}" && [ -f "${1}" ]
}

is_directory() {
defined "${1}" || die "is_directory - No path given."
exists "${1}" && [ -d "${1}" ]
}

add_arg() {
local argname="${1}"
local argcontent="${2}"
local original="${3}"

# Make no changes if the content to add is empty
if ! defined "${argcontent}"
then
echo -n "${original}"
return
fi

local arg="'${argname}=${argcontent}'"

if defined "${original}"
then
original="${arg}, ${original}"
else
original="${arg}"
fi

echo -n "${original}"
}

start() {
local app_name=${1}
local app_root="/data/${app_name}/current"
local rails_env=${3}

# Clear out the non-option stuff so getopts doesn't freak out
shift 3

local queues="${QUEUES}"
local worker_name=""
local min_priority=""
local max_priority=""
local OPTIND=1

while getopts ":n:p:P:q:" opt
do
case $opt in
n)
worker_name="${OPTARG}"
;;
q)
if defined "${queues}"
then
queues="${queues},${OPTARG}"
else
queues=${OPTARG}
fi
;;
p)
min_priority=${OPTARG}
;;
P)
max_priority=${OPTARG}
;;
:)
echo "Option -${OPTARG} requires an argument."
exit 1
;;
esac
done

# Sanitize the worker name
if ! defined "${worker_name}"
then
worker_name = 'nil'
fi

# Sanitize the queues (whitespace -> comma)
if defined "${queues}"
then
queues="$(echo "${queues}" | sed -e 's/[[:space:]]/,/g')"
fi

local worker_options=""
worker_options="$(add_arg "--queues" "${queues}" "${worker_options}")"
worker_options="$(add_arg '--max-priority' "${max_priority}" "${worker_options}")"
worker_options="$(add_arg '--min-priority' "${min_priority}" "${worker_options}")"

echo "worker_options == \"${worker_options}\""
}

stop() {
echo "stop got '${@}'"
}

main() {
if [ $# -lt 3 ]
then
usage
fi

local app_name=$1
local action=$2
local app_root="/data/${app_name}/current"
local rails_env=$3

if ! is_directory "${app_root}"
then
echo "${app_root} doesn't exist"
usage
fi

case "${action}" in
start)
start ${@}
;;
stop)
stop ${@}
;;
*)
echo "Unknown action '${action}'"
usage
;;
esac
}

main $@
4 changes: 2 additions & 2 deletions cookbooks/delayed_job4/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
mode 0755
end

template "/engineyard/custom/dj" do
source "dj.erb"
cookbook_file "/engineyard/custom/dj" do
source "dj.sh"
owner "root"
group "root"
mode 0755
Expand Down
210 changes: 0 additions & 210 deletions cookbooks/delayed_job4/templates/default/dj.erb

This file was deleted.

4 changes: 2 additions & 2 deletions cookbooks/delayed_job4/templates/default/dj.monitrc.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
check process <%= @worker_name %>
with pidfile /var/run/engineyard/dj/<%= @app_name %>/dj_<%= @worker_name %>.pid
start program = "/engineyard/custom/dj <%= @app_name %> start <%= @framework_env %> <%= @worker_name %>" with timeout 90 seconds
stop program = "/engineyard/custom/dj <%= @app_name %> stop <%= @framework_env %> <%= @worker_name %>" with timeout 90 seconds
start program = "/engineyard/custom/dj <%= @app_name %> start <%= @framework_env %> -n <%= @worker_name %>" with timeout 90 seconds
stop program = "/engineyard/custom/dj <%= @app_name %> stop <%= @framework_env %> -n <%= @worker_name %>" with timeout 90 seconds
if totalmem is greater than <%= @worker_memory %> MB then restart # eating up memory?
group dj_<%= @app_name %>