komrade-client 1.0.6 → 1.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,10 @@
1
1
  require 'uri'
2
+ require 'thread'
2
3
 
3
4
  module Komrade
4
5
  extend self
5
6
  Error = Class.new(StandardError)
7
+ @outLocker = Mutex.new
6
8
 
7
9
  def env(key)
8
10
  ENV[key]
@@ -25,16 +27,25 @@ module Komrade
25
27
  if block_given?
26
28
  start = Time.now
27
29
  result = yield
28
- data.merge!(val: (Time.now - start))
30
+ data.merge!(:val => (Time.now - start))
29
31
  end
30
32
  data.reduce(out=String.new) do |s, tup|
31
33
  s << [tup.first, tup.last].join("=") << " "
32
34
  end
33
- $stdout.puts(out)
35
+ @outLocker.synchronize do
36
+ $stdout.puts(out)
37
+ end
34
38
  return result
35
39
  end
36
40
 
37
41
  end
38
42
 
39
43
  require 'komrade-client/queue'
40
- require 'komrade-client/railtie' if defined?(Rails)
44
+
45
+ if defined?(Rails)
46
+ require 'rails/railtie'
47
+ require 'komrade-client/railtie'
48
+
49
+ require 'rails/generators/base'
50
+ require 'komrade-client/komrade_generator'
51
+ end
@@ -70,7 +70,7 @@ module Komrade
70
70
  Net::HTTP.new(Komrade.url.host, Komrade.url.port).tap do |h|
71
71
  if Komrade.url.scheme == 'https'
72
72
  h.use_ssl = true
73
- h.verify_mode = OpenSSL::SSL::VERIFY_NONE
73
+ h.verify_mode = OpenSSL::SSL::VERIFY_PEER
74
74
  end
75
75
  end
76
76
  end
@@ -0,0 +1,9 @@
1
+ class KomradeGenerator < Rails::Generators::Base
2
+ desc "This generator adds a komrade-worker process to your Procfile"
3
+ def append_procfile
4
+ File.open("Procfile", 'ab') do |file|
5
+ file.write("komrade-worker: bundle exec rake komrade:work")
6
+ end
7
+ puts "A worker process has been added to your Procfile you will be billed accordingly."
8
+ end
9
+ end
@@ -12,7 +12,7 @@ module Komrade
12
12
  # Fast operation.
13
13
  def enqueue(method, *args)
14
14
  SecureRandom.uuid.tap do |id|
15
- log(:at => "enqueue-job", :job => id, :method => method) do
15
+ log(:at => "enqueue-job", :id => id, :method => method) do
16
16
  put("/jobs/#{id}", method: method, args: args)
17
17
  end
18
18
  end
@@ -23,19 +23,21 @@ module Komrade
23
23
  # If you dequeue a job, it is your responsiblity to update the job.
24
24
  # Updates to jobs include: heartbeat, fail, and delete.
25
25
  #
26
+ # There is no logging for dequeue since this method will be called
27
+ # frequently. Instead, logging should happen in the caller of
28
+ # dequeue to inform the stream that a job was locked.
29
+ #
26
30
  # Moderately fast operation.
27
31
  def dequeue(opts={})
28
32
  limit = opts[:limit] || 1
29
- log(:at => "dequeue-job", :limit => limit) do
30
- get("/jobs?limit=#{limit}")
31
- end
33
+ get("/jobs?limit=#{limit}")
32
34
  end
33
35
 
34
36
  # Idempotent call to delete a job from the queue.
35
37
  #
36
38
  # Fast operation.
37
39
  def remove(id)
38
- log(:at => "remove-job", :job => id) do
40
+ log(:at => "remove-job", :id => id) do
39
41
  delete("/jobs/#{id}")
40
42
  end
41
43
  end
@@ -45,7 +47,7 @@ module Komrade
45
47
  #
46
48
  # Slow operation.
47
49
  def delete_all
48
- log(:at => "cleat") do
50
+ log(:at => "delete-all") do
49
51
  post("/delete-all-jobs")
50
52
  end
51
53
  end
@@ -25,23 +25,33 @@ module Komrade
25
25
  # This method will lock a job & evaluate the code defined by the job.
26
26
  # Also, this method will make the best attempt to delete the job
27
27
  # from the queue before returning.
28
+ #
29
+ # Before the worker evaluates the code extracted from the job,
30
+ # it spawns a thread which will send heartbeats to komrade. This
31
+ # indicates to the back end that the job is being processed. If heartbeats
32
+ # stop coming in for a job, komrade may thing that the job is lost and
33
+ # subsequently release the lock and place it back in the queue.
28
34
  def work
29
35
  jobs = Queue.dequeue
30
36
  until jobs.empty?
31
37
  job = jobs.pop
32
38
  begin
33
- @finished, @beats = false, 0
34
- Thread.new do
35
- while @beats == 0 || !@finished
36
- @beats += 1
37
- HttpHelpers.post("/jobs/#{job['id']}/heartbeats")
38
- sleep(1)
39
+ log(:at => "work-job", :id => job['id']) do
40
+ @finished, @beats = false, 0
41
+ Thread.new do
42
+ while @beats == 0 || !@finished
43
+ @beats += 1
44
+ log(:at => "heartbeat-job", :id => job['id'])
45
+ HttpHelpers.post("/jobs/#{job['id']}/heartbeats")
46
+ sleep(1)
47
+ end
39
48
  end
49
+ call(job["payload"])
50
+ @finished = true
40
51
  end
41
- call(job["payload"])
42
- @finished = true
43
52
  rescue => e
44
53
  handle_failure(job, e)
54
+ raise(e)
45
55
  ensure
46
56
  Queue.remove(job["id"])
47
57
  end
@@ -62,13 +72,13 @@ module Komrade
62
72
  # is raised during the execution of the job.
63
73
  def handle_failure(job,e)
64
74
  fid = SecureRandom.uuid
65
- log(:at => "handle-failure", :failure_id => fid)
75
+ log(:at => "handle-failure", :id => job['id'], 'failure-id' => fid)
66
76
  b = {error: e.class, message: e.message}
67
77
  HttpHelpers.put("/jobs/#{job['id']}/failures/#{fid}", b)
68
78
  end
69
79
 
70
- def log(data)
71
- Komrade.log(data)
80
+ def log(data, &blk)
81
+ Komrade.log(data, &blk)
72
82
  end
73
83
 
74
84
  end
data/readme.md CHANGED
@@ -14,30 +14,46 @@ $ gem install komrade-client
14
14
  ## Usage
15
15
 
16
16
  1. Install Gem
17
- 2. Enqueue
18
- 3. Dequeue
17
+ 2. Minimalist Example
18
+ 3. Rails Example
19
19
  4. Komrade Dashboard
20
20
 
21
- ### Install
21
+ ### Install Gem
22
22
 
23
23
  Gemfile
24
24
 
25
25
  ```ruby
26
26
  source :rubygems
27
- gem 'komrade-client', '1.0.6'
27
+ gem 'komrade-client', '~> 1.0.15'
28
28
  ```
29
29
 
30
- ### Enqueue
30
+ ### Minimalist Example ###
31
31
 
32
- Simple Example
32
+ This is the absolute bare minimum to see Komrade in action.
33
33
 
34
34
  ```bash
35
- $ export KOMRADE_URL=https://u:p@service.komrade.io
35
+ $ export KOMRADE_URL=https://{heroku_username}:{heroku_password}@service.komrade.io
36
36
  $ ruby -r komrade-client -e 'Komrade::Queue.enqueue("puts", "hello world")'
37
37
  $ ruby -r komrade-client -e 'puts Komrade::Queue.dequeue'
38
38
  ```
39
39
 
40
- Example Model
40
+ You should see "hello world" output in your terminal.
41
+
42
+ ### Rails Example ###
43
+
44
+ To get started add `gem 'komrade-client', '~> 1.0.15'` to your Gemfile. Then run
45
+ `rails g komrade`. This will add a komrade-worker process to your Procfile (feel
46
+ free to edit your Procfile by hand if you prefer).
47
+
48
+ Your Procfile now should look something like this:
49
+ ```
50
+ web: bundle exec rails s
51
+ komrade-worker: bundle exec rake komrade:work
52
+ ```
53
+
54
+ This is an example of a Rails model that sends a welcome email upon user sign up.
55
+ The only code that is unique to Komrade here is the `Komrade::Queue.enqueue` method.
56
+ This method takes a method as a string and any parameters you want to pass to that method.
41
57
 
42
58
  ```ruby
43
59
 
@@ -56,14 +72,9 @@ class User < ActiveRecord::Base
56
72
  end
57
73
  ```
58
74
 
59
- ### Dequeue
60
-
61
- Procfile
75
+ When you deploy your code, the will queue be ready to accept jobs, and the worker process
76
+ is waiting to do the work.
62
77
 
63
- ```
64
- web: bundle exec rails s
65
- worker: bundle exec rake komrade:work
66
- ```
67
78
 
68
79
  ### Komrade Dashboard
69
80
 
@@ -72,4 +83,3 @@ $ heroku addons:open komrade:test
72
83
  ```
73
84
 
74
85
  ![img](http://f.cl.ly/items/0G3f0B2J3J40451h0k3I/Screen%20Shot%202013-01-27%20at%2010.41.53%20PM.png)
75
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: komrade-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,13 +11,14 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-01-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: A client library for the komrad worker queue.
14
+ description: A client library for the komrade worker queue.
15
15
  email: komrade@32k.io
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/komrade-client/http_helpers.rb
21
+ - lib/komrade-client/komrade_generator.rb
21
22
  - lib/komrade-client/queue.rb
22
23
  - lib/komrade-client/railtie.rb
23
24
  - lib/komrade-client/rate_limiter.rb