Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Stasis::Server mostly working

  • Loading branch information...
commit 048eccba2fd6b790602d86d7eaefcad56128b095 1 parent fa956a6
@winton authored
View
2  README.md
@@ -255,7 +255,7 @@ Stasis server that uses redis on port 6379:
<!-- highlight:-s language:console -->
- $ stasis -s localhost:6379
+ $ stasis -s localhost:6379/0
Push to the server (in Ruby):
View
17 bin/stasis
@@ -7,21 +7,16 @@ require 'slop'
opts = Slop.parse :help => true do
on :d, :development, "Development mode\t\t(auto-regenerate)"
- on :o, :only, "Only generate specific files\t(comma-separated)", :as => Array
- on :s, :server, "Server mode\t\t\t(default redis host: localhost:6379)", :optional => true, :default => "localhost:6379"
+ on :o, :only, "Only generate specific files\t(comma-separated)", :optional => true, :as => Array
+ on :s, :server, "Server mode\t\t\t(default redis host: localhost:6379/0)", :optional => true, :default => "localhost:6379/0"
end
-args = []
-opts.parse { |arg| args << arg }
-
-dir = args.first || Dir.pwd
-
if opts.development?
- Stasis::DevMode.new(dir, opts)
+ Stasis::DevMode.new(Dir.pwd, opts)
elsif opts.only?
- Stasis.new(dir).generate(:only => opts[:only])
+ Stasis.new(Dir.pwd).render(*opts[:only])
elsif opts.server?
- Stasis::Server.new(opts)
+ Stasis::Server.new(Dir.pwd, opts)
else
- Stasis.new(dir).generate
+ Stasis.new(Dir.pwd).render
end
View
24 lib/stasis.rb
@@ -92,6 +92,7 @@ def initialize(root, destination=root+'/public')
end
def render(*only)
+ collect = {}
options = {}
if only.last.is_a?(::Hash)
@@ -103,17 +104,17 @@ def render(*only)
# If `path` is a regular expression...
if path.is_a?(::Regexp)
array << path
- # If `path` exists...
- elsif File.exists?(path)
- array << path
# If `root + path` exists...
elsif (path = File.expand_path(path, root)) && File.exists?(path)
array << path
+ # If `path` exists...
+ elsif File.exists?(path)
+ array << path
end
array
end
- if options[:only].empty?
+ if only.empty?
# Remove old generated files.
FileUtils.rm_rf(destination)
end
@@ -135,10 +136,10 @@ def render(*only)
@paths.uniq.each do |path|
@path = path
- # If `:only` option specified...
- unless options[:only].empty?
+ # If `only` parameters given...
+ unless only.empty?
# Skip iteration unless there is a match.
- next unless options[:only].any? do |only|
+ next unless only.any? do |only|
# Regular expression match.
(only.is_a?(::Regexp) && @path =~ only) ||
(
@@ -195,7 +196,7 @@ def render(*only)
trigger(:after_render)
# Cut the `root` out of the `path` to get the relative destination.
- dest = @path[root.length..-1]
+ relative = @path[root.length..-1]
# Add `destination` (as specified from `Stasis.new`) to front of relative
# destination.
@@ -218,6 +219,10 @@ def render(*only)
File.open(dest, 'w') do |f|
f.write(view)
end
+ # Collect render output.
+ if options[:collect]
+ collect[relative] = view
+ end
# If markup was not rendered and the path exists...
elsif File.exists?(@path)
# Copy the file located at the path to the destination path.
@@ -230,6 +235,9 @@ def render(*only)
# Unset class-level instance variables.
@action, @path = nil, nil
+
+ # Respond with collected render output if `collect` option given.
+ collect if options[:collect]
end
# Add a plugin to all existing controller instances. This method should be called by
View
15 lib/stasis/dev_mode.rb
@@ -9,7 +9,10 @@ def initialize(dir, options={})
puts "\nDevelopment mode enabled: #{dir}"
- generate(dir)
+ @dir = dir
+ @options = options
+
+ render
dw = DirectoryWatcher.new(@stasis.root)
dw.interval = 1
@@ -27,7 +30,7 @@ def initialize(dir, options={})
dw.add_observer do |*events|
modified = events.detect { |e| e[:type] == :modified }
- generate(dir) if modified
+ render if modified
end
dw.start
@@ -36,11 +39,11 @@ def initialize(dir, options={})
private
- def generate(dir)
- puts "\n[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] Regenerating project..."
+ def render
+ puts "\n[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] Regenerating #{@options[:only].empty? ? 'project' : @options[:only].join(', ')}..."
begin
- @stasis = Stasis.new(dir)
- @stasis.generate
+ @stasis = Stasis.new(@dir)
+ @stasis.render(*@options[:only])
rescue Exception => e
puts "\n[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] Error: #{e.message}`"
puts "\t#{e.backtrace.join("\n\t")}"
View
76 lib/stasis/server.rb
@@ -1,28 +1,49 @@
Stasis::Gems.activate %w(redis yajl-ruby)
+require 'digest/sha1'
require 'redis'
require 'yajl'
class Stasis
class Server
- def initialize(options={})
- puts "\nStarting Stasis daemon (redis @ #{options[:redis]})..."
+ def initialize(root, options={})
+ puts "\nStarting Stasis server (redis @ #{options[:server]})..."
- host, port = options[:redis].split(':')
- redis = Redis.new(:host => host, :port => port)
- stasis = Stasis.new(Dir.pwd)
+ redis = Redis.connect(:url => "redis://#{options[:server]}")
+ stasis = Stasis.new(root)
begin
while true
sleep(1.0 / 1000.0)
- data = redis.lpop('stasis:requests')
- if data
- data = Yajl::Parser.parse(data)
- # stasis.generate(
- # :only => data['request_uri']
- # )
- puts data.inspect
+ request = redis.lpop('stasis:requests')
+
+ if request
+ request = Yajl::Parser.parse(request)
+ files = nil
+
+ puts request.inspect
+ stasis.render(*request['paths'], :locals => request['locals'])
+
+ if request['return'] && request['paths'] && !request['paths'].empty?
+ files = request['paths'].inject({}) do |hash, path|
+ path = "#{root}/public/#{path}"
+ puts path
+ hash[path] = File.read(path) if File.file?(path)
+ hash
+ end
+ request['wait'] = true
+ end
+
+ if request['wait']
+ response = {
+ :id => request['id'],
+ :files => files
+ }
+ redis.publish(self.class.response_key(request['id']), Yajl::Encoder.encode(response))
+ end
+
+ puts response.inspect
end
end
rescue Interrupt
@@ -35,8 +56,37 @@ def initialize(options={})
end
def shut_down
- puts "\nShutting down Stasis daemon..."
+ puts "\nShutting down Stasis server..."
exit
end
+
+ class <<self
+ def push(options)
+ options[:id] = Digest::SHA1.hexdigest("#{options['paths']}#{Random.rand}")
+ redis = options.delete(:redis)
+ response = nil
+
+ redis.rpush("stasis:requests", Yajl::Encoder.encode(options))
+
+ if options[:return] || options[:wait]
+ redis.subscribe(response_key(options[:id])) do |on|
+ on.subscribe do |channel, subscriptions|
+
+ end
+
+ on.message do |channel, message|
+ response = Yajl::Parser.parse(message)
+ redis.unsubscribe
+ end
+ end
+ end
+
+ response
+ end
+
+ def response_key(id)
+ "stasis:response:#{id}"
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.