Class: Stasis::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/stasis/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/stasis/server.rb', line 11

def initialize(root, options={})
  puts "\nStarting Stasis server (redis @ #{options[:server]})..."

  redis = Redis.connect(:url => "redis://#{options[:server]}")
  stasis = Stasis.new(*[ root, options[:public], options ].compact)
  retries = 0

  begin
    while true
      sleep(1.0 / 1000.0)
      request = redis.lpop('stasis:requests')

      if request
        files = {}
        request = Yajl::Parser.parse(request)
        paths = request['paths']

        unless request['force']
          paths = request['paths'].reject do |path|
            files[path] = redis.get("stasis:caches:#{root}:#{path}")
          end
        end

        if paths.empty? && !request['paths'].empty?
          new_files = {}
        else
          params = request['paths'] + [
            {
              :collect => request['return'] || request['force'],
              :params => request['params'],
              :write => request['write']
            }
          ]
          new_files = stasis.render(*params) || {}
        end

        if request['ttl']
          new_files.each do |path, body|
            key = "stasis:caches:#{root}:#{path}"
            redis.set(key, body)
            redis.expire(key, request['ttl'])
          end
        end

        if request['return']
          request['wait'] = true
        end

        if request['wait']
          response = files.merge(new_files)
          redis.publish(self.class.response_key(request['id']), Yajl::Encoder.encode(response))
        end
      end
    end
  rescue Interrupt
    shut_down
  rescue Exception => e
    puts "\nError: #{e.message}"
    puts "\t#{e.backtrace.join("\n\t")}"
    retries += 1
    shut_down if retries >= 10
    retry
  end
end

Class Method Details

.push(options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/stasis/server.rb', line 82

def push(options)
  options[:id] = Digest::SHA1.hexdigest("#{options['paths']}#{rand}")
  redis_url = "redis://#{options.delete(:redis) || "localhost:6379/0"}"
  response = nil

  redis_1 = Redis.connect(:url => redis_url)
  redis_2 = Redis.connect(:url => redis_url)

  if options[:return] || options[:wait]
    redis_1.subscribe(response_key(options[:id])) do |on|
      on.subscribe do |channel, subscriptions|
        redis_2.rpush("stasis:requests", Yajl::Encoder.encode(options))
      end

      on.message do |channel, message|
        response = Yajl::Parser.parse(message)
        redis_1.unsubscribe
      end
    end
  else
    redis_1.rpush("stasis:requests", Yajl::Encoder.encode(options))
  end

  redis_1.quit
  redis_2.quit

  response
end

.response_key(id) ⇒ Object



111
112
113
# File 'lib/stasis/server.rb', line 111

def response_key(id)
  "stasis:response:#{id}"
end

Instance Method Details

#shut_downObject



76
77
78
79
# File 'lib/stasis/server.rb', line 76

def shut_down
  puts "\nShutting down Stasis server..."
  exit
end