insque 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/generators/insque/templates/insque.erb +1 -10
- data/lib/insque/version.rb +1 -1
- data/lib/insque.rb +16 -9
- metadata +1 -1
| @@ -1,13 +1,4 @@ | |
| 1 1 | 
             
            Insque.sender = '<%= sender_name %>'
         | 
| 2 2 | 
             
            REDIS_CONFIG = YAML.load(File.open("#{Rails.root}/config/redis.yml"))[Rails.env]
         | 
| 3 | 
            -
             | 
| 4 | 
            -
            Insque.redis = $redis
         | 
| 3 | 
            +
            Insque.redis_config = { :host => REDIS_CONFIG["host"], :port => REDIS_CONFIG["port"] }
         | 
| 5 4 | 
             
            Insque.debug = true
         | 
| 6 | 
            -
            if defined?(PhusionPassenger)
         | 
| 7 | 
            -
              PhusionPassenger.on_event(:starting_worker_process) do |forked|
         | 
| 8 | 
            -
                if forked
         | 
| 9 | 
            -
                  $redis = Redis.new(:host => REDIS_CONFIG["host"], :port => REDIS_CONFIG["port"])
         | 
| 10 | 
            -
                  Insque.redis = $redis
         | 
| 11 | 
            -
                end
         | 
| 12 | 
            -
              end
         | 
| 13 | 
            -
            end
         | 
    
        data/lib/insque/version.rb
    CHANGED
    
    
    
        data/lib/insque.rb
    CHANGED
    
    | @@ -7,8 +7,9 @@ module Insque | |
| 7 7 | 
             
                @debug = debug
         | 
| 8 8 | 
             
              end
         | 
| 9 9 |  | 
| 10 | 
            -
              def self. | 
| 11 | 
            -
                @ | 
| 10 | 
            +
              def self.redis_config= redis
         | 
| 11 | 
            +
                @redis_config = redis
         | 
| 12 | 
            +
                @redis = Redis.new @redis_config
         | 
| 12 13 | 
             
                @redis.select 7
         | 
| 13 14 | 
             
              end
         | 
| 14 15 |  | 
| @@ -37,10 +38,13 @@ module Insque | |
| 37 38 | 
             
              end
         | 
| 38 39 |  | 
| 39 40 | 
             
              def self.listen worker_name=''
         | 
| 40 | 
            -
                 | 
| 41 | 
            +
                redis = Redis.new redis
         | 
| 42 | 
            +
                redis.select 7
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                redis.sadd 'insque_inboxes', @inbox
         | 
| 41 45 | 
             
                log "#{worker_name} START LISTENING #{@inbox}"
         | 
| 42 46 | 
             
                loop do
         | 
| 43 | 
            -
                  message =  | 
| 47 | 
            +
                  message = redis.brpoplpush(@inbox, @processing, 0)
         | 
| 44 48 | 
             
                  log "#{worker_name} RECEIVING: #{message}" if @debug
         | 
| 45 49 | 
             
                  begin
         | 
| 46 50 | 
             
                    parsed_message = JSON.parse message
         | 
| @@ -50,17 +54,20 @@ module Insque | |
| 50 54 | 
             
                    log e.inspect
         | 
| 51 55 | 
             
                    log e.backtrace
         | 
| 52 56 | 
             
                  end
         | 
| 53 | 
            -
                   | 
| 57 | 
            +
                  redis.lrem @processing, 0, message
         | 
| 54 58 | 
             
                end
         | 
| 55 59 | 
             
              end
         | 
| 56 60 |  | 
| 57 61 | 
             
              def self.janitor
         | 
| 62 | 
            +
                redis = Redis.new redis
         | 
| 63 | 
            +
                redis.select 7
         | 
| 64 | 
            +
             | 
| 58 65 | 
             
                loop do
         | 
| 59 | 
            -
                   | 
| 66 | 
            +
                  redis.watch @processing
         | 
| 60 67 | 
             
                  errors = []
         | 
| 61 68 | 
             
                  restart = []
         | 
| 62 69 | 
             
                  delete = []
         | 
| 63 | 
            -
                   | 
| 70 | 
            +
                  redis.lrange(@processing, 0, -1).each do |m|
         | 
| 64 71 | 
             
                    begin
         | 
| 65 72 | 
             
                      parsed_message = JSON.parse(m)
         | 
| 66 73 | 
             
                      if parsed_message['restarted_at'] && DateTime.parse(parsed_message['restarted_at']) < 1.hour.ago.utc
         | 
| @@ -74,7 +81,7 @@ module Insque | |
| 74 81 | 
             
                      log "========== JANITOR_BROKEN_MESSAGE: #{m} =========="
         | 
| 75 82 | 
             
                    end
         | 
| 76 83 | 
             
                  end
         | 
| 77 | 
            -
                  result =  | 
| 84 | 
            +
                  result = redis.multi do |r|
         | 
| 78 85 | 
             
                    restart.each {|m| r.lpush @inbox, m.to_json }
         | 
| 79 86 | 
             
                    delete.each {|m| r.lrem @processing, 0, m }
         | 
| 80 87 | 
             
                  end
         | 
| @@ -89,7 +96,7 @@ module Insque | |
| 89 96 | 
             
                end
         | 
| 90 97 | 
             
              end
         | 
| 91 98 |  | 
| 92 | 
            -
             | 
| 99 | 
            +
            private
         | 
| 93 100 | 
             
              def self.log message
         | 
| 94 101 | 
             
                print "#{Time.now.utc} #{message}\n"
         | 
| 95 102 | 
             
                STDOUT.flush if @debug
         |