resque-delay-with-mongoid 0.0.1

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) 2013 K$
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.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ resque-delay-with-mongoid
2
+ ---------------
3
+
4
+ Based on [resque-delay by Michael Rykov](https://github.com/rykov/resque-delay)
5
+
6
+ Adds support for mongoid backends.
7
+
8
+ Requires the resque gem.
9
+
10
+ Allows to call .send_later or .delay.method on objects ala DelayedJob
11
+
12
+
13
+ Installation
14
+ ============
15
+
16
+ $ gem install resque-delay-with-mongoid
17
+
18
+
19
+ Author
20
+ =====
21
+
22
+ K$ :: kdmny30@gmail.com
data/Rakefile ADDED
@@ -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_with_mongoid/performable_method'
2
+ require 'resque_delay_with_mongoid/message_sending'
3
+
4
+ Object.send(:include, ResqueDelayWithMongoid::MessageSending)
5
+ Module.send(:include, ResqueDelayWithMongoid::MessageSending::ClassMethods)
@@ -0,0 +1,49 @@
1
+ require 'active_support/basic_object'
2
+
3
+ module ResqueDelayWithMongoid
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.resque_args)
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,74 @@
1
+ module ResqueDelayWithMongoid
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
+ MG_STRING_FORMAT = /^MG\:([A-Z][\w\:]+)\:(\w+)$/
6
+
7
+ def self.create(object, method, args)
8
+ raise NoMethodError, "undefined method `#{method}' for #{object.inspect}" unless object.respond_to?(method)
9
+ self.new(object, method, args)
10
+ end
11
+
12
+ def initialize(object, method, args)
13
+ self.object = dump(object)
14
+ self.args = args.map { |a| dump(a) }
15
+ self.method = method.to_sym
16
+ end
17
+
18
+ def resque_args
19
+ [object, method, args]
20
+ end
21
+
22
+ def display_name
23
+ case self.object
24
+ when CLASS_STRING_FORMAT then "#{$1}.#{method}"
25
+ when AR_STRING_FORMAT then "#{$1}##{method}"
26
+ when MG_STRING_FORMAT then "#{$1}##{method}"
27
+ else "Unknown##{method}"
28
+ end
29
+ end
30
+
31
+ def perform
32
+ load(object).send(method, *args.map{|a| load(a)})
33
+ rescue ActiveRecord::RecordNotFound
34
+ # We cannot do anything about objects which were deleted in the meantime
35
+ true
36
+ end
37
+
38
+ private
39
+
40
+ def load(arg)
41
+ case arg
42
+ when CLASS_STRING_FORMAT then $1.constantize
43
+ when AR_STRING_FORMAT then $1.constantize.find($2)
44
+ when MG_STRING_FORMAT then $1.constantize.find($2)
45
+ else arg
46
+ end
47
+ end
48
+
49
+ def dump(arg)
50
+ case arg
51
+ when Class then class_to_string(arg)
52
+ when ActiveRecord::Base then ar_to_string(arg)
53
+ else
54
+ if defined?(Mongoid) && arg.is_a?(Mongoid::Document)
55
+ mg_to_string(arg)
56
+ else
57
+ arg
58
+ end
59
+ end
60
+ end
61
+
62
+ def mg_to_string(obj)
63
+ "MG:#{obj.class}:#{obj.id}"
64
+ end
65
+
66
+ def ar_to_string(obj)
67
+ "AR:#{obj.class}:#{obj.id}"
68
+ end
69
+
70
+ def class_to_string(obj)
71
+ "CLASS:#{obj.name}"
72
+ end
73
+ end
74
+ 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,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-delay-with-mongoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - K$
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: resque
16
+ requirement: &70333215299280 !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: *70333215299280
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70333215298780 !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: *70333215298780
36
+ description: ! 'Enable send_later support for Resque. Includes support for mongoid
37
+ backends. Based on the
38
+
39
+ original resque-delay gem by Michael Ryko.
40
+
41
+ '
42
+ email: kdmny30@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - README.md
48
+ - Rakefile
49
+ - LICENSE
50
+ - lib/resque-delay-with-mongoid.rb
51
+ - lib/resque_delay_with_mongoid/message_sending.rb
52
+ - lib/resque_delay_with_mongoid/performable_method.rb
53
+ - spec/resque_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: http://github.com/kdmny/resque-delay-with-mongoid
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.11
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Enable send_later/delay for Resque with support for mongoid backends.
79
+ test_files: []