resque-access_worker_from_job 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.rdoc +39 -10
- data/VERSION +1 -1
- data/lib/resque-access_worker_from_job.rb +1 -1
- data/lib/resque/job.rb +9 -0
- data/lib/resque/plugins/access_worker_from_job.rb +47 -23
- data/resque-access_worker_from_job.gemspec +52 -0
- metadata +4 -2
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,32 +1,61 @@
|
|
1
1
|
= Resque-AccessWorkerFromJob
|
2
2
|
|
3
|
-
Small plugin
|
3
|
+
Small plugin allowing the +perform+ method of Resque jobs to access the worker running them via the cleverly-named +worker+ instance variable.
|
4
4
|
|
5
5
|
Purpose: this allows jobs to access shared sockets in the parent worker.
|
6
6
|
|
7
|
-
|
8
|
-
Be careful to note that each job forks before running, so ivars will be a COPY and, if changed,
|
9
|
-
their changes won't persist into the next job's perform method. However, since sockets ARE persisted, this
|
10
|
-
allows multiple jobs to share a single persistent socket kept alive in the worker.
|
11
|
-
|
12
|
-
=== Only running in certain workers
|
13
|
-
As additional functionality, this plugin can also abort a job gracefully if it's picked up by the wrong worker
|
7
|
+
As additional functionality, it can also abort a job gracefully if it's picked up by the wrong worker
|
14
8
|
class, which is useful if you've subclassed Resque::Worker to add your own functionality and need to ensure
|
15
9
|
your jobs aren't accidentally run against the original superclass.
|
16
10
|
|
11
|
+
Developed against Resque 1.8.0.
|
12
|
+
|
13
|
+
|
17
14
|
== Usage
|
18
15
|
To use, add
|
19
16
|
|
20
17
|
extend Resque::Plugins::AccessWorkerFromJob
|
21
18
|
|
22
|
-
to the class with the perform method
|
19
|
+
to the _bottom_ of the class with the perform method (the extend line must come _after_ the perform method has
|
20
|
+
already been defined, or else the alias method will fail). Now your perform method can reference a +worker+
|
21
|
+
attribute.
|
22
|
+
|
23
|
+
Example:
|
24
|
+
|
25
|
+
class MessageJob
|
26
|
+
@queue = :messages
|
27
|
+
|
28
|
+
# Example using shared socket from worker (via @worker or self.worker)
|
29
|
+
def self.perform( msg )
|
30
|
+
worker.socket.write( msg )
|
31
|
+
end
|
32
|
+
|
33
|
+
extend Resque::Plugins::AccessWorkerFromJob
|
34
|
+
self.required_worker_class = 'CustomApplication::MessageSender'
|
35
|
+
end
|
23
36
|
|
24
|
-
|
37
|
+
|
38
|
+
To implement the additional abort-if-picked-up-by-wrong-worker-class feature, add
|
25
39
|
|
26
40
|
self.required_worker_class = 'ClassName'
|
27
41
|
|
28
42
|
as well.
|
29
43
|
|
44
|
+
|
45
|
+
== Warnings
|
46
|
+
=== Argument errors in mixed environments
|
47
|
+
Internally, this overrides Resque::Job to append the worker to the argument list passed to any <code>JobClass#perform</code>
|
48
|
+
method. For a class that extends Resque::Plugins::AccessWorkerFromJob, this is handled transparently (worker is
|
49
|
+
removed from the argument list and is available via the <code>@worker</code> instance variable, instead). For mixed environments
|
50
|
+
where not all jobs to be run extend AccessWorkerFromJob, and where some of these expect a set number of arguments,
|
51
|
+
this may cause problems ("wrong number of arguments" exceptions).
|
52
|
+
|
53
|
+
=== Ephemeral nature of worker changes
|
54
|
+
Be careful to note that each job forks before running, so instance variables will be a _copy_ and, if changed,
|
55
|
+
their changes won't persist into the next job's perform method. However, since sockets ARE persisted, this
|
56
|
+
allows multiple jobs to share a single persistent socket kept alive in the worker.
|
57
|
+
|
58
|
+
|
30
59
|
== Copyright
|
31
60
|
|
32
61
|
Copyright (c) 2010 Kali Donovan. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require File.expane_path(File.dirname(__FILE__)) + "/resque/plugins/access_worker_from_job.rb"
|
data/lib/resque/job.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
module Resque # :nodoc:
|
2
|
+
# Resque-AccessWorkerFromJob plugin overrides the Resque::Job#args method to pass the worker along as the last argument.
|
3
|
+
class Job
|
4
|
+
# Overridden args appends the worker when returning the list of args represented in this job's payload.
|
5
|
+
def args
|
6
|
+
@payload['args'] + [worker]
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -1,46 +1,70 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# their changes won't persist into the next job's perform method. However, since sockets ARE persisted, this
|
8
|
-
# allows multiple jobs to share a single persistent socket kept alive in the worker.
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + '/../job'
|
2
|
+
|
3
|
+
module Resque
|
4
|
+
module Plugins # :nodoc:
|
5
|
+
# Adds a Resque plugin to allow jobs to access the worker running them via a cleverly-named +worker+
|
6
|
+
# instance variable. Purpose: this allows jobs to access shared sockets in the parent worker.
|
9
7
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
8
|
+
# Be careful to note that each job forks before running, however, so instance variables will be a COPY and, if
|
9
|
+
# changed, their changes won't persist into the next job's perform method. However, since sockets ARE persisted,
|
10
|
+
# this allows multiple jobs to share a single persistent socket kept alive in the worker.
|
13
11
|
#
|
14
12
|
# To use, add
|
15
13
|
#
|
16
14
|
# extend Resque::Plugins::AccessWorkerFromJob
|
17
15
|
#
|
18
|
-
# to the class with the perform method
|
16
|
+
# to the BOTTOM of the class with the perform method (the extend line must come AFTER the perform method has
|
17
|
+
# already been defined, or else the alias method will fail). Now your perform method can reference a +worker+
|
18
|
+
# attribute.
|
19
19
|
#
|
20
|
-
#
|
20
|
+
# As additional functionality, this plugin can also abort a job gracefully if it's picked up by the wrong worker
|
21
|
+
# class, which is useful if you've subclassed Resque::Worker to add your own functionality and need to ensure
|
22
|
+
# your jobs aren't accidentally run against the original superclass. Note that if your job is picked up by the
|
23
|
+
# wrong class it'll be removed from the queue, though -- this provides a way to avoid messy exceptions, but it's
|
24
|
+
# still your responsibility to make sure you keep your queues + worker classes straight.
|
25
|
+
#
|
26
|
+
# To implement this additional requiring-certain-worker-class feature, add
|
21
27
|
#
|
22
28
|
# self.required_worker_class = 'ClassName'
|
23
29
|
#
|
24
30
|
# as well.
|
25
31
|
module AccessWorkerFromJob
|
26
|
-
attr_accessor :required_worker_class
|
32
|
+
attr_accessor :required_worker_class, :worker
|
27
33
|
|
28
|
-
|
29
|
-
|
30
|
-
|
34
|
+
def self.extended(base)
|
35
|
+
|
36
|
+
unless base.methods.include?('perform')
|
37
|
+
raise %Q{You must call "extend Resque::Plugins::AccessWorkerFromJob" AFTER (below) defining the perform method}
|
38
|
+
end
|
39
|
+
|
40
|
+
class << base
|
41
|
+
|
42
|
+
# Remove worker from last argument, so can write their perform method assuming just the arguments
|
43
|
+
# they sent it, without worrying about the appended worker.
|
44
|
+
def perform_with_worker_in_arguments(*args)
|
45
|
+
args.pop if args.last.is_a?(::Resque::Worker)
|
46
|
+
perform_without_modified_arguments( *args )
|
47
|
+
end
|
48
|
+
|
49
|
+
alias_method :perform_without_modified_arguments, :perform unless method_defined?(:perform_without_modified_arguments)
|
50
|
+
alias_method :perform, :perform_with_worker_in_arguments
|
51
|
+
end
|
31
52
|
end
|
32
53
|
|
33
|
-
|
54
|
+
|
55
|
+
# Abort gracefully if picked up by the wrong worker (job will be removed from queue)
|
34
56
|
def before_perform_ensure_proper_worker(*args)
|
35
|
-
|
57
|
+
if args.last.is_a?(::Resque::Worker)
|
58
|
+
self.worker = args.last
|
59
|
+
else
|
60
|
+
raise "You must override Resque::Job#args to pass worker as last argument. See README."
|
61
|
+
end
|
62
|
+
|
36
63
|
if required_worker_class && worker.class.name != required_worker_class
|
37
64
|
raise ::Resque::Job::DontPerform
|
38
65
|
end
|
39
|
-
|
40
|
-
# TODO: Make sure this keeps the job in the queue! seems to just skip it and remove from queue, so it'll never be performed
|
41
|
-
# raise ::Resque::Job::DontPerform
|
42
66
|
end
|
43
67
|
|
44
68
|
end
|
45
69
|
end
|
46
|
-
end
|
70
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{resque-access_worker_from_job}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kali Donovan"]
|
12
|
+
s.date = %q{2010-04-25}
|
13
|
+
s.description = %q{By allowing multiple jobs to share a single socket, which is persisted over the life of the worker, this plugin is an important building block for implementing a Resque-based service send background iPhone messages via the Apple Push Notification servers.}
|
14
|
+
s.email = %q{kali.donovan@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"init.rb",
|
27
|
+
"lib/resque-access_worker_from_job.rb",
|
28
|
+
"lib/resque/job.rb",
|
29
|
+
"lib/resque/plugins/access_worker_from_job.rb",
|
30
|
+
"rails/init.rb",
|
31
|
+
"resque-access_worker_from_job.gemspec"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/kdonovan/resque-access_worker_from_job}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.5}
|
37
|
+
s.summary = %q{Resque plugin to allow jobs access to their calling worker at runtime.}
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<resque>, [">= 0"])
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<resque>, [">= 0"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<resque>, [">= 0"])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resque-access_worker_from_job
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kali Donovan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-04-
|
12
|
+
date: 2010-04-25 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -40,8 +40,10 @@ files:
|
|
40
40
|
- VERSION
|
41
41
|
- init.rb
|
42
42
|
- lib/resque-access_worker_from_job.rb
|
43
|
+
- lib/resque/job.rb
|
43
44
|
- lib/resque/plugins/access_worker_from_job.rb
|
44
45
|
- rails/init.rb
|
46
|
+
- resque-access_worker_from_job.gemspec
|
45
47
|
has_rdoc: true
|
46
48
|
homepage: http://github.com/kdonovan/resque-access_worker_from_job
|
47
49
|
licenses: []
|