Skip to content

Rack middleware that implements the Worker Pattern to process generic GET requests in the background and only serve them from a cache.

License

Notifications You must be signed in to change notification settings

csquared/rack-worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rack::Worker

Build Status

Rack middleware that implements the Worker Pattern.

It processes GET requests with a worker backend and only serves them straight from a cache.
While processing the request it serves empty HTTP 202 responses. Your web frontend is never blocked processing the request.

How it works

When GET requests hit your app, the middleware tries to serve them from the cache.

If the request is not found, it stores the environment data in the cache. A worker process will then use the App.call(env) convention from Rack to run the request through your webapp in the background as if it were a normal Rack request. The status, headers, and body are then stored in the cache so they can be served.

What makes this technique different from a standard HTTP caching approach is that your web server never processes the long HTTP request. The middleware will return empty HTTP 202 responses unless the response is found in the cache. Every request that generates a 202 will only queue one background job per URL.

Installation

Add this line to your application's Gemfile:

gem 'rack-worker'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-worker

Usage

class  App < Sinatra::Base
  use Rack::Worker

  get '/long_ass_request' do
    long_ass_work
  end
end

That's it! Now GETs to /long_ass_request will be processed in the background and only serve HTTP 202 responses until they are processed, after which they will return whatever your app would have returned.

If you already have queue_classic and dalli installed, everything will just work.

See configuration for setting an expiry time on records.

Configuration

  Rack::Worker.cache = Dalli::Client.new(nil, {:expires_in => 300})

The cache can be anything that responds to get(key) and add(key, string)

  Rack::Worker.queue = QC

The queue can be anything that responds to enqueue(method, *params)

Clients

Here's an example of a polling GET request in javascript using jQuery:

Lib.get = function(path, params, callback) {
  var success_callback = function(data, textStatus, xhr){
    if(xhr.status == 202){
      setTimeout(function() {
        return Lib.get(path, params, callback)  
      }, 500)
    }else{
      return callback(data)
    }
  }

  $.ajax({
    url: path, 
    success: success_callback,
    'data': params,
    dataType: 'json' 
  })
}

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Rack middleware that implements the Worker Pattern to process generic GET requests in the background and only serve them from a cache.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages