resque-batched-job 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Daniel Johnston
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,33 @@
1
+ Resque Batched Job
2
+ ==================
3
+
4
+ A [Resque](http://github.com/defunkt/resque) plugin. Requires Resque 1.10.0
5
+
6
+ TODO
7
+ ----
8
+ * Define a complete batch
9
+
10
+ Example
11
+ -------
12
+
13
+ class Job
14
+
15
+ extend Resque::Plugins::BatchedJob
16
+
17
+ @queue = :example
18
+
19
+ def self.before_perform_clean_up(place, name)
20
+ puts "#{name} straightening up desk"
21
+ end
22
+
23
+ def self.perform(place, name)
24
+ puts "#{name} leaving the #{place}"
25
+ sleep 1
26
+ end
27
+
28
+ def self.after_batch_turn_lights_out(place, name)
29
+ puts "#{name} turing the lights out in the #{place}."
30
+ sleep 1
31
+ end
32
+
33
+ end
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ $:.unshift(File.dirname(__FILE__))
6
+
7
+ task :default => :test
8
+
9
+ Rake::TestTask.new do |task|
10
+ task.libs << 'lib'
11
+ task.pattern = 'test/test_*.rb'
12
+ task.verbose = true
13
+ end
@@ -0,0 +1,78 @@
1
+ module Resque
2
+
3
+ module Plugin
4
+
5
+ #
6
+ # This hook is really the meaning of our adventure.
7
+ def after_batch_hooks(job)
8
+ job.methods.grep(/^after_batch/).sort
9
+ end
10
+
11
+ end
12
+
13
+ module Plugins
14
+
15
+ module BatchedJob
16
+
17
+ include Resque::Helpers
18
+
19
+ #
20
+ # Helper method used to generate the batch key.
21
+ def batch(id)
22
+ "batch:#{id}"
23
+ end
24
+
25
+ #
26
+ # Batch the job. The first argument of a batched job, is the batch id.
27
+ def after_enqueue_batch(id, *args)
28
+ redis.sadd(batch(id), encode(args))
29
+ end
30
+
31
+ =begin
32
+ TODO: Determine if it's necessary to double check the jobs existance
33
+ before performing it. If so, do what?
34
+
35
+ def before_perform_audit_batch(id, *args)
36
+ unless redis.sismember(batch(id), "#{encode(args)}")
37
+ raise Resque::Job::DontPerform.new("#{args} are not a member of #{batch(id)}")
38
+ end
39
+ end
40
+ =end
41
+
42
+ #
43
+ # After every job, no matter in the event of success or failure, we need
44
+ # to remove the job from the batch set.
45
+ def around_perform_amend_batch(id, *args)
46
+ begin
47
+ yield
48
+ ensure
49
+ redis.srem(batch(id), "#{encode(args)}")
50
+ end
51
+ end
52
+
53
+ #
54
+ # After each job is performed, check to see if the job is the last of
55
+ # the given batch. If so, run after_batch hooks.
56
+ def after_perform_batch(id, *args)
57
+ if batch_complete?(id)
58
+ after_batch_hooks = Resque::Plugin.after_batch_hooks(self)
59
+ after_batch_hooks.each do |hook|
60
+ send(hook, id, *args)
61
+ end
62
+ end
63
+ end
64
+
65
+ #
66
+ # Checks to see if the batch key exists. If the key does exist, is the
67
+ # set empty? The Redis srem command deletes the key when the last item
68
+ # of a set is removed. Ah, go ahead and check the size.
69
+ def batch_complete?(id)
70
+ redis.scard(batch(id)) == 0
71
+ end
72
+ alias :batch_exist? :batch_complete? # => are they?
73
+
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,7 @@
1
+ module Resque
2
+ module Plugins
3
+ module BatchedJob
4
+ VERSION = '0.1.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'resque'
5
+ require 'resque/plugins/batched_job'
6
+
7
+ class BatchedJobTest < Test::Unit::TestCase
8
+
9
+ def test_list
10
+ assert_nothing_raised do
11
+ Resque::Plugin.lint(Resque::Plugins::BatchedJob)
12
+ end
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-batched-job
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Daniel Johnston
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-31 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: resque
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 10
31
+ - 0
32
+ version: 1.10.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: " Resque plugin for batching jobs. When a batch/group of jobs are complete, \n\
36
+ additional work can be performed usings batch hooks.\n"
37
+ email: dan@dj-agiledev.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - LICENSE
46
+ - Rakefile
47
+ - README.markdown
48
+ - lib/resque/plugins/batched_job.rb
49
+ - lib/resque/plugins/batched_job/version.rb
50
+ - test/test_batched_job.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/djohnston/resque-batched-job
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options: []
57
+
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Resque plugin
83
+ test_files:
84
+ - test/test_batched_job.rb