perform_later 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,9 +5,71 @@ Perform later is a gem meant to work with the [Resque](http://github.com/defunkt
5
5
 
6
6
  The gem handles queuing tasks without the need to have additional "Worker" classes or with any changes to your original model/object code base.
7
7
 
8
+ The gem really simplifies queuing tasks, no need for extra workers and you may never need to change your code.
9
+
10
+ The gem will "translate" objects to a serializable (suitable for json) versions of those classes.
11
+
12
+ ### Usage
13
+ You can call the `perform_later` method on any object or active record instance
14
+
15
+ the `perform_later` method has 3 params
16
+
17
+ ```ruby
18
+ :queue_name #The queue in which this task will be in
19
+ :method_name #the method that will be called on the object
20
+ args #array of arguments
21
+ ```
22
+
23
+ ### Samples
24
+
25
+ ```ruby
26
+ @user = User.find(1)
27
+ @user.perform_later(:queue_name, :method_name, args)
28
+ ```
29
+
30
+ You can also call objects on the User object itself
31
+
32
+ ```ruby
33
+ User.perform_later(:queue_name, :method_name, args)
34
+ ```
35
+
36
+ ## Configuration
37
+ perform_later has a single configuration file, you should put the file in `config/resque_perform_later.yml`
38
+
39
+ ```ruby
40
+ defaults: &DEFAULTS
41
+ enabled: true
42
+
43
+ development:
44
+ <<: *DEFAULTS
45
+
46
+ test:
47
+ enabled: false
48
+
49
+ production:
50
+ <<: *DEFAULTS
51
+
52
+ staging:
53
+ <<: *DEFAULTS
54
+ ```
55
+
56
+ ### Configuration explained
57
+ The config is simple, either it's enabled or it's not, and you can set the environment which you want to enable in
58
+
59
+ ## What happens in test when it's disabled
60
+ In test mode, the method is not queued, it's being sent immediately on the object, this way your test work completely normal and you don't need to worry about Resque or Redis in your tests, this is very useful
61
+
62
+
8
63
  ## Contribute / Bug reports
9
64
  If you have an issue with this gem, please open an issue in the main repo, it will help tons if you could supply a failing spec with that, so I can better track where the bug is coming from, if not, no worries, just report I will do my best to address it as fast and efficient as I can.
10
65
 
11
66
  If you want to contribute (awesome), open a feature branch, base it on master.
12
67
 
13
- Be as descriptive as you can in the pull request description, just to be clear what problem you are solving or what feature are you adding.
68
+ Be as descriptive as you can in the pull request description, just to be clear what problem you are solving or what feature are you adding.
69
+
70
+ ## TODO
71
+ 1. Add the ability to perform_later without a queue provided (will go to a default queue - configurable)
72
+ 2. Add generator for the configuration file
73
+
74
+ ## Ideas
75
+ 1. Add the ability that a method will be tagged as "perform_later", this way you can call the method by name and it will be queued
@@ -4,14 +4,14 @@ module ActiveRecordPerformLater
4
4
  if ResquePerformLater.config['enabled']
5
5
  args = ResquePerformLater.args_to_resque(args)
6
6
 
7
- Resque::Job.create( queue,
8
- ActiveRecordWorker,
9
- self.class.name,
10
- self.id,
11
- method,
12
- *args)
7
+ Resque::Job.create(queue,
8
+ ActiveRecordWorker,
9
+ self.class.name,
10
+ self.id,
11
+ method,
12
+ *args)
13
13
  else
14
- self.send( method, *args )
14
+ self.send(method, *args)
15
15
  end
16
16
  end
17
17
  end
@@ -1,4 +1,10 @@
1
1
  class ActiveRecordWorker
2
+ # Public: perform.
3
+ #
4
+ # klass_name - name of the class (string).
5
+ # method - method name, this method will be called on the object.
6
+ # *args - array of arguments to send to the method
7
+ #
2
8
  def self.perform(klass, id, method, *args)
3
9
  args = ResquePerformLater.args_from_resque(args)
4
10
  runner_klass = eval(klass)
data/lib/object_worker.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  class ObjectWorker
2
- # @param klass_name [The class name that you should work on]
3
- # @param method [method you should send on the class]
4
- # @param args [method args]
2
+ # Public: perform.
3
+ #
4
+ # klass_name - name of the class (string).
5
+ # method - method name, this method will be called on the object.
6
+ # *args - array of arguments to send to the method
7
+ #
5
8
  def self.perform(klass_name, method, *args)
6
9
  args = ResquePerformLater.args_from_resque(args)
7
10
 
@@ -1,3 +1,3 @@
1
1
  module PerformLater
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -6,7 +6,6 @@ class ResquePerformLater
6
6
  }
7
7
 
8
8
  # inspired by DelayedJob
9
-
10
9
  CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
11
10
  AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/
12
11
  YAML_STRING_FORMAT = /\A---/
@@ -58,9 +57,8 @@ class ResquePerformLater
58
57
  if File.exists?(DEFAULT_CONFIG_PATH)
59
58
  config = YAML.load(ERB.new(IO.read(DEFAULT_CONFIG_PATH)).result)[self.env_str]
60
59
  end
61
-
60
+
62
61
  config = {}.merge(DEFAULT_CONFIG || {}).merge(config || {})
63
-
64
62
  @@_config = config
65
63
  end
66
64
  end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'resque'
3
+
4
+ describe ActiveRecordPerformLater do
5
+ let(:user) { User.create }
6
+
7
+ let(:enabled) do
8
+ { 'enabled' => true }
9
+ end
10
+ let(:disabled) do
11
+ { 'enabled' => false }
12
+ end
13
+
14
+ it "should insert a task into resque when the config is enabled" do
15
+ Resque.redis = $redis
16
+
17
+ ResquePerformLater.stub!(:config).and_return(enabled)
18
+ user.perform_later(:generic, :full_name)
19
+
20
+ Resque.peek(:generic, 0, 20).length.should == 1
21
+ end
22
+
23
+ it "should send the method on the class when the config is disabled" do
24
+ ResquePerformLater.stub!(:config).and_return(disabled)
25
+ user.should_receive(:full_name)
26
+ user.perform_later(:generic, :full_name)
27
+ end
28
+
29
+
30
+ end
@@ -1,3 +1,5 @@
1
1
  class User < ActiveRecord::Base
2
-
2
+ def full_name
3
+ "Avi Tzurel"
4
+ end
3
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perform_later
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-19 00:00:00.000000000 Z
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70120073048240 !ruby/object:Gem::Requirement
16
+ requirement: &70289648690300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70120073048240
24
+ version_requirements: *70289648690300
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: redis
27
- requirement: &70120073047640 !ruby/object:Gem::Requirement
27
+ requirement: &70289648689880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70120073047640
35
+ version_requirements: *70289648689880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: resque
38
- requirement: &70120073046940 !ruby/object:Gem::Requirement
38
+ requirement: &70289648689400 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70120073046940
46
+ version_requirements: *70289648689400
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70120073046500 !ruby/object:Gem::Requirement
49
+ requirement: &70289648688780 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70120073046500
57
+ version_requirements: *70289648688780
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &70120073045940 !ruby/object:Gem::Requirement
60
+ requirement: &70289648688280 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70120073045940
68
+ version_requirements: *70289648688280
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: sqlite3
71
- requirement: &70120073045260 !ruby/object:Gem::Requirement
71
+ requirement: &70289648687300 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70120073045260
79
+ version_requirements: *70289648687300
80
80
  description: Queue any method in any class or instance with no need for additional
81
81
  Worker class and no extra code
82
82
  email:
@@ -100,6 +100,7 @@ files:
100
100
  - lib/resque_perform_later.rb
101
101
  - license
102
102
  - perform_later.gemspec
103
+ - spec/lib/active_record_perform_later_spec.rb
103
104
  - spec/lib/resque_perform_later_spec.rb
104
105
  - spec/spec_helper.rb
105
106
  - spec/support/database_connection.rb
@@ -132,6 +133,7 @@ specification_version: 3
132
133
  summary: Queue any method in any class or instance with no need for additional Worker
133
134
  class and no extra code
134
135
  test_files:
136
+ - spec/lib/active_record_perform_later_spec.rb
135
137
  - spec/lib/resque_perform_later_spec.rb
136
138
  - spec/spec_helper.rb
137
139
  - spec/support/database_connection.rb