resque-serializable 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .project
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in resque-serializable.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ require "resque-serializable/version"
2
+ require "resque-serializable/job"
3
+ require 'yaml'
4
+
5
+ module Resque #:nodoc:
6
+ class Job #:nodoc:
7
+ include Resque::Serializable::Job
8
+ end
9
+ end
@@ -0,0 +1,64 @@
1
+ module Resque
2
+ module Serializable
3
+ module Job
4
+
5
+ def self.included(base)
6
+
7
+ base.class_eval do
8
+
9
+ # Alias Resque's Job.perform to Job.perform_after_deserializing
10
+ alias_method :perform_after_deserializing, :perform
11
+
12
+ # Deserializes serialized arguments from YAML.
13
+ # Looks for an argument of type Hash with a key :serialize and deserializes its value.
14
+
15
+ def perform
16
+ args.collect! do |arg|
17
+ if arg.is_a?(Hash) && arg.key?('serialize')
18
+ if arg['serialize'].is_a?(Hash)
19
+ arg['serialize'].values.collect{|a| YAML::load(a) }
20
+ elsif arg['serialize'].is_a?(Array)
21
+ arg['serialize'].collect{|a| YAML::load(a) }
22
+ end
23
+ else
24
+ arg
25
+ end
26
+ end
27
+ args.flatten!
28
+ perform_after_deserializing
29
+ end # perform
30
+
31
+ class << self
32
+
33
+ # Alias Resque's Job.create to Job.create_after_serializing
34
+ alias_method :create_after_serializing, :create
35
+
36
+ # Attempts to serialize job arguments before creating a new job, by looking for
37
+ # an argument of type Hash with a key :serialize and serializing its value to YAML.
38
+ #
39
+ # @param [Symbol] Current Resque job's queue name
40
+ # @param [Class] Resque job worker class
41
+ # @param [Array] anonymous arguments passed to the Resque worker class. A hash argument with key :serialize will be serialized to YAML
42
+ def create(queue, klass, *args)
43
+ args.collect! do |arg|
44
+ if arg.is_a?(Hash) && arg.key?(:serialize)
45
+ if arg[:serialize].is_a?(Hash)
46
+ arg[:serialize].each{|k,v| arg[:serialize][k] = v.to_yaml }
47
+ elsif arg[:serialize].is_a?(Array)
48
+ arg[:serialize].collect!{|a| a.to_yaml }
49
+ end
50
+ end
51
+ arg
52
+ end
53
+ create_after_serializing(queue, klass, *args)
54
+ end #create
55
+
56
+ end #class << self
57
+
58
+ end# base.class_eval
59
+
60
+ end #self.included
61
+
62
+ end #Job
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module Resque
2
+ module Serializable
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "resque-serializable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "resque-serializable"
7
+ s.version = Resque::Serializable::VERSION
8
+ s.authors = ["Zohar Arad"]
9
+ s.email = ["zohar@zohararad.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Serialize & Unserialize Resque job arguments}
12
+ s.description = %q{Adds support for serializable arguments in a Resque job. Serializable arguments can be passed to a Resque job via a Hash containing a :serialize key. If such arguments are found, they will be deserialized before calling the job's perform method}
13
+
14
+ s.rubyforge_project = "resque-serializable"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "resque", "~>1.19.0"
22
+ end
@@ -0,0 +1,115 @@
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
@@ -0,0 +1,43 @@
1
+ dir = File.dirname(File.expand_path(__FILE__))
2
+ require dir + '/test_helper'
3
+
4
+ class SerializableJobArgsTest < Test::Unit::TestCase
5
+ def setup
6
+ $job_args = nil
7
+ Resque.redis.flushall
8
+ @worker = Resque::Worker.new(:test)
9
+ end
10
+
11
+ def test_version
12
+ major, minor, patch = Resque::Version.split('.')
13
+ assert_equal 1, major.to_i
14
+ assert minor.to_i >= 19
15
+ end
16
+
17
+ def test_normal_job_args
18
+ Resque.enqueue(MockJob,1,'hello',{'key' => 'value'})
19
+ @worker.process
20
+ assert_equal 1, $job_args['i'], 'integer argument should be equal 1'
21
+ assert_equal "hello", $job_args['s'], 'string argument should be equal "hello"'
22
+ assert $job_args['h'].is_a?(Hash), 'hash argument should be a hash'
23
+ assert_equal 'value', $job_args['h']['key'], 'hash "key" property should equal "value"'
24
+ end
25
+
26
+ def test_serializable_job_args_array
27
+ Resque.enqueue(MockJob,1,'hello',:serialize => [{'key' => 'value'}])
28
+ @worker.process
29
+ assert_equal 1, $job_args['i'], 'integer argument should be equal 1'
30
+ assert_equal "hello", $job_args['s'], 'string argument should be equal "hello"'
31
+ assert $job_args['h'].is_a?(Hash), 'hash argument should be a hash'
32
+ assert_equal 'value', $job_args['h']['key'], 'hash "key" property should equal "value"'
33
+ end
34
+
35
+ def test_serializable_job_args_hash
36
+ Resque.enqueue(MockJob,1,'hello',:serialize => {'some_hash' => {'key' => 'value'}})
37
+ @worker.process
38
+ assert_equal 1, $job_args['i'], 'integer argument should be equal 1'
39
+ assert_equal "hello", $job_args['s'], 'string argument should be equal "hello"'
40
+ assert $job_args['h'].is_a?(Hash), 'hash argument should be a hash'
41
+ assert_equal 'value', $job_args['h']['key'], 'hash "key" property should equal "value"'
42
+ end
43
+ end
@@ -0,0 +1,41 @@
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
+
9
+ require 'resque-serializable'
10
+ require dir + '/test_jobs'
11
+
12
+ ##
13
+ # make sure we can run redis
14
+ if !system("which redis-server")
15
+ puts '', "** can't find `redis-server` in your path"
16
+ puts "** try running `sudo rake install`"
17
+ abort ''
18
+ end
19
+
20
+ ##
21
+ # start our own redis when the tests start,
22
+ # kill it when they end
23
+ at_exit do
24
+ next if $!
25
+
26
+ if defined?(MiniTest)
27
+ exit_code = MiniTest::Unit.new.run(ARGV)
28
+ else
29
+ exit_code = Test::Unit::AutoRunner.run
30
+ end
31
+
32
+ pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
33
+ puts "Killing test redis server..."
34
+ `rm -f #{dir}/dump.rdb`
35
+ Process.kill("KILL", pid.to_i)
36
+ exit exit_code
37
+ end
38
+
39
+ puts "Starting redis for testing at localhost:9736..."
40
+ `redis-server #{dir}/redis-test.conf`
41
+ Resque.redis = '127.0.0.1:9736'
data/test/test_jobs.rb ADDED
@@ -0,0 +1,14 @@
1
+ # Mock job for unit tests
2
+ class MockJob #:nodoc:
3
+
4
+ @queue = :test
5
+
6
+ def self.perform(i,s,h)
7
+ $job_args = {
8
+ 'i' => i,
9
+ 's' => s,
10
+ 'h' => h
11
+ }
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-serializable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zohar Arad
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: resque
16
+ requirement: &70114808583480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.19.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70114808583480
25
+ description: Adds support for serializable arguments in a Resque job. Serializable
26
+ arguments can be passed to a Resque job via a Hash containing a :serialize key.
27
+ If such arguments are found, they will be deserialized before calling the job's
28
+ perform method
29
+ email:
30
+ - zohar@zohararad.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - .gitignore
36
+ - Gemfile
37
+ - Rakefile
38
+ - lib/resque-serializable.rb
39
+ - lib/resque-serializable/job.rb
40
+ - lib/resque-serializable/version.rb
41
+ - resque-serializable.gemspec
42
+ - test/redis-test.conf
43
+ - test/serializable_job_args_test.rb
44
+ - test/test_helper.rb
45
+ - test/test_jobs.rb
46
+ homepage: ''
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project: resque-serializable
66
+ rubygems_version: 1.8.11
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Serialize & Unserialize Resque job arguments
70
+ test_files:
71
+ - test/redis-test.conf
72
+ - test/serializable_job_args_test.rb
73
+ - test/test_helper.rb
74
+ - test/test_jobs.rb