Class: Langis::Rackish::RackishJob
- Inherits:
-
Struct
- Object
- Struct
- Langis::Rackish::RackishJob
- Defined in:
- lib/langis/rackish.rb
Overview
RackishJob is a dual DelayedJob-Resque job that is used to execute Rackish applications, or Rack applications that are robust against non-conformant “Rack Environments”, in the background. Rackish Applications are created and registered with this RackishJob class. Each registration is associated with an app_key that is well known to any component that wants to execute that particular Rackish Application. Client components then queue up this job class with the app_key and the input hash for that application.
Notes
-
This class does not provide a compliant Rack specified environment to the Rackish applications it calls. Prepend middleware that provides such an environment to the application chain if required.
For example, to queue up a RackishJob using DelayedJob:
Delayed::Job.enqueue Langis::Rackish::RackishJob.new(
'my_app',
{
'input_key' => 'value'
})
For example, to queue up a RackishJob using Resque:
Resque.enqueue(
Langis::Rackish::RackishJob,
'my_app',
{
'input_key' => 'value'
})
The my_app job may be registered in the worker process as follows:
Langis::Rackish::RackishJob.register_rackish_app(
'my_app',
lambda { |env|
# Do something
})
Instance Attribute Summary collapse
-
#app_key ⇒ Object
Returns the value of attribute app_key.
-
#env ⇒ Object
Returns the value of attribute env.
Class Method Summary collapse
-
.perform(app_key = nil, env = {}) ⇒ Object
Acts as the Resque starting point.
-
.register_rackish_app(app_key, app) ⇒ #call
Registers a Rackish Application under a given name so it can be executed by the RackishJob class via the DelayedJob or Resque background job libraries.
Instance Method Summary collapse
-
#perform ⇒ Object
Acts as the DelayedJob starting point.
Instance Attribute Details
#app_key ⇒ Object
Returns the value of attribute app_key
49 50 51 |
# File 'lib/langis/rackish.rb', line 49 def app_key @app_key end |
#env ⇒ Object
Returns the value of attribute env
49 50 51 |
# File 'lib/langis/rackish.rb', line 49 def env @env end |
Class Method Details
.perform(app_key = nil, env = {}) ⇒ Object
Acts as the Resque starting point.
For example, the following can be used to execute the ‘my_app’ Rackish application using Resque from an ActiveRecord callback:
def after_create(record)
Resque.enqueue RackishJob, 'my_app', { 'my.data' => record.id }
end
91 92 93 94 95 96 97 98 |
# File 'lib/langis/rackish.rb', line 91 def perform(app_key=nil, env={}) app = @apps[app_key] if app.respond_to? :call app.call env else raise NotFoundError.new "#{app_key} not found" end end |
.register_rackish_app(app_key, app) ⇒ #call
Registers a Rackish Application under a given name so it can be executed by the RackishJob class via the DelayedJob or Resque background job libraries.
For example, the following can be found in a Rails initializer.
my_app = Rack::Builder.app do
run MyApp
end
RackishJob.register 'my_app', my_app
68 69 70 71 |
# File 'lib/langis/rackish.rb', line 68 def register_rackish_app(app_key, app) @apps ||= {} @apps[app_key.to_s] = app end |
Instance Method Details
#perform ⇒ Object
Acts as the DelayedJob starting point. All this does is relays the call to the Resque starting point.
For example, the following can be used to execute the ‘my_app’ Rackish application using DelayedJob from an ActiveRecord callback:
def after_create(record)
Delayed::Job.enqueue(
RackishJob.new('my_app', { 'my.data' => record.id }))
end
113 114 115 |
# File 'lib/langis/rackish.rb', line 113 def perform self.class.perform(app_key.to_s, env || {}) end |