Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding functionality+documentation

  • Loading branch information...
commit 5b80d102770169c1eab5b826e1dcac9f6710937b 1 parent c134776
@kdonovan authored
View
41 README.rdoc
@@ -1,16 +1,31 @@
-= resque-worker-access-from-job
-
-Description goes here.
-
-== Note on Patches/Pull Requests
-
-* Fork the project.
-* Make your feature addition or bug fix.
-* Add tests for it. This is important so I don't break it in a
- future version unintentionally.
-* Commit, do not mess with rakefile, version, or history.
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-* Send me a pull request. Bonus points for topic branches.
+= Resque-WorkerAccessFromJob
+
+Small plugin appending the calling worker to the argument list received by the +perform+ method of Resque jobs. Developed against Resque 1.8.0.
+
+Purpose: this allows jobs to access shared sockets in the parent worker.
+
+=== Warning
+Be careful to note that each job forks before running, so ivars will be a COPY and, if changed,
+their changes won't persist into the next job's perform method. However, since sockets ARE persisted, this
+allows multiple jobs to share a single persistent socket kept alive in the worker.
+
+=== Only running in certain workers
+As additional functionality, this plugin can also abort a job gracefully if it's picked up by the wrong worker
+class, which is useful if you've subclassed Resque::Worker to add your own functionality and need to ensure
+your jobs aren't accidentally run against the original superclass.
+
+== Usage
+To use, add
+
+ extend Resque::Plugins::AccessWorkerFromJob
+
+to the class with the perform method.
+
+To implement the additional requiring-certain-worker-class feature, add
+
+ self.required_worker_class = 'ClassName'
+
+as well.
== Copyright
View
1  init.rb
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + "/rails/init.rb"
View
1  lib/resque-worker-access-from-job.rb
@@ -0,0 +1 @@
+require 'lib/resque/plugins/access_worker_from_job'
View
46 lib/resque/plugins/access_worker_from_job.rb
@@ -0,0 +1,46 @@
+module Resque
+ module Plugins
+ # Adds a Resque plugin to allow jobs to access the worker running them as the last argument in the call
+ # to +perform+. Purpose: this allows jobs to access shared sockets in the parent worker.
+ #
+ # Be careful to note that each job forks before running, however, so ivars will be a COPY and, if changed,
+ # their changes won't persist into the next job's perform method. However, since sockets ARE persisted, this
+ # allows multiple jobs to share a single persistent socket kept alive in the worker.
+ #
+ # As additional functionality, this plugin can also abort a job gracefully if it's picked up by the wrong worker
+ # class, which is useful if you've subclassed Resque::Worker to add your own functionality and need to ensure
+ # your jobs aren't accidentally run against the original superclass.
+ #
+ # To use, add
+ #
+ # extend Resque::Plugins::AccessWorkerFromJob
+ #
+ # to the class with the perform method.
+ #
+ # To implement the additional requiring-certain-worker-class feature, add
+ #
+ # self.required_worker_class = 'ClassName'
+ #
+ # as well.
+ module AccessWorkerFromJob
+ attr_accessor :required_worker_class
+
+ # Overrides args to append the worker when returning the list of args represented in this job's payload.
+ def args
+ @payload['args'] + [worker]
+ end
+
+ # Abort gracefully if picked up by the wrong worker, so message remains in queue to be sent
+ def before_perform_ensure_proper_worker(*args)
+ worker = args.last
+ if required_worker_class && worker.class.name != required_worker_class
+ raise ::Resque::Job::DontPerform
+ end
+
+ # 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
+ # raise ::Resque::Job::DontPerform
+ end
+
+ end
+ end
+end
View
1  rails/init.rb
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + "/../lib/resque-worker-access-from-job.rb"
Please sign in to comment.
Something went wrong with that request. Please try again.