Skip to content

Commit

Permalink
Makes possible starting the worker as a daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
guiocavalcanti committed Oct 31, 2012
1 parent 38bab5f commit 12f044c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.swo
bin
.bundle
tmp
log
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'amqp'
gem 'yajl-ruby'
gem 'rake'
gem 'configurable'
gem 'daemons'

group :development, :test do
gem 'rspec'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ GEM
configurable (1.0.0)
config_parser (~> 0.5.4)
lazydoc (~> 1.0)
daemons (1.1.9)
debugger (1.2.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.1.1)
Expand Down Expand Up @@ -98,6 +99,7 @@ DEPENDENCIES
amqp
bson_ext
configurable
daemons
debugger
em-mongo
em-synchrony
Expand Down
6 changes: 6 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ permit > curl -H 'Accept: application/json' http://0.0.0.0:9000/rules?resource_i

## Running

### Server

You just need to run the server with ``ruby server.rb -sv``

### AMQP Consumer

``bundle exec ruby worker.rb`` or, as a daemon, ``bundle exec permitd.rb start``

### Deploy

In order to deploy on heroku you should enable some MongoDB addon and set environment variables:
Expand Down
15 changes: 15 additions & 0 deletions permitd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'bundler/setup'
require 'daemons'

require_relative 'boot'

options = {
:backtrace => true,
:log_output => true,
:app_name => 'permit',
:dir_mode => :script,
:dir => '/tmp/pids',
:log_dir => "#{File.expand_path File.dirname __FILE__}/log"
}

Daemons.run("worker.rb", options)
17 changes: 14 additions & 3 deletions worker.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
require_relative 'boot'
$:.unshift File.expand_path 'lib'

require 'amqp'
require 'yajl/json_gem'

AMQP.start do |connection|
require 'permit/config'
require 'permit/consumer'
require 'permit/worker'

AMQP.start do |connection|
EventMachine.synchrony do
Goliath.env = :development
Permit.configure do |c|
dirname = "#{File.dirname(__FILE__)}/log"
Dir.mkdir dirname unless File.exists?(dirname)
c.logger = Logger.new("#{dirname}/permitd_#{c.env}.log")
end

channel = AMQP::Channel.new(connection)
exchange = channel.topic("permit", :auto_delete => true)
consumer = Permit::Consumer.new
worker = Permit::Worker.new(:consumer => consumer, :channel => channel,
:exchange => exchange)
worker.start

# For testing...
#
# EM::add_periodic_timer do
# policy = {:resource_id => 'r', :subject_id => 's',
# :actions => { :read => true } }
Expand Down

0 comments on commit 12f044c

Please sign in to comment.