Skip to content

Commit

Permalink
Merge pull request pedroaxl#2 from bargeapp/master
Browse files Browse the repository at this point in the history
use opsworks native deploy_to and rails_env
  • Loading branch information
pedroaxl committed Jan 21, 2015
2 parents c5fd3a9 + 6908e92 commit 575d760
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 45 deletions.
60 changes: 48 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,74 @@ This is a very simple cookbook to deploy a pool of resque workers directly in Am

# Usage

In your custom layer, you must add this recipes to each stage
## Rails App Server layer type

**Configure**
If you're using a _Rails App Server_ layer type you'll need to add the following custom recipes to your layer:

rails::configure
opsworks-resque::configure
**Setup**

opsworks-resque::setup

**Deploy**

deploy::rails
opsworks-resque::restart

**Shutdown**
**Undeploy**

opsworks-resque::stop

## Other layer types

If you're using another layer type you'll need to configure some Rails recipes too:

**Setup**

opsworks-resque::setup

**Configure**

rails::configure

**Deploy**

deploy::rails opsworks-resque::restart

**Undeploy**

opsworks-resque::stop deploy::rails-undeploy

# Attributes

It expects an array with the queues of workers to run, for example
```ruby
default['resque']['path'] = "/srv/www/mailee_staging/current"
default['resque']['workers'] = {"*" => 2, "images" => 1} # 2 workers for queue * and 1 worker for queue images
default['resque']['rails_env'] = "production"
```json
"resque": {
"app-name": {
"workers": {
"*": 1
}
}
}
```

if you're not using the _Rails App Server_ layer type you'll also need to specify a `rails_env` like so:

```json
"resque": {
"app-name": {
"rails_env": "development",
"workers": {
"*": 1
}
}
}
```

# Recipes

**opsworks-resque::configure** - initial setup
**opsworks-resque::setup** - initial setup
**opsworks-resque::restart** - restart the workers (after deploy)
**opsworks-resque::stop** - stop the workers (shutdown)

# Author

Author:: Pedro Axelrud (<pedroaxl@gmail.com>)
Author:: Pedro Axelrud (<pedroaxl@gmail.com>)
2 changes: 0 additions & 2 deletions attributes/configure.rb

This file was deleted.

1 change: 1 addition & 0 deletions attributes/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default[:resque][:workers] = { '*' => 1 }
25 changes: 0 additions & 25 deletions recipes/configure.rb

This file was deleted.

8 changes: 5 additions & 3 deletions recipes/restart.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
service 'resque' do
action [:stop, :start]
provider Chef::Provider::Service::Upstart
node[:deploy].each do |application, deploy|
service "resque-#{application}" do
action [:stop, :start]
provider Chef::Provider::Service::Upstart
end
end
34 changes: 34 additions & 0 deletions recipes/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Cookbook Name:: opsworks-resque
# Recipe:: default
#
# Copyright (C) 2014 PEDRO AXELRUD
#
# All rights reserved - Do Not Redistribute
#

node[:deploy].each do |application, deploy|

Chef::Log.info("Configuring resque for application #{application}")

template "/etc/init/resque-#{application}.conf" do
source "resque.conf.erb"
mode '0644'
variables deploy: deploy
end

settings = node[:resque][application]
# configure rails_env in case of non-rails app
rack_env = deploy[:rails_env] || settings[:rack_env] || settings[:rails_env]
settings[:workers].each do |queue, quantity|

quantity.times do |idx|
idx = idx + 1 # make index 1-based
template "/etc/init/resque-#{application}-#{idx}.conf" do
source "resque-n.conf.erb"
mode '0644'
variables application: application, rack_env: rack_env, deploy: deploy, queue: queue, instance: idx
end
end
end
end
11 changes: 8 additions & 3 deletions templates/default/resque-n.conf.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!upstart
description "Resque worker <%= @instance %> <%= @queue %>"
respawn
start on starting resque-<%= @application %>
stop on stopping resque-<%= @application %>

start on starting resque
stop on stopping resque
env QUEUE=<%= @queue %>
env RACK_ENV=<%= @rack_env %>
env TERM_CHILD=1

exec su - deploy -c 'cd <%= node['resque']['path'] %>; QUEUE=<%= @queue %> VERBOSE=1 RAILS_ENV=<%= node['resque']['rails_env'] %> bundle exec rake resque:work >> /var/log/resque/<%= @instance %>.log 2>&1'
chdir <%= @deploy[:deploy_to] %>/current

exec sudo -u deploy -E -- bundle exec rake resque:work >> /var/log/resque/<%= @application %>-<%= @instance %>.log 2>&1
5 changes: 5 additions & 0 deletions templates/default/resque.conf.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!upstart
description "Resque workers"

<% @deploy[:environment].each do |k,v| %>
env <%= k %>=<%= v %>
export <%= k %>
<% end %>

pre-start script

bash << "EOF"
Expand Down

0 comments on commit 575d760

Please sign in to comment.