Sha256: 4ad46f84a9afbb4608b11f6525cd8cd08734e91826bcaf3bd50504de0a9f0a98

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

class WorklessChecker
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, response = @app.call(env)
    return [status, headers, response] if file?(headers) || empty?(response)

    Delayed::Job.scaler.up if Delayed::Job.scaler.jobs.size > 0
    response_body = nil
    if status == 200 && !response.body.frozen? && html_request?(headers, response)
      response_body = response.body << "\n<!-- workless jobs: #{Delayed::Job.scaler.jobs.size} -->"
      headers['Content-Length'] = response_body.bytesize.to_s
    end

    return [status, headers, response_body ? [response_body] : response]
  end

  # fix issue if response's body is a Proc
  def empty?(response)
    # response may be ["Not Found"], ["Move Permanently"], etc.
    (response.is_a?(Array) && response.size <= 1) ||
      !response.respond_to?(:body) ||
      !response.body.respond_to?(:empty?) ||
      response.body.empty?
  end

  # if send file?
  def file?(headers)
    headers["Content-Transfer-Encoding"] == "binary"
  end

  def html_request?(headers, response)
    headers['Content-Type'] && headers['Content-Type'].include?('text/html') && response.body.include?("<html")
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workless_revived-1.2.4.2 lib/workless/middleware/workless_checker.rb
workless_revived-1.2.4 lib/workless/middleware/workless_checker.rb