resque-send-later 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ ## 0.1.0 (2011-05-17)
2
+
3
+ * Initial version.
4
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Monica McArthur
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,70 @@
1
+ ResqueSendLater
2
+ ============
3
+
4
+ A [Resque][rq] plugin. Requires Resque 1.9.10.
5
+
6
+ Implements an approximation of DelayedJob's send_later on resque.
7
+
8
+
9
+ Usage / Examples
10
+ ================
11
+
12
+ Usage 1: Delaying an instance method call for any object that has a class find(id) method
13
+ (e.g., Rails ActiveRecord):
14
+
15
+ The class itself:
16
+
17
+ class TestThing < ActiveRecord::Base
18
+ include Resque::Plugins::SendLater
19
+
20
+ def method_to_delay(arg1, arg2)
21
+ <some code>
22
+ end
23
+ end
24
+
25
+ Delaying a method call on any instance of the class:
26
+
27
+ t = TestThing.find(_id_)
28
+ t.send_later(:method_to_delay, a1, a2)
29
+
30
+ Usage 2: Delaying a class method call on any class
31
+
32
+ The class itself:
33
+
34
+ class TestThing
35
+ include Resque::Plugins::SendLater
36
+
37
+ def TestThing.class_method_to_delay(arg1, arg2)
38
+ <some code>
39
+ end
40
+ end
41
+
42
+ Delaying a method call:
43
+
44
+ TestThing.send_later(:class_method_to_delay, a1, a2)
45
+
46
+
47
+ Customize & Extend
48
+ ==================
49
+
50
+ No options for now.
51
+
52
+
53
+ Install
54
+ =======
55
+
56
+ ### As a gem
57
+
58
+ $ gem install resque-send-later
59
+
60
+ ### In a Rails app, as a plugin
61
+
62
+ $ rails plugin install git://github.com/Mechaferret/resque-send-later.git
63
+
64
+
65
+ Acknowledgements
66
+ ================
67
+
68
+ Copyright (c) 2011 Monica McArthur, released under the MIT license.
69
+
70
+ [rq]: http://github.com/defunkt/resque
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the resque_send_later plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the resque_send_later plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ResqueSendLater'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1 @@
1
+ require 'resque/plugins/send_later'
@@ -0,0 +1,54 @@
1
+ module Resque
2
+ module Plugins
3
+ module SendLater
4
+ begin
5
+ include Spawn
6
+ rescue
7
+ end
8
+ def self.included(base)
9
+ base.extend self
10
+ end
11
+
12
+ def send_later(method, *args)
13
+ if self.instance_of? Class
14
+ classname = self.to_s
15
+ refid = nil
16
+ else
17
+ classname = self.class.to_s
18
+ refid = self.id
19
+ end
20
+ Resque.enqueue(Resque::Plugins::SendLater::DelayedPerformer, classname, refid, method, *args)
21
+ rescue Exception => ex
22
+ handle_exception(ex, method, *args)
23
+ end
24
+
25
+
26
+ def handle_exception(ex, method, *args)
27
+ # Try to spawn instead
28
+ if defined? Spawn
29
+ puts "transaction spawning due to exception #{ex.inspect} #{ex.backtrace.join("\n")}"
30
+ spawn do
31
+ self.send(method, *args)
32
+ end
33
+ else
34
+ puts "no way to handle transaction with exception #{ex.inspect} #{ex.backtrace.join("\n")}"
35
+ end
36
+ end
37
+
38
+ class DelayedPerformer
39
+ @queue = :method
40
+
41
+ def self.perform(klass, object_id, method, *args)
42
+ if object_id.nil? # class method delay
43
+ klass.constantize.send(method, *args)
44
+ else
45
+ model = klass.constantize.send(:find, object_id)
46
+ model.send(method, *args)
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,42 @@
1
+ class TestThing
2
+ include Resque::Plugins::SendLater
3
+
4
+ attr_accessor :id, :delayed_message
5
+ @@delayed_class_message = nil
6
+ @@delayed_message = nil
7
+
8
+ def TestThing.find(_id)
9
+ t = TestThing.new
10
+ t.id = _id
11
+ t
12
+ end
13
+
14
+ def ask_now(what)
15
+ puts "You asked for #{what} from #{self.id} at #{Time.now}"
16
+ self.send_later(:get_later, what)
17
+ end
18
+
19
+ def get_later(what)
20
+ @@delayed_message = "You got #{what} from #{self.id}"
21
+ puts "Do not panic. Thank you for your patience. #{@@delayed_message} at #{Time.now}!"
22
+ end
23
+
24
+ def self.ask_now(what)
25
+ puts "You asked for #{what} from #{self.to_s} at #{Time.now}"
26
+ TestThing.send_later(:get_later, what)
27
+ end
28
+
29
+ def self.get_later(what)
30
+ @@delayed_class_message = "You got #{what} from #{self.to_s}"
31
+ puts "Do not panic. Thank you for your patience. #{@@delayed_class_message} at #{Time.now}!"
32
+ end
33
+
34
+ def self.delayed_class_message
35
+ @@delayed_class_message
36
+ end
37
+
38
+ def self.delayed_message
39
+ @@delayed_message
40
+ end
41
+
42
+ end
@@ -0,0 +1,132 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile ./test/redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9736
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ dir ./test/
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
116
+
117
+ # Use object sharing. Can save a lot of memory if you have many common
118
+ # string in your dataset, but performs lookups against the shared objects
119
+ # pool so it uses more CPU and can be a bit slower. Usually it's a good
120
+ # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
131
+ #shareobjects no
132
+ #shareobjectspoolsize 1024
@@ -0,0 +1,31 @@
1
+ require File.expand_path('test_helper.rb', File.dirname(__FILE__))
2
+
3
+ class SendLaterTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ $success = $lock_failed = $lock_expired = 0
7
+ Resque.redis.flushall
8
+ @worker = Resque::Worker.new(:method)
9
+ end
10
+
11
+ def test_lint
12
+ assert_nothing_raised do
13
+ Resque::Plugin.lint(Resque::Plugins::SendLater)
14
+ end
15
+ end
16
+
17
+ def test_instance_send_later
18
+ t = TestThing.new
19
+ t.id = 11
20
+ t.ask_now('instance request')
21
+ @worker.process
22
+ assert_equal TestThing.delayed_message, "You got instance request from 11"
23
+ end
24
+
25
+ def test_class_send_later
26
+ TestThing.ask_now('class request')
27
+ @worker.process
28
+ assert_equal TestThing.delayed_class_message, 'You got class request from TestThing'
29
+ end
30
+
31
+ end
@@ -0,0 +1,44 @@
1
+ dir = File.dirname(File.expand_path(__FILE__))
2
+ $LOAD_PATH.unshift dir + '/../lib'
3
+ $TESTING = true
4
+
5
+ require 'rubygems'
6
+ require 'test/unit'
7
+ require 'resque'
8
+ require 'active_support'
9
+ require 'active_support/test_case'
10
+
11
+ require 'logger'
12
+ require '../revrails/vendor/plugins/spawn/lib/spawn'
13
+ require 'resque-send-later'
14
+
15
+ # make sure we can run redis
16
+ if !system('which redis-server')
17
+ puts '', "** can't find `redis-server` in your path"
18
+ puts "** try running `sudo rake install`"
19
+ abort ''
20
+ end
21
+
22
+ # start our own redis when the tests start,
23
+ # kill it when they end
24
+ at_exit do
25
+ next if $!
26
+
27
+ if defined?(MiniTest)
28
+ exit_code = MiniTest::Unit.new.run(ARGV)
29
+ else
30
+ exit_code = Test::Unit::AutoRunner.run
31
+ end
32
+
33
+ pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
34
+ puts 'Killing test redis server...'
35
+ `rm -f #{dir}/dump.rdb`
36
+ Process.kill('KILL', pid.to_i)
37
+ exit exit_code
38
+ end
39
+
40
+ puts 'Starting redis for testing at localhost:9736...'
41
+ `redis-server #{dir}/redis-test.conf`
42
+ Resque.redis = '127.0.0.1:9736'
43
+
44
+ Dir.glob(File.expand_path(dir + '/fixtures/*')).each { |filename| require filename }
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-send-later
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Monica McArthur
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-17 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: resque
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 39
30
+ segments:
31
+ - 1
32
+ - 9
33
+ - 10
34
+ version: 1.9.10
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: " A Resque plugin. Implements an approximation of DelayedJob's send_later on resque. Has a little more ease-of-use and features than the example provided with Resque.\n"
38
+ email: mechaferret@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - MIT-LICENSE
49
+ - HISTORY.md
50
+ - lib/resque/plugins/send_later.rb
51
+ - lib/resque-send-later.rb
52
+ - test/fixtures/test_thing.rb
53
+ - test/redis-test.conf
54
+ - test/send_later_test.rb
55
+ - test/test_helper.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/mechaferret/resque-send-later
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: A Resque plugin that implements an approximation of DelayedJob's send_later on resque.
90
+ test_files: []
91
+