delayed-method 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -36,9 +36,16 @@ model.long_call(arg1, arg2)
36
36
 
37
37
  DelayedMethod.enqueue(model, :long_call, arg1, arg2)
38
38
 
39
+ That's it. This is the only 2 cases that DelayedMethod will work: Caller
40
+ need to be either a class or a persisted ActiveRecord model
39
41
 
40
- That's it. This is the only 2 cases that DelayedMethod will work.
42
+ If you use resque-scheduler, you can also do this:
41
43
 
44
+ DelayedMethod.enqueue_at(1.day.from_now, Executor, :long_call, arg1, arg2)
45
+
46
+ or
47
+
48
+ DelayedMethod.enqueue_at(1.day.from_now, model, :long_call, arg1, arg2)
42
49
 
43
50
  Warning
44
51
  ===========
@@ -16,11 +16,29 @@ class DelayedMethod
16
16
  end
17
17
 
18
18
  def enqueue(object, method, *args)
19
+ ensure_proper_call(object, method) do |klass, id|
20
+ Resque.enqueue(DelayedMethod, klass, id, method, *args)
21
+ end
22
+ end
23
+
24
+ def enqueue_at(time, object, method, *args)
25
+ ensure_proper_call(object, method) do |klass, id|
26
+ if Resque.respond_to?(:enqueue_at)
27
+ Resque.enqueue_at(time, DelayedMethod, klass, id, method, *args)
28
+ else
29
+ raise "resque-scheduler need to be included for this to work"
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+ def ensure_proper_call(object, method)
19
36
  raise ArgumentError.new("object does not respond to #{method}") unless object.respond_to?(method)
20
37
  if object.is_a? Class
21
- Resque.enqueue(DelayedMethod, object.name, nil, method, *args)
38
+ yield object.name, nil
22
39
  elsif object.is_a? ActiveRecord::Base
23
- Resque.enqueue(DelayedMethod, object.class.name, object.id, method, *args)
40
+ raise ArgumentError.new("object need to be persisted") unless object.persisted?
41
+ yield object.class.name, object.id
24
42
  else
25
43
  raise ArgumentError.new("Only class and ActiveRecord resource are supported")
26
44
  end
@@ -28,7 +28,7 @@ describe DelayedMethod do
28
28
  end
29
29
 
30
30
  def mock_armodel
31
- mock(ActiveRecord::Base, { :id => 1, :execute => true }).tap do |armodel|
31
+ mock(ActiveRecord::Base, { :id => 1, :execute => true, :persisted? => true }).tap do |armodel|
32
32
  armodel.should_receive(:is_a?).with(Class).and_return(false)
33
33
  armodel.should_receive(:is_a?).with(ActiveRecord::Base).and_return(true)
34
34
  armodel.should_receive(:class).and_return ActiveRecord::Base
@@ -3,3 +3,7 @@ require 'bundler'
3
3
  require 'rspec'
4
4
  require 'delayed-method'
5
5
  require 'debugger'
6
+
7
+ Dir[File.expand_path("../support/*.rb", __FILE__)].each do |file|
8
+ require file
9
+ end
@@ -0,0 +1,70 @@
1
+ TMP_PATH = File.expand_path("../../../tmp", __FILE__)
2
+ PIDS_PATH = TMP_PATH + "/pids"
3
+ REDIS_PID = "#{PIDS_PATH}/redis-test.pid"
4
+ REDIS_CACHE_PATH = "#{TMP_PATH}/cache/"
5
+
6
+ module RedisHelper
7
+ class << self
8
+ def start_server
9
+ FileUtils.mkdir_p REDIS_CACHE_PATH
10
+ FileUtils.mkdir_p PIDS_PATH
11
+
12
+ redis_options = {
13
+ "daemonize" => 'yes',
14
+ "pidfile" => REDIS_PID,
15
+ "port" => 9736,
16
+ "timeout" => 300,
17
+ "save 900" => 1,
18
+ "save 300" => 1,
19
+ "save 60" => 10000,
20
+ "dbfilename" => "dump.rdb",
21
+ "dir" => REDIS_CACHE_PATH,
22
+ "loglevel" => "debug",
23
+ "logfile" => "stdout",
24
+ "databases" => 16
25
+ }.map { |k, v| "#{k} #{v}" }.join('\n')
26
+ `echo '#{redis_options}' | redis-server -`
27
+ success = false
28
+
29
+ wait_time_remaining = 5
30
+ begin
31
+ TCPSocket.open("localhost", "9736")
32
+ success = true
33
+ rescue Exception => e
34
+ if wait_time_remaining > 0
35
+ wait_time_remaining -= 0.1
36
+ sleep 0.1
37
+ else
38
+ raise "Cannot start redis server in 5 seconds. Pinging server yield\n#{e.inspect}"
39
+ end
40
+ end while(!success)
41
+
42
+ Resque.redis = "localhost:9736"
43
+ end
44
+
45
+ def stop_server
46
+ %x{
47
+ cat #{REDIS_PID} | xargs kill -QUIT
48
+ rm -f #{REDIS_CACHE_PATH}dump.rdb
49
+ }
50
+ end
51
+
52
+ def clear
53
+ Resque.redis.flushdb
54
+ end
55
+ end
56
+ end
57
+
58
+ RSpec.configure do |config|
59
+ config.before(:each) do
60
+ RedisHelper.clear
61
+ end
62
+
63
+ config.before(:suite) do
64
+ RedisHelper.start_server
65
+ end
66
+
67
+ config.after(:suite) do
68
+ RedisHelper.stop_server
69
+ end
70
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delayed-method
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-14 00:00:00.000000000 Z
12
+ date: 2012-09-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: resque
@@ -104,6 +104,7 @@ files:
104
104
  - lib/delayed-method.rb
105
105
  - spec/delayed_method_spec.rb
106
106
  - spec/spec_helper.rb
107
+ - spec/support/redis.rb
107
108
  homepage: http://github.com/phuongnd08/delayed-method
108
109
  licenses: []
109
110
  post_install_message:
@@ -124,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
125
  version: '0'
125
126
  requirements: []
126
127
  rubyforge_project:
127
- rubygems_version: 1.8.24
128
+ rubygems_version: 1.8.23
128
129
  signing_key:
129
130
  specification_version: 3
130
131
  summary: Allow you to quickly move a long call to background which then executed by