Skip to content

Instantly share code, notes, and snippets.

@mikz
Last active January 8, 2016 16:36
Show Gist options
  • Save mikz/3b0bc2ac97bfdf6263b4 to your computer and use it in GitHub Desktop.
Save mikz/3b0bc2ac97bfdf6263b4 to your computer and use it in GitHub Desktop.

Revisions

  1. mikz revised this gist Jan 8, 2016. 2 changed files with 85 additions and 11 deletions.
    20 changes: 16 additions & 4 deletions resque-worker.conf
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,35 @@
    #!upstart
    manual

    instance N=${N}
    instance ${N}
    kill timeout 10800 # 3 hours in seconds
    setuid user

    stop on stopping resque

    env N
    env QUEUE="*"
    env TASK="environment resque:work"
    env RAKE="/usr/local/bin/rake"
    env BUNDLER="/usr/local/bin/bundle"
    env COUNT=1
    env RAILS_ENV="production"
    env RAILS_ENV="staging"

    env DEFAULTFILE=/etc/default/resque

    chdir /home/bender/system_production/current
    chdir /home/user/system_staging/current

    script
    if [ -f "$DEFAULTFILE" ]; then
    . "$DEFAULTFILE"
    fi
    exec ${BUNDLER} exec ${RAKE} ${TASK}
    end script

    post-start script
    # wait for resque to boot and change its command name to resque-version
    while [ -z "${pid}" ] || ! ps -p ${pid} -o args | grep '^resque-'
    do
    pid=$(status ${UPSTART_JOB} N=${UPSTART_INSTANCE} | grep 'start/' | awk '{print $NF}')
    sleep 1
    done
    end script
    76 changes: 69 additions & 7 deletions resque.conf
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,79 @@
    #!upstart
    #manual
    start on startup

    stop on stopping resque-worker
    # stop on stopping resque-worker

    pre-start script
    # TODO: loop
    start resque-worker N=0 QUEUE="foo,*" || true
    start resque-worker N=1 QUEUE="bar,*" || true
    env QUEUE_1="*"
    env QUEUE_2="*"
    env QUEUE_3="*"
    env QUEUE_4="*"
    env QUEUE_5="*"
    env QUEUE_6="default"
    env WORKERS=6

    kill timeout 60 # resque workers have higher timeout

    script
    start_worker () {
    start resque-worker N=${1} QUEUE=$(eval echo \$QUEUE_${1}) || :
    }
    start_workers () {
    for n in $(seq ${WORKERS})
    do
    start_worker ${n} &
    done
    wait
    }

    restart_worker () {
    restart resque-worker N=${1}
    }

    stop_worker () {
    stop resque-worker N=${1} || :
    }

    reload_workers () {
    for n in $(seq ${WORKERS})
    do
    restart_worker ${n} &
    done
    wait
    }

    stop_workers () {
    for n in $(seq ${WORKERS})
    do
    stop_worker ${n} &
    done
    wait
    exit
    }

    trap reload_workers HUP
    trap stop_workers TERM

    start_workers

    while :
    do
    sleep 1 # just 1, so it can trap signals
    done

    end script

    post-start script
    while [ $(initctl list | grep resque-worker | grep start/running | wc -l) -ne ${WORKERS} ]
    do
    sleep 1 # wait until all the workers start
    done
    end script

    pre-stop script
    post-stop script
    for n in `initctl list|grep "^resque-worker "|awk '{print $2}'|tr -d ')'|tr -d '('`
    do
    stop resque-worker N=$n
    stop resque-worker N=$n &
    done
    wait
    end script
  2. mikz revised this gist Jan 5, 2016. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions resque-worker.conf
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #!upstart
    manual

    instance N=${N}
    1 change: 1 addition & 0 deletions resque.conf
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #!upstart
    start on startup

    stop on stopping resque-worker
  3. mikz renamed this gist Jan 5, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. mikz created this gist Jan 5, 2016.
    22 changes: 22 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    manual

    instance N=${N}

    env N
    env QUEUE="*"
    env TASK="environment resque:work"
    env RAKE="/usr/local/bin/rake"
    env BUNDLER="/usr/local/bin/bundle"
    env COUNT=1
    env RAILS_ENV="production"

    env DEFAULTFILE=/etc/default/resque

    chdir /home/bender/system_production/current

    script
    if [ -f "$DEFAULTFILE" ]; then
    . "$DEFAULTFILE"
    fi
    exec ${BUNDLER} exec ${RAKE} ${TASK}
    end script
    16 changes: 16 additions & 0 deletions resque.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    start on startup

    stop on stopping resque-worker

    pre-start script
    # TODO: loop
    start resque-worker N=0 QUEUE="foo,*" || true
    start resque-worker N=1 QUEUE="bar,*" || true
    end script

    pre-stop script
    for n in `initctl list|grep "^resque-worker "|awk '{print $2}'|tr -d ')'|tr -d '('`
    do
    stop resque-worker N=$n
    done
    end script