resque-concurrent-restriction 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec.opts ADDED
@@ -0,0 +1,8 @@
1
+ --colour
2
+ --format
3
+ specdoc
4
+ --reverse
5
+ --timeout
6
+ 20
7
+ --loadby
8
+ mtime
@@ -0,0 +1,153 @@
1
+ require 'rspec'
2
+ require 'ap'
3
+
4
+ spec_dir = File.dirname(File.expand_path(__FILE__))
5
+ REDIS_CMD = "redis-server #{spec_dir}/redis-test.conf"
6
+
7
+ puts "Starting redis for testing at localhost:9736..."
8
+ puts `cd #{spec_dir}; #{REDIS_CMD}`
9
+
10
+ require 'resque'
11
+ Resque.redis = 'localhost:9736'
12
+ require 'resque-concurrent-restriction'
13
+
14
+ # Schedule the redis server for shutdown when tests are all finished.
15
+ at_exit do
16
+ pid = File.read("#{spec_dir}/redis.pid").to_i rescue nil
17
+ system ("kill -9 #{pid}") if pid != 0
18
+ File.delete("#{spec_dir}/redis.pid") rescue nil
19
+ File.delete("#{spec_dir}/redis-server.log") rescue nil
20
+ File.delete("#{spec_dir}/dump.rdb") rescue nil
21
+ end
22
+
23
+
24
+ ##
25
+ # Helper to perform job classes
26
+ #
27
+ module PerformJob
28
+
29
+ def run_resque_job(job_class, *job_args)
30
+ opts = job_args.last.is_a?(Hash) ? job_args.pop : {}
31
+ queue = opts[:queue] || Resque.queue_from_class(job_class)
32
+
33
+ Resque::Job.create(queue, job_class, *job_args)
34
+
35
+ run_resque_queue(queue, opts)
36
+ end
37
+
38
+ def run_resque_queue(queue, opts={})
39
+ worker = Resque::Worker.new(queue)
40
+ worker.very_verbose = true if opts[:verbose]
41
+
42
+ # do a single job then shutdown
43
+ def worker.done_working
44
+ super
45
+ shutdown
46
+ end
47
+
48
+ if opts[:inline]
49
+ job = worker.reserve
50
+ worker.perform(job)
51
+ else
52
+ worker.work(0)
53
+ end
54
+ end
55
+
56
+ def dump_redis
57
+ result = {}
58
+ Resque.redis.keys("*").each do |key|
59
+ type = Resque.redis.type(key)
60
+ result[key] = case type
61
+ when 'string' then Resque.redis.get(key)
62
+ when 'list' then Resque.redis.lrange(key, 0, -1)
63
+ when 'set' then Resque.redis.smembers(key)
64
+ else type
65
+ end
66
+ end
67
+ return result
68
+ end
69
+
70
+ end
71
+
72
+ module RunCountHelper
73
+
74
+ def around_perform(*args)
75
+ begin
76
+ Resque.redis.set("restricted_job_started:#{self}:#{args.to_json}", true)
77
+ Resque.redis.incr("restricted_job_run_count:#{self}:#{args.to_json}")
78
+ yield
79
+ ensure
80
+ Resque.redis.set("restricted_job_ended:#{self}:#{args.to_json}", true)
81
+ end
82
+ end
83
+
84
+ def perform(*args)
85
+ #puts "Running job #{self}:#{args}"
86
+ end
87
+
88
+ def run_count(*args)
89
+ Resque.redis.get("restricted_job_run_count:#{self}:#{args.to_json}").to_i
90
+ end
91
+
92
+ def total_run_count
93
+ keys = Resque.redis.keys("restricted_job_run_count:#{self}:*")
94
+ keys.inject(0) {|sum, k| sum + Resque.redis.get(k).to_i }
95
+ end
96
+
97
+ def started?(*args)
98
+ return Resque.redis.get("restricted_job_started#{self}:#{args.to_json}") == true
99
+ end
100
+
101
+ def ended?(*args)
102
+ return Resque.redis.get("restricted_job_ended#{self}:#{args.to_json}") == true
103
+ end
104
+ end
105
+
106
+ class NoRestrictionJob
107
+ extend RunCountHelper
108
+ @queue = 'normal'
109
+ end
110
+
111
+ class RestrictionJob
112
+ extend RunCountHelper
113
+ extend Resque::Plugins::ConcurrentRestriction
114
+ concurrent 1
115
+ @queue = 'normal'
116
+ end
117
+
118
+ class IdentifiedRestrictionJob
119
+ extend RunCountHelper
120
+ extend Resque::Plugins::ConcurrentRestriction
121
+ concurrent 1
122
+ @queue = 'normal'
123
+
124
+ def self.concurrent_identifier(*args)
125
+ args.first.to_s
126
+ end
127
+
128
+ end
129
+
130
+ class ConcurrentRestrictionJob
131
+ extend RunCountHelper
132
+ extend Resque::Plugins::ConcurrentRestriction
133
+ concurrent 1
134
+
135
+ @queue = 'normal'
136
+
137
+ def self.perform(*args)
138
+ raise args.first if args.first
139
+ sleep 0.2
140
+ end
141
+ end
142
+
143
+ class MultipleConcurrentRestrictionJob
144
+ extend RunCountHelper
145
+ extend Resque::Plugins::ConcurrentRestriction
146
+ concurrent 4
147
+
148
+ @queue = 'normal'
149
+
150
+ def self.perform(*args)
151
+ sleep 0.5
152
+ end
153
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-concurrent-restriction
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.5.0
6
+ platform: ruby
7
+ authors:
8
+ - Matt Conway
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-08 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: resque
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "1.10"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "2.5"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: awesome_print
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ description: A resque plugin for limiting how many of a specific job can run concurrently
50
+ email:
51
+ - matt@conwaysplace.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - lib/resque-concurrent-restriction.rb
65
+ - lib/resque/plugins/concurrent_restriction/concurrent_restriction_job.rb
66
+ - lib/resque/plugins/concurrent_restriction/resque_worker_extension.rb
67
+ - lib/resque/plugins/concurrent_restriction/version.rb
68
+ - resque-concurrent-restriction.gemspec
69
+ - spec/concurrent_restriction_job_spec.rb
70
+ - spec/redis-test.conf
71
+ - spec/resque_worker_extensions_spec.rb
72
+ - spec/spec.opts
73
+ - spec/spec_helper.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/wr0ngway/resque-concurrent-restriction
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project: resque-concurrent-restriction
98
+ rubygems_version: 1.6.1
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: A resque plugin for limiting how many of a specific job can run concurrently
102
+ test_files:
103
+ - spec/concurrent_restriction_job_spec.rb
104
+ - spec/redis-test.conf
105
+ - spec/resque_worker_extensions_spec.rb
106
+ - spec/spec.opts
107
+ - spec/spec_helper.rb