resque-data 0.0.1

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.
@@ -0,0 +1 @@
1
+ /pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resque-data.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ coderay (1.0.7)
5
+ interactive_editor (0.0.10)
6
+ spoon (>= 0.0.1)
7
+ method_source (0.8)
8
+ pry (0.9.10)
9
+ coderay (~> 1.0.5)
10
+ method_source (~> 0.8)
11
+ slop (~> 3.3.1)
12
+ rack (1.4.1)
13
+ rack-cors (0.2.7)
14
+ rack
15
+ rack-protection (1.2.0)
16
+ rack
17
+ redis (3.0.2)
18
+ redis-namespace (1.2.1)
19
+ redis (~> 3.0.0)
20
+ sinatra (1.3.3)
21
+ rack (~> 1.3, >= 1.3.6)
22
+ rack-protection (~> 1.2)
23
+ tilt (~> 1.3, >= 1.3.3)
24
+ slop (3.3.3)
25
+ spoon (0.0.1)
26
+ tilt (1.3.3)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ interactive_editor
33
+ pry
34
+ rack-cors
35
+ redis-namespace
36
+ sinatra
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 j-wilkins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # Resque::Data
2
+
3
+ Resque Data is a small Sinatra based Rack app that will fetch basic information
4
+ about a Resque instance.
5
+
6
+ It has no security, and very little polish. Use at your own risk.
7
+
8
+ That said, I'm using it to serve up Resque stats for internal apps, and it
9
+ seems to work ok.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'resque-data'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install resque-data
24
+
25
+ ## Usage
26
+
27
+ The easiest way to use `Resque::Data` is going to be mounting it as an engine.
28
+
29
+ ```
30
+ mount Resque::Data::Server, at: 'resque_data'
31
+ ```
32
+
33
+ I'm hoping to get around to writing a `resque_data` executable, but until
34
+ then there's a `config.ru` sitting the the `lib` folder that you can use.
35
+
36
+ ### Configuration
37
+
38
+ You have a few configuration options off of `Resque::Data::Config`:
39
+
40
+ * `default_redis` - set the redis to be used when none is specified.
41
+ * `multi_namespace` - specify whether to accept a namespace parameter. Default: true
42
+ * `multi_redis` - specifiy whether to accept a redis parameter. Default: true
43
+
44
+ Currently, you're going to get weird results if you allow `multi_redis` without
45
+ allowing `multi_namespace`, but I'm not really sure why you'd want to do that.
46
+
47
+ You set these options like:
48
+
49
+ ```
50
+ Resque::Data::Config.<option> = <whatever>
51
+ ```
52
+
53
+ Also, the configuration is currently persisted when `Resque::Data::Server` is
54
+ loaded, this is something to do with it being in a Sinatra `configuration`
55
+ block. However, if all you do is configure it in an initializer then mount it
56
+ you should never notice this.
57
+
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,13 @@
1
+ require 'resque-data/version'
2
+ require "resque-data/config"
3
+
4
+ require "resque-data/fetcher"
5
+ require "resque-data/manager"
6
+
7
+ module Resque
8
+ module Data
9
+
10
+ autoload :Server, 'resque-data/server'
11
+
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+
2
+ module Resque
3
+ module Data
4
+
5
+ class Config
6
+ def self.default_redis=(arg)
7
+ Thread.current[:redis_data_default_redis] = arg
8
+ end
9
+
10
+ def self.default_redis
11
+ Thread.current[:redis_data_default_redis].tap do |ans|
12
+ return nil if ans.nil?
13
+ end
14
+ end
15
+
16
+ def self.multi_namespace=(true_or_false)
17
+ Thread.current[:redis_data_multi_namespace] = !!true_or_false
18
+ end
19
+
20
+ def self.multi_namespace
21
+ Thread.current[:redis_data_multi_namespace].tap do |ans|
22
+ return true if ans.nil?
23
+ end
24
+ end
25
+
26
+ def self.multi_redis=(true_or_false)
27
+ Thread.current[:redis_data_multi_redis] = !!true_or_false
28
+ end
29
+
30
+ def self.multi_redis
31
+ Thread.current[:redis_data_multi_redis].tap do |ans|
32
+ return true if ans.nil?
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ require 'sinatra'
2
+ require 'rack/cors'
3
+
4
+ require 'resque-data'
5
+
6
+ #Resque::Data::Config.default_redis = "localhost:6379/"
7
+ #Resque::Data::Config.multi_namespace = false
8
+ #Resque::Data::Config.multi_redis = false
9
+
10
+ use Rack::Cors do |config|
11
+ config.allow do |allow|
12
+ allow.origins '*'
13
+ allow.resource '/', :headers => :any
14
+ end
15
+ end
16
+
17
+ run Resque::Data::Server
@@ -0,0 +1,83 @@
1
+
2
+ class Resque::Data::Fetcher
3
+
4
+ def initialize(params)
5
+ @multi_namespace = params.delete(:multi_namespace)
6
+ @multi_namespace = true if @multi_namespace.nil?
7
+
8
+ (params[:redis] || 'redis://localhost:6379').tap do |r|
9
+ @connection, @default_namespace = parse_redis(r)
10
+ end
11
+
12
+ @default_namespace ||= params[:namespace] || :resque
13
+ end
14
+
15
+ def fetch
16
+ fetch_for(@default_namespace)
17
+ end
18
+
19
+ def fetch_for(ns)
20
+ ns = @default_namespace unless @multi_namespace
21
+
22
+ using_namespace(ns) do
23
+ queue_counts.merge(failed_count).merge(worker_counts)
24
+ end
25
+ end
26
+
27
+ def queue_counts
28
+ {queues: redis.smembers(:queues).map {|q|
29
+ {queue: q, count: redis.llen("queue:#{q}")}
30
+ }}
31
+ end
32
+
33
+ def failed_count
34
+ {failed: redis.llen(:failed)}
35
+ end
36
+
37
+ def worker_counts
38
+ workers = redis.smembers(:workers)
39
+ working = 0
40
+ workers.each {|w| working += 1 if redis.exists("worker:#{w}")}
41
+ {working: working, workers: workers.length}
42
+ end
43
+
44
+ def worker_stats
45
+ Hash[redis.smembers(:workers).map do |w|
46
+ [w, redis.exists("worker:#{w}") ? :working : :idle]
47
+ end]
48
+ end
49
+
50
+ def using_namespace(ns)
51
+ redis.namespace = ns
52
+ ret = yield
53
+ ensure
54
+ redis.namespace = @default_namespace
55
+ ret
56
+ end
57
+
58
+ def redis
59
+ @redis ||= Redis::Namespace.new(@default_namespace, redis: @connection)
60
+ end
61
+
62
+ private
63
+
64
+ def parse_redis(server)
65
+ case server
66
+ when String
67
+ if server['redis://']
68
+ redis = Redis.new(:url => server, :thread_safe => true)
69
+ else
70
+ server, namespace = server.split('/', 2)
71
+ host, port, db = server.split(':')
72
+ redis = Redis.new(:host => host, :port => port,
73
+ :thread_safe => true, :db => db)
74
+ end
75
+ [redis, namespace]
76
+ when Redis::Namespace
77
+ [server.redis, server.namespace]
78
+ else
79
+ [server, nil]
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,47 @@
1
+ class Resque::Data::RedisManager
2
+
3
+ def initialize(default_redis = nil, multi_namespace = true, multi_redis = true, redi = [])
4
+ @default_redis = default_redis || 'redis://localhost:6379'
5
+
6
+ @multi_namespace, @multi_redis = multi_namespace, multi_redis
7
+
8
+ @redi = Hash[redi.map {|r| [r, new_redis(r)]}]
9
+ @redi[@default_redis] = new_redis(@default_redis) if @redi[@default_redis].nil?
10
+
11
+ @timestamps = Hash.new
12
+ end
13
+
14
+ def for(host = @default_redis)
15
+ return @redi[@default_redis] unless @multi_redis
16
+
17
+ (@redi[host] ||= new_redis(host)).tap do
18
+ @timestamps[host] = Time.now
19
+ end
20
+ end
21
+
22
+ def new_redis(host)
23
+ prune if needs_pruning?
24
+
25
+ Resque::Data::Fetcher.new(redis: host, multi_namespace: @multi_namespace)
26
+ end
27
+
28
+ def needs_pruning?
29
+ @redi.length >= 20
30
+ end
31
+
32
+ def prune
33
+ sorted = @timestamps.sort_by {|host, time| time}
34
+ sorted.pop if sorted.first.first == @default_redis
35
+
36
+ oldest = sorted.first.first
37
+ puts "deleting #{oldest}"
38
+
39
+ @timestamps.delete(oldest)
40
+ @redi.delete(oldest)
41
+ end
42
+
43
+ def to_json
44
+ @redi.to_json
45
+ end
46
+
47
+ end
@@ -0,0 +1,31 @@
1
+ require 'sinatra'
2
+ require 'json'
3
+ require 'redis/namespace'
4
+
5
+ module Resque
6
+ module Data
7
+ class Server < Sinatra::Application
8
+
9
+ configure do
10
+ $redis ||= RedisManager.new(Resque::Data::Config.default_redis,
11
+ Resque::Data::Config.multi_namespace,
12
+ Resque::Data::Config.multi_redis)
13
+ end
14
+
15
+ before do
16
+ content_type 'application/json'
17
+
18
+ end
19
+
20
+ get '/' do
21
+ begin
22
+ $redis.for(params['redis']).fetch_for(params['namespace']).to_json
23
+ rescue Redis::CannotConnectError => boom
24
+ status 404
25
+ {message: 'The passed redis url was invalid'}.to_json
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ module Resque
2
+ module Data
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'resque-data/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "resque-data"
8
+ gem.version = Resque::Data::VERSION
9
+ gem.authors = ["j-wilkins"]
10
+ gem.email = ["pablo_honey@me.com"]
11
+ gem.description = %q{Make Resque data accessible via HTTP.}
12
+ gem.summary = %q{Make Resque data accessible via HTTP.}
13
+ gem.homepage = "https://github.com/j-wilkins/resque-data"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "sinatra"
21
+ gem.add_dependency "rack-cors"
22
+ gem.add_dependency "redis-namespace"
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - j-wilkins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rack-cors
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: redis-namespace
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Make Resque data accessible via HTTP.
63
+ email:
64
+ - pablo_honey@me.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/resque-data.rb
76
+ - lib/resque-data/config.rb
77
+ - lib/resque-data/config.ru
78
+ - lib/resque-data/fetcher.rb
79
+ - lib/resque-data/manager.rb
80
+ - lib/resque-data/server.rb
81
+ - lib/resque-data/version.rb
82
+ - resque-data.gemspec
83
+ homepage: https://github.com/j-wilkins/resque-data
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.24
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Make Resque data accessible via HTTP.
107
+ test_files: []