labor 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Labor
2
2
 
3
- A wrapper for "gearman-ruby" which provides an easy mechanism for managing your jobs. Jobs are just Ruby classes/modules which respond to the `perform` method.
3
+ A wrapper for "gearman-ruby" which provides an easy mechanism for managing your jobs. Jobs are just Ruby classes which respond to the `perform` method.
4
4
 
5
5
  ## Install
6
6
 
@@ -13,16 +13,16 @@ Basic `Rakefile`:
13
13
  require 'labor'
14
14
  require 'labor/tasks'
15
15
 
16
- class TestJob
17
- def self.perform
16
+ class TestJob < Labor::Ability
17
+ def perform
18
18
  puts "Hello, world!"
19
19
 
20
20
  true
21
21
  end
22
22
  end
23
23
 
24
- class AnotherTest
25
- def self.perform
24
+ class AnotherTest < Labor::Ability
25
+ def perform
26
26
  puts "Hello, world... again!"
27
27
 
28
28
  true
@@ -36,7 +36,9 @@ Let's run the worker:
36
36
  $ export ABILITIES=test-job,another-test
37
37
  $ rake labor:work
38
38
 
39
- The `ABILITIES` environment variable can accept multiple job names; each seperated by a comma.
39
+ The `ABILITIES` environment variable can accept multiple job names; each seperated by a comma.
40
+
41
+ Job names can also carry meta data associated with that particular worker. Meta data is any information that shouldn't affect what you name the job class in your system, but indicate to Gearman that this worker is unique. You can do this by putting your meta data inside `[` and `]` in the ability name. For example: `email[123]`. Everything within and including the `[]` symbols will be removed when referencing your worker class' `perform` method.
40
42
 
41
43
  ## Configuration
42
44
 
@@ -61,8 +63,8 @@ The config file would then be loaded in like this:
61
63
 
62
64
  When the worker is started up, Labor will load this config into the `@config` instance variable of your job class/module. You'll then be able to retrieve keys from the config within your job.
63
65
 
64
- class TestJob
65
- def self.perform
66
+ class TestJob < Labor::Ability
67
+ def perform
66
68
  puts "Hey! I'm #{@config[:i_am]}"
67
69
  #=> "Hey! I'm at the bar"
68
70
 
@@ -5,6 +5,7 @@ require 'labor/version'
5
5
  require 'labor/core_ext'
6
6
  require 'labor/helpers'
7
7
  require 'labor/worker'
8
+ require 'labor/ability'
8
9
  require 'labor/config'
9
10
 
10
11
  module Labor
@@ -0,0 +1,12 @@
1
+ module Labor
2
+ class Ability
3
+ include Helpers
4
+
5
+ def initialize(payload, job)
6
+ puts "Payload: #{payload.inspect}"
7
+
8
+ @payload = payload.symbolize_keys!
9
+ @job = job
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class Hash
2
+ def symbolize_keys!
3
+ keys.each do |key|
4
+ self[(key.to_sym rescue key) || key] = delete(key)
5
+ end
6
+ values.each{|h| h.symbolize_keys! if h.is_a?(Hash) }
7
+ self
8
+ end
9
+ end
10
+
@@ -1,3 +1,3 @@
1
1
  module Labor
2
- VERSION = Version = "0.1.2"
2
+ VERSION = Version = "0.1.3"
3
3
  end
@@ -25,15 +25,19 @@ module Labor
25
25
  # Returns nothing.
26
26
  def add_abilities(abilities)
27
27
  abilities.each do |ability|
28
- klass = constantize(classify(ability))
29
- klass.instance_variable_set(:@config, Labor.config)
28
+ klass = constantize(classify(remove_meta_data(ability)))
29
+ config = Labor.config
30
30
 
31
31
  @worker.add_ability(ability) do |data, job|
32
32
  begin
33
33
  payload = JSON.parse data
34
- klass.perform(payload, job)
34
+ instance = klass.new(payload, job)
35
+ instance.instance_variable_set(:@config, config)
36
+ instance.perform
35
37
  rescue Exception => e
38
+ backtrace = Array(e.backtrace)[0..500]
36
39
  log "Job failed: #{e.inspect}"
40
+ log backtrace.join("\n")
37
41
  return false
38
42
  end
39
43
  end
@@ -44,7 +48,9 @@ module Labor
44
48
  payload = JSON.parse data
45
49
  klass.after_perform(payload, result)
46
50
  rescue Exception => e
51
+ backtrace = Array(e.backtrace)[0..500]
47
52
  log "After job failed: #{e.inspect}"
53
+ log backtrace.join("\n")
48
54
  end
49
55
  end
50
56
  end
@@ -66,5 +72,18 @@ module Labor
66
72
  end
67
73
  end
68
74
 
75
+ private
76
+
77
+ # Removes the meta data from an ability name.
78
+ #
79
+ # name - The String name of the ability.
80
+ #
81
+ # Returns the String name of the ability with the
82
+ # meta data removed. (e.g. "ability_name[123]" => "ability_name")
83
+ def remove_meta_data(name)
84
+ matches = name.match(/(.*)\[.*\]/)
85
+ return matches[1] unless matches.nil?
86
+ name
87
+ end
69
88
  end
70
89
  end
@@ -15,4 +15,11 @@ describe Labor::Worker do
15
15
  real_worker = @worker.instance_variable_get(:@worker)
16
16
  real_worker.instance_variable_get(:@abilities).should have_key("test")
17
17
  end
18
+
19
+ describe "meta data removal" do
20
+ it "can strip the metadata from a ability name" do
21
+ name = @worker.send(:remove_meta_data, "ability_name[123]")
22
+ name.should == "ability_name"
23
+ end
24
+ end
18
25
  end
metadata CHANGED
@@ -1,62 +1,53 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: labor
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 2
9
- version: 0.1.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Brett Buddin
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-03-29 00:00:00 -04:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-05-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: gearman-ruby
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2164345000 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
31
22
  type: :runtime
32
- version_requirements: *id001
33
- - !ruby/object:Gem::Dependency
34
- name: json
35
23
  prerelease: false
36
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *2164345000
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &2164344420 !ruby/object:Gem::Requirement
37
28
  none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
44
33
  type: :runtime
45
- version_requirements: *id002
46
- description: Wrapper for gearman-ruby that provides a different, and more portable, way of defining jobs.
34
+ prerelease: false
35
+ version_requirements: *2164344420
36
+ description: Wrapper for gearman-ruby that provides a different, and more portable,
37
+ way of defining jobs.
47
38
  email: brett@intraspirit.net
48
39
  executables: []
49
-
50
40
  extensions: []
51
-
52
- extra_rdoc_files:
41
+ extra_rdoc_files:
53
42
  - LICENSE
54
43
  - README.md
55
- files:
44
+ files:
56
45
  - LICENSE
57
46
  - README.md
58
47
  - Rakefile
48
+ - lib/labor/ability.rb
59
49
  - lib/labor/config.rb
50
+ - lib/labor/core_ext/hash.rb
60
51
  - lib/labor/core_ext.rb
61
52
  - lib/labor/helpers.rb
62
53
  - lib/labor/tasks.rb
@@ -68,39 +59,31 @@ files:
68
59
  - spec/labor_spec.rb
69
60
  - spec/spec_helper.rb
70
61
  - spec/worker_spec.rb
71
- has_rdoc: true
72
62
  homepage: http://github.com/brettbuddin/labor
73
63
  licenses: []
74
-
75
64
  post_install_message:
76
65
  rdoc_options: []
77
-
78
- require_paths:
66
+ require_paths:
79
67
  - lib
80
- required_ruby_version: !ruby/object:Gem::Requirement
68
+ required_ruby_version: !ruby/object:Gem::Requirement
81
69
  none: false
82
- requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- segments:
86
- - 0
87
- version: "0"
88
- required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
75
  none: false
90
- requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- segments:
94
- - 0
95
- version: "0"
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
96
80
  requirements: []
97
-
98
81
  rubyforge_project:
99
- rubygems_version: 1.3.7
82
+ rubygems_version: 1.7.2
100
83
  signing_key:
101
84
  specification_version: 3
102
85
  summary: More portable jobs for Gearman workers.
103
- test_files:
86
+ test_files:
104
87
  - spec/config_spec.rb
105
88
  - spec/files/sample_config.rb
106
89
  - spec/labor_spec.rb