resque-result 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +38 -0
- data/lib/resque/plugins/result/version.rb +7 -0
- data/lib/resque/plugins/result.rb +54 -0
- data/test/redis-test.conf +132 -0
- data/test/result_test.rb +44 -0
- data/test/test_helper.rb +34 -0
- metadata +127 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Lee Marlow
|
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,28 @@
|
|
1
|
+
Resque Result
|
2
|
+
=============
|
3
|
+
|
4
|
+
A [Resque][rq] plugin. Requires Resque 1.8.
|
5
|
+
|
6
|
+
If you want to be able fetch the result from a Resque
|
7
|
+
job's perform method. Results will be encoded using JSON.
|
8
|
+
|
9
|
+
For example:
|
10
|
+
|
11
|
+
class MyJob
|
12
|
+
extend Resque::Jobs::Result
|
13
|
+
|
14
|
+
def self.perform(meta_id, big_num)
|
15
|
+
factor(big_num)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
meta0 = MyJob.enqueue(3574406403731)
|
20
|
+
meta0.enqueued_at # => 'Wed May 19 13:42:41 -0600 2010'
|
21
|
+
meta0.meta_id # => '03c9e1a045ad012dd20500264a19273c'
|
22
|
+
|
23
|
+
# later
|
24
|
+
meta1 = MyJob.get_meta('03c9e1a045ad012dd20500264a19273c')
|
25
|
+
meta1.succeeded? # => true
|
26
|
+
meta1.result # => [ 1299709, 2750159 ]
|
27
|
+
|
28
|
+
[rq]: http://github.com/defunkt/resque
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
|
4
|
+
def command?(command)
|
5
|
+
system("type #{command} > /dev/null")
|
6
|
+
end
|
7
|
+
|
8
|
+
#
|
9
|
+
# Tests
|
10
|
+
#
|
11
|
+
|
12
|
+
task :default => :test
|
13
|
+
|
14
|
+
if command? :turn
|
15
|
+
desc "Run tests"
|
16
|
+
task :test do
|
17
|
+
suffix = "-n #{ENV['TEST']}" if ENV['TEST']
|
18
|
+
sh "turn test/*_test.rb #{suffix}"
|
19
|
+
end
|
20
|
+
else
|
21
|
+
Rake::TestTask.new do |t|
|
22
|
+
t.libs << 'lib'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# Gems
|
30
|
+
#
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'mg'
|
34
|
+
MG.new("resque-result.gemspec")
|
35
|
+
rescue LoadError
|
36
|
+
warn "mg not available."
|
37
|
+
warn "Install it with: gem i mg"
|
38
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'resque'
|
2
|
+
require 'resque/plugins/result/version'
|
3
|
+
require 'resque/plugins/meta'
|
4
|
+
|
5
|
+
module Resque
|
6
|
+
module Plugins
|
7
|
+
# If you want to be able fetch the result from a Resque
|
8
|
+
# job's perform method. Results will be encoded using JSON.
|
9
|
+
#
|
10
|
+
# For example:
|
11
|
+
#
|
12
|
+
# class MyJob
|
13
|
+
# extend Resque::Jobs::Result
|
14
|
+
#
|
15
|
+
# def self.perform(meta_id, big_num)
|
16
|
+
# factor(big_num)
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# meta0 = MyJob.enqueue(3574406403731)
|
21
|
+
# meta0.enqueued_at # => 'Wed May 19 13:42:41 -0600 2010'
|
22
|
+
# meta0.meta_id # => '03c9e1a045ad012dd20500264a19273c'
|
23
|
+
#
|
24
|
+
# # later
|
25
|
+
# meta1 = MyJob.get_meta('03c9e1a045ad012dd20500264a19273c')
|
26
|
+
# meta1.succeeded? # => true
|
27
|
+
# meta1.result # => [ 1299709, 2750159 ]
|
28
|
+
module Result
|
29
|
+
def self.extended(mod)
|
30
|
+
mod.extend(Resque::Plugins::Meta)
|
31
|
+
Resque::Plugins::Meta::Metadata.send(:include, Resque::Plugins::Result::Metadata)
|
32
|
+
end
|
33
|
+
|
34
|
+
def around_perform_meta_result(meta_id, *args)
|
35
|
+
if meta = get_meta(meta_id)
|
36
|
+
meta.result = yield
|
37
|
+
meta.save
|
38
|
+
else
|
39
|
+
yield
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module Metadata
|
44
|
+
def result
|
45
|
+
self['result']
|
46
|
+
end
|
47
|
+
|
48
|
+
def result=(val)
|
49
|
+
self['result'] = val
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
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
|
data/test/result_test.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require 'resque/plugins/result'
|
3
|
+
|
4
|
+
class ResultJob
|
5
|
+
extend Resque::Plugins::Result
|
6
|
+
@queue = :test
|
7
|
+
|
8
|
+
def self.perform(meta_id, big_num)
|
9
|
+
[ 1299709, 2750159 ]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ResultTest < Test::Unit::TestCase
|
14
|
+
def setup
|
15
|
+
Resque.redis.flushall
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_result_version
|
19
|
+
assert_equal '1.0.0', Resque::Plugins::Result::Version
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_lint
|
23
|
+
assert_nothing_raised do
|
24
|
+
Resque::Plugin.lint(Resque::Plugins::Result)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_resque_version
|
29
|
+
major, minor, patch = Resque::Version.split('.')
|
30
|
+
assert_equal 1, major.to_i
|
31
|
+
assert minor.to_i >= 9
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_processed_job
|
35
|
+
meta = ResultJob.enqueue(3574406403731)
|
36
|
+
assert_nil meta.result
|
37
|
+
worker = Resque::Worker.new(:test)
|
38
|
+
worker.work(0)
|
39
|
+
|
40
|
+
meta = ResultJob.get_meta(meta.meta_id)
|
41
|
+
assert meta.succeeded?, 'Job should have succeeded'
|
42
|
+
assert_equal [ 1299709, 2750159 ], meta.result
|
43
|
+
end
|
44
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
dir = File.dirname(File.expand_path(__FILE__))
|
2
|
+
$LOAD_PATH.unshift dir + '/../lib'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'resque'
|
6
|
+
|
7
|
+
#
|
8
|
+
# make sure we can run redis
|
9
|
+
#
|
10
|
+
|
11
|
+
if !system("which redis-server")
|
12
|
+
puts '', "** can't find `redis-server` in your path"
|
13
|
+
puts "** try running `sudo rake install`"
|
14
|
+
abort ''
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
#
|
19
|
+
# start our own redis when the tests start,
|
20
|
+
# kill it when they end
|
21
|
+
#
|
22
|
+
|
23
|
+
at_exit do
|
24
|
+
next if $!
|
25
|
+
|
26
|
+
pid = `ps -A -o pid,command | grep [r]edis-test`.split(" ")[0]
|
27
|
+
puts "Killing test redis server..."
|
28
|
+
`rm -f #{dir}/dump.rdb`
|
29
|
+
Process.kill("KILL", pid.to_i)
|
30
|
+
end
|
31
|
+
|
32
|
+
puts "Starting redis for testing at localhost:9736..."
|
33
|
+
`redis-server #{dir}/redis-test.conf`
|
34
|
+
Resque.redis = 'localhost:9736'
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-result
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Lee Marlow
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-04 00:00:00 -06: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: 51
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 9
|
33
|
+
- 0
|
34
|
+
version: 1.9.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: resque-meta
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
version: 1.0.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
description: |
|
54
|
+
If you want to be able fetch the result from a Resque
|
55
|
+
job's perform method. Results will be encoded using JSON.
|
56
|
+
|
57
|
+
For example:
|
58
|
+
|
59
|
+
class MyJob
|
60
|
+
extend Resque::Jobs::Result
|
61
|
+
|
62
|
+
def self.perform(meta_id, big_num)
|
63
|
+
factor(big_num)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
meta0 = MyJob.enqueue(3574406403731)
|
68
|
+
meta0.enqueued_at # => 'Wed May 19 13:42:41 -0600 2010'
|
69
|
+
meta0.meta_id # => '03c9e1a045ad012dd20500264a19273c'
|
70
|
+
|
71
|
+
# later
|
72
|
+
meta1 = MyJob.get_meta('03c9e1a045ad012dd20500264a19273c')
|
73
|
+
meta1.succeeded? # => true
|
74
|
+
meta1.result # => [ 1299709, 2750159 ]
|
75
|
+
|
76
|
+
email: lee.marlow@gmail.com
|
77
|
+
executables: []
|
78
|
+
|
79
|
+
extensions: []
|
80
|
+
|
81
|
+
extra_rdoc_files: []
|
82
|
+
|
83
|
+
files:
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- LICENSE
|
87
|
+
- lib/resque/plugins/result/version.rb
|
88
|
+
- lib/resque/plugins/result.rb
|
89
|
+
- test/redis-test.conf
|
90
|
+
- test/result_test.rb
|
91
|
+
- test/test_helper.rb
|
92
|
+
has_rdoc: true
|
93
|
+
homepage: http://github.com/lmarlow/resque-result
|
94
|
+
licenses: []
|
95
|
+
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
requirements: []
|
120
|
+
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.3.7
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: A Resque plugin for retrieving a job's return value.
|
126
|
+
test_files: []
|
127
|
+
|