Class: BackgroundCache::Daemon
- Inherits:
-
Object
- Object
- BackgroundCache::Daemon
- Defined in:
- lib/background_cache/daemon.rb
Instance Method Summary collapse
-
#initialize(root) ⇒ Daemon
constructor
A new instance of Daemon.
- #shut_down ⇒ Object
Constructor Details
#initialize(root) ⇒ Daemon
Returns a new instance of Daemon.
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 75 76 77 78 79 80 |
# File 'lib/background_cache/daemon.rb', line 13 def initialize(root) if File.exists?(yaml = "#{root}/config/background_cache.yml") = YAML.load(File.read(yaml)) else puts "\nFAIL: config/background_cache.yml not found" shut_down end puts "\nStarting background cache server (redis @ #{['redis']})..." require "#{root}/config/environment.rb" require File.("../../background_cache", __FILE__) instance = BackgroundCache.boot redis = Redis.connect(:url => "redis://#{['redis']}") retries = 0 begin while true request = redis.lpop('background_cache:request') if request puts request.inspect Timeout.timeout(60) do request = Yajl::Parser.parse(request) channel = request.delete('channel') cache_key = 'background_cache:queue:' + request.to_a.sort { |a, b| a.first <=> b.first }.inspect request.keys.each do |key| request[(key.to_sym rescue key) || key] = request.delete(key) end unless redis.get(cache_key) redis.set(cache_key, 1) # Timeout incase execution fails redis.expire(cache_key, 5 * 60) if request[:group] BackgroundCache.cache!(request[:group], instance) else BackgroundCache.manual( BackgroundCache::Config.build_cache(request), instance ) end redis.del(cache_key) redis.publish( "background_cache:response:#{channel}", "[SUCCESS]" ) end end end sleep(1.0 / 1000.0) end rescue Interrupt shut_down rescue Exception => e puts "\nError: #{e.}" puts "\t#{e.backtrace.join("\n\t")}" retries += 1 shut_down if retries >= 10 retry end end |
Instance Method Details
#shut_down ⇒ Object
82 83 84 85 |
# File 'lib/background_cache/daemon.rb', line 82 def shut_down puts "\nShutting down background cache server..." exit end |