Skip to content

Commit

Permalink
Daemon working with beanstalk, spec for custom plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
winton committed Jun 28, 2011
1 parent 3a79f56 commit 6ffd6a1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 39 deletions.
6 changes: 3 additions & 3 deletions bin/stasis
Expand Up @@ -7,8 +7,8 @@ require 'slop'

opts = Slop.parse :help => true do
on :a, :auto, 'Auto-regenerate'
on :b, :beanstalk, 'Beanstalk addresses (comma-separated, default: localhost:11300)', :optional => true, :as => Array, :default => "localhost:11300"
on :o, :only, 'Only generate specific files (comma-separated)', :as => Array
on :p, :port, 'Port (daemon mode)', :optional => true, :as => Integer, :default => 7000
end

args = []
Expand All @@ -20,8 +20,8 @@ if opts.auto?
Stasis::Auto.new(dir)
elsif opts.only?
Stasis.new(dir).generate(:only => opts[:only])
elsif opts.port?
Stasis::Daemon.new(dir, opts[:port])
elsif opts.beanstalk?
Stasis::Daemon.new(opts)
else
Stasis.new(dir).generate
end
1 change: 1 addition & 0 deletions config/gemsets.yml
@@ -1,4 +1,5 @@
stasis:
beanstalk-client: ~>1.1.0
directory_watcher: ~>1.4.0
rake: >=0.8.7
rocco: ~>0.6
Expand Down
1 change: 1 addition & 0 deletions config/gemspec.yml
Expand Up @@ -7,6 +7,7 @@ homepage: http://wintoni.us
summary: Markup-agnostic static site generator
description: A markup-agnostic static site generator.
dependencies:
- beanstalk-client
- directory_watcher
- slop
- tilt
Expand Down
2 changes: 1 addition & 1 deletion lib/stasis.rb
Expand Up @@ -26,7 +26,7 @@

# Activate the [Tilt][ti] gem.

Stasis::Gems.activate %w(tilt yajl-ruby)
Stasis::Gems.activate %w(tilt)

# Add the project directory to the load paths.

Expand Down
48 changes: 14 additions & 34 deletions lib/stasis/daemon.rb
@@ -1,56 +1,36 @@
Stasis::Gems.activate %w(eventmachine yajl-ruby)
require 'eventmachine'
Stasis::Gems.activate %w(beanstalk-client yajl-ruby)

require 'beanstalk-client'
require 'yajl'

class Stasis
class Daemon

def initialize(dir, port)
@port = port

last_retry = nil
retries = 0
def initialize(options={})
puts "\nStarting Stasis daemon (beanstalk @ #{options[:beanstalk].join(', ')})..."

puts "\nStarting Stasis daemon on port #{port}..."
beanstalk = Beanstalk::Pool.new(options[:beanstalk])

begin
EM.epoll if EM.epoll?
EM.run do
EM.start_server '0.0.0.0', port, Tcp
while true
sleep(1.0 / 1000.0)
job = beanstalk.reserve
data = Yajl::Parser.parse(job.body)
puts data.inspect
job.delete
end
rescue Interrupt
shut_down
rescue Exception => e
puts "\nError: #{e.message}"
puts "\t#{e.backtrace.join("\n\t")}"

if retries >= 10 && Time.now - last_retry < 10
shut_down
else
retries += 1
last_retry = Time.now
end
retry
end
end

def shut_down
puts "\nShutting down Stasis daemon on port #{@port}..."
puts "\nShutting down Stasis daemon..."
exit
end

module Tcp
def self.parser
@parser ||= Yajl::Parser.new(:symbolize_keys => true)
end

def parser
self.class.parser
end

def receive_data(data)
puts parser.parse(data).inspect
send_data "OK\n"
end
end
end
end
2 changes: 1 addition & 1 deletion spec/fixtures/project/plugin.rb
Expand Up @@ -4,7 +4,7 @@ class Plugin < Stasis::Plugin
def plugin(controller, controllers, paths)
if controller._[:root] == controller._[:dir]
controller.before("custom_plugin.html") do
instead render(:text => 'root')
instead "pass"
end
end
[ controller, controllers, paths ]
Expand Down
4 changes: 4 additions & 0 deletions spec/stasis_spec.rb
Expand Up @@ -10,6 +10,10 @@
$files['not_dynamic.html'].should =~ /pass/
end

it "should use the custom plugin" do
$files['custom_plugin.html'].should =~ /pass/
end

describe 'generate with :only option' do

before(:each) do
Expand Down

0 comments on commit 6ffd6a1

Please sign in to comment.