resque-delay 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Michael Rykov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ resque-delay
2
+ ---------------
3
+
4
+ Requires the resque gem.
5
+
6
+ Allows to call .send_later or .delay.method on objects ala DelayedJob
7
+
8
+
9
+ Installation
10
+ ============
11
+
12
+ $ gem install resque-delay
13
+
14
+
15
+ Author
16
+ =====
17
+
18
+ Michael Rykov :: mrykov@gmail.com
@@ -0,0 +1,7 @@
1
+ task :default => :spec
2
+ task :test => :spec
3
+
4
+ desc "Run specs"
5
+ task :spec do
6
+ exec "spec spec/resque_spec.rb"
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'resque_delay/performable_method'
2
+ require 'resque_delay/message_sending'
3
+
4
+ Object.send(:include, ResqueDelay::MessageSending)
5
+ Module.send(:include, ResqueDelay::MessageSending::ClassMethods)
@@ -0,0 +1,49 @@
1
+ require 'active_support/basic_object'
2
+
3
+ module ResqueDelay
4
+ class DelayProxy < ActiveSupport::BasicObject
5
+ def initialize(target, options)
6
+ @target = target
7
+ @options = options
8
+ end
9
+
10
+ def method_missing(method, *args)
11
+ queue = @options[:to] || :default
12
+ performable_method = PerformableMethod.create(@target, method, args)
13
+ Resque::Job.create(queue, DelayProxy, performable_method)
14
+ end
15
+
16
+ # Called asynchrously by Resque
17
+ def self.perform(args)
18
+ PerformableMethod.new(*args).perform
19
+ end
20
+ end
21
+
22
+ module MessageSending
23
+ def delay(options = {})
24
+ DelayProxy.new(self, options)
25
+ end
26
+ alias __delay__ delay
27
+
28
+ #def send_later(method, *args)
29
+ # warn "[DEPRECATION] `object.send_later(:method)` is deprecated. Use `object.delay.method"
30
+ # __delay__.__send__(method, *args)
31
+ #end
32
+ #
33
+ #def send_at(time, method, *args)
34
+ # warn "[DEPRECATION] `object.send_at(time, :method)` is deprecated. Use `object.delay(:run_at => time).method"
35
+ # __delay__(:run_at => time).__send__(method, *args)
36
+ #end
37
+
38
+ module ClassMethods
39
+ def handle_asynchronously(method)
40
+ aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
41
+ with_method, without_method = "#{aliased_method}_with_delay#{punctuation}", "#{aliased_method}_without_delay#{punctuation}"
42
+ define_method(with_method) do |*args|
43
+ delay.__send__(without_method, *args)
44
+ end
45
+ alias_method_chain method, :delay
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,58 @@
1
+ module ResqueDelay
2
+ class PerformableMethod < Struct.new(:object, :method, :args)
3
+ CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
4
+ AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/
5
+
6
+ def self.create(object, method, args)
7
+ raise NoMethodError, "undefined method `#{method}' for #{object.inspect}" unless object.respond_to?(method)
8
+ self.new(object, method, args)
9
+ end
10
+
11
+ def initialize(object, method, args)
12
+ self.object = dump(object)
13
+ self.args = args.map { |a| dump(a) }
14
+ self.method = method.to_sym
15
+ end
16
+
17
+ def display_name
18
+ case self.object
19
+ when CLASS_STRING_FORMAT then "#{$1}.#{method}"
20
+ when AR_STRING_FORMAT then "#{$1}##{method}"
21
+ else "Unknown##{method}"
22
+ end
23
+ end
24
+
25
+ def perform
26
+ load(object).send(method, *args.map{|a| load(a)})
27
+ rescue ActiveRecord::RecordNotFound
28
+ # We cannot do anything about objects which were deleted in the meantime
29
+ true
30
+ end
31
+
32
+ private
33
+
34
+ def load(arg)
35
+ case arg
36
+ when CLASS_STRING_FORMAT then $1.constantize
37
+ when AR_STRING_FORMAT then $1.constantize.find($2)
38
+ else arg
39
+ end
40
+ end
41
+
42
+ def dump(arg)
43
+ case arg
44
+ when Class then class_to_string(arg)
45
+ when ActiveRecord::Base then ar_to_string(arg)
46
+ else arg
47
+ end
48
+ end
49
+
50
+ def ar_to_string(obj)
51
+ "AR:#{obj.class}:#{obj.id}"
52
+ end
53
+
54
+ def class_to_string(obj)
55
+ "CLASS:#{obj.name}"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,6 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'logger'
3
+
4
+ describe "resque" do
5
+
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ $TESTING=true
3
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'resque'
5
+
6
+ Spec::Matchers.define :have_key do |expected|
7
+ match do |redis|
8
+ redis.exists(expected)
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-delay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Rykov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: resque
16
+ requirement: &70232132418640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '1.9'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70232132418640
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70232132417640 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '2.3'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70232132417640
36
+ description: ! 'Enable send_later support for Resque
37
+
38
+ '
39
+ email: mrykov@gmail
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - README.md
45
+ - Rakefile
46
+ - LICENSE
47
+ - lib/resque-delay.rb
48
+ - lib/resque_delay/message_sending.rb
49
+ - lib/resque_delay/performable_method.rb
50
+ - spec/resque_spec.rb
51
+ - spec/spec_helper.rb
52
+ homepage: http://github.com/rykov/resque-delay
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Enable send_later/delay for Resque
76
+ test_files: []