sabbath 0.0.1 → 0.0.2

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.
@@ -6,17 +6,24 @@ REST interface to beanstalkd (or probably other queues at some point).
6
6
 
7
7
  >> sabbath -h
8
8
 
9
- << Usage: sabbath [options]
10
- <<
11
- << Options:
12
- << -p, --port[OPTIONAL] Port (default: 11300)
13
- << -h, --host[OPTIONAL] Host (default: localhost)
14
- << -P, --web-port[OPTIONAL] Web port (default: 4848)
15
- << -H, --web-host[OPTIONAL] Web host (default: 0.0.0.0)
16
- << --help Show this help message.
17
-
9
+ Usage: sabbath [options]
10
+
11
+ Options:
12
+ -p, --port[OPTIONAL] Port (default: 11300)
13
+ -h, --host[OPTIONAL] Host (default: localhost)
14
+ -P, --web-port[OPTIONAL] Web port (default: 4848)
15
+ -H, --web-host[OPTIONAL] Web host (default: 0.0.0.0)
16
+ -R, --rackup[OPTIONAL] Rackup file (optional)
17
+ --help Show this help message.
18
18
  == Why?
19
19
 
20
20
  This allows you to interface with beanstalk over normal HTTP calls. I've included a really simple example in the +examples+ directory. It consumes
21
21
  and pushes jobs onto a queue from jQuery.
22
22
 
23
+ This provides the following routes:
24
+
25
+ * /:tube GET -- This lets you get the latest job. If no job is available, the connection will be held open until one becomes available.
26
+ * /:tube/:id GET -- This lets you get a job by id.
27
+ * /:tube/:id/release PUT -- This lets you release a job back onto beanstalk.
28
+ * /:tube/:id DELETE -- This deletes a job from the queue.
29
+ * /:tube POST -- This lets you create a new job. The post parameter +body+ is expected.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -31,6 +31,10 @@ parser = OptionParser.new do |opts|
31
31
  options[:host] = v
32
32
  end
33
33
 
34
+ opts.on("-R[OPTIONAL]", "--rackup", "Rackup file (optional)") do |v|
35
+ options[:rackup] = v
36
+ end
37
+
34
38
  opts.on_tail("-h", "--help", "Show this help message.") { puts opts; exit }
35
39
  end
36
40
  parser.parse!(ARGV)
@@ -47,7 +47,7 @@
47
47
  $.ajax({
48
48
  type: "GET",
49
49
  dataType: "jsonp",
50
- url: base + "/default/" + id + "?_method=delete",
50
+ url: base + "/default/" + id + "?_method=DELETE",
51
51
 
52
52
  async: true, /* If set to non-async, browser shows page as "Loading.."*/
53
53
  cache: false,
@@ -77,7 +77,7 @@
77
77
  $.ajax({
78
78
  type: "GET",
79
79
  dataType: "jsonp",
80
- url: base + "/default/" + id + "?_method=delete",
80
+ url: base + "/default/" + id + "?_method=DELETE",
81
81
 
82
82
  async: true, /* If set to non-async, browser shows page as "Loading.."*/
83
83
  cache: false,
@@ -107,7 +107,7 @@
107
107
  $.ajax({
108
108
  type: "GET",
109
109
  dataType: "jsonp",
110
- url: base + "/default/" + id + "/release?_method=put",
110
+ url: base + "/default/" + id + "/release?_method=PUT",
111
111
 
112
112
  async: true, /* If set to non-async, browser shows page as "Loading.."*/
113
113
  cache: false,
@@ -135,7 +135,7 @@
135
135
  type: "GET",
136
136
  dataType: "jsonp",
137
137
  url: base + "/default",
138
- data: {body: $('#body').val(), _method: 'post'},
138
+ data: {body: $('#body').val(), _method: 'POST'},
139
139
  async: true, /* If set to non-async, browser shows page as "Loading.."*/
140
140
  cache: false,
141
141
  timeout: 50000, /* Timeout in ms */
@@ -19,7 +19,7 @@ class Sabbath
19
19
  end
20
20
 
21
21
  def start
22
- Server.new(@backend, options[:web_host], options[:web_port]).start
22
+ Server.new(@backend, options[:web_host], options[:web_port], options[:rackup]).start
23
23
  end
24
24
 
25
25
  end
@@ -4,7 +4,7 @@ module Thin
4
4
  def start
5
5
  raise ArgumentError, 'app required' unless @app
6
6
 
7
- log ">> Sabbath ---> connected to #{app.backend.name} on port #{app.backend.port}, host #{app.backend.host}"
7
+ log ">> Sabbath ---> connected to #{app.name} on port #{app.port}, host #{app.host}"
8
8
  log ">> Using Thin web server (v#{VERSION::STRING} codename #{VERSION::CODENAME})"
9
9
  debug ">> Debugging ON"
10
10
  trace ">> Tracing ON"
@@ -21,6 +21,30 @@ end
21
21
  class Sabbath
22
22
  class Server
23
23
 
24
+ class MethodOverride
25
+ HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
26
+
27
+ METHOD_OVERRIDE_PARAM_KEY = "_method".freeze
28
+ HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE".freeze
29
+
30
+ def initialize(app)
31
+ @app = app
32
+ end
33
+
34
+ def call(env)
35
+ request = Rack::Request.new(env)
36
+ query_params = Rack::Utils.parse_query(request.query_string)
37
+ method = query_params[METHOD_OVERRIDE_PARAM_KEY]
38
+ method = method.to_s.upcase
39
+ if HTTP_METHODS.include?(method)
40
+ env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
41
+ env["REQUEST_METHOD"] = method
42
+ end
43
+
44
+ @app.call(env)
45
+ end
46
+ end
47
+
24
48
  class DeferrableBody
25
49
  include EventMachine::Deferrable
26
50
 
@@ -53,11 +77,19 @@ class Sabbath
53
77
 
54
78
  AsyncResponse = [-1, {}, []].freeze
55
79
 
56
- attr_reader :backend, :web_host, :web_port, :cookie_name
80
+ class StatsProvider
81
+ attr_reader :name, :host, :port
82
+ def initialize(app, name, host, port)
83
+ @app, @name, @host, @port = app, name, host, port
84
+ end
85
+ def call(env); @app.call(env); end
86
+ end
57
87
 
58
- def initialize(backend, web_host, web_port, cookie_name = 'sabbath_id')
88
+ attr_reader :backend, :web_host, :web_port, :cookie_name, :rackup
89
+
90
+ def initialize(backend, web_host, web_port, rackup, cookie_name = 'sabbath_id')
59
91
  @backend = backend
60
- @web_host, @web_port, @cookie_name = web_host, web_port, cookie_name
92
+ @web_host, @web_port, @cookie_name, @rackup = web_host, web_port, cookie_name, rackup
61
93
  @router = Usher.new(:request_methods => [:request_method], :delimiters => ['/', '.'])
62
94
  @router.add_route('/:tube', :conditions => {:request_method => 'GET'}) .name(:get_latest_job)
63
95
  @router.add_route('/:tube/:job_id', :conditions => {:request_method => 'GET'}) .name(:get_job)
@@ -66,13 +98,21 @@ class Sabbath
66
98
  @router.add_route('/:tube/:job_id/release', :conditions => {:request_method => 'PUT'}) .name(:release_job)
67
99
  end
68
100
 
101
+ def builder
102
+ builder = Rack::Builder.new
103
+ builder.use(StatsProvider, backend.name, backend.host, backend.port)
104
+ builder.instance_eval(File.read(rackup)) if rackup
105
+ builder.use(MethodOverride)
106
+ builder.run(self)
107
+ builder
108
+ end
109
+
69
110
  def call(env)
111
+ p env
70
112
  request = Rack::Request.new(env)
71
113
  query_params = Rack::Utils.parse_query(request.query_string)
72
- env['REQUEST_METHOD'] = query_params['_method'].upcase if query_params['_method']
73
114
 
74
115
  id = request.cookies[cookie_name] || UUID.new.generate
75
- p id
76
116
  common_response_headers = {'Content-Type' => 'text/javascript'}
77
117
 
78
118
  common_response_headers['Set-cookie'] = Rack::Utils.build_query(cookie_name => id) unless request.cookies[cookie_name]
@@ -85,16 +125,15 @@ class Sabbath
85
125
  when nil
86
126
  env['async.callback'].call([404, {}, []])
87
127
  else
88
- params = response.params_as_hash
128
+ params = Hash[response.params]
129
+ p response.path.route.named
130
+ p params
89
131
  case response.path.route.named
90
132
  when :get_latest_job
91
- puts "latest job..."
92
133
  env['async.callback'].call([200, common_response_headers, body])
93
134
  backend.get_latest_job(id, params[:tube], params['timeout']) {|job|
94
- puts "sending job.body: #{job.body}"
95
135
  body.succeed_with(:id => job.id, :body => job.body)
96
136
  }.on_error {|message|
97
- puts "message.. #{message}"
98
137
  body.succeed_with(:error => message)
99
138
  }
100
139
  when :get_job
@@ -132,9 +171,8 @@ class Sabbath
132
171
  end
133
172
 
134
173
  def start
135
- @server = self
136
174
  EM.run do
137
- Thin::Server.start(web_host, web_port, self)
175
+ Thin::Server.start(web_host, web_port, builder.to_app)
138
176
  end
139
177
  end
140
178
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sabbath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull