瘦服务器多次记录相同的请求

时间:2012-08-12 02:07:47

标签: ruby logging heroku sinatra thin

我正在使用瘦Web服务器来提供Ruby / Sinatra应用程序。引起我注意的是当我跑步时:

RACK_ENV=production rackup

每个请求记录5次,并且时间戳不同。

vagrant@lucid32:/app$ RACK_ENV=production rackup
** Building /assets/application.js...
** Building /assets/screen.css...
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0024
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0034
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0044
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0054
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0065
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0076
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0094
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 - 0.0102

这让我相信这个请求不知何故多次发生。或者只是记录多次?

当我跑步时

rackup

尝试相同的请求。这就是我得到的。

vagrant@lucid32:/app$ rackup
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
10.0.2.2 - - [12/Aug/2012 03:56:42] "GET / HTTP/1.1" 200 - 0.1228

差异基本上是状态代码后面的“ - ”,就像生产服务器中的最终日志行一样。但我真的不知道这意味着什么。

我想知道生产中是否以某种方式处理了多次请求。 我正在这样使用Sinatra这条路线。

class Project::Base < Sinatra::Application
  ..configs here
end

class Project::Routes::Home < Project::Base
  get '/'
    "something"
  end
end

config.ru

RACK_ENV = ENV["RACK_ENV"] ||= "development" unless defined? RACK_ENV
require 'sprockets'
map '/assets' do
  environment = Sprockets::Environment.new
  environment.append_path 'app/assets/javascripts'
  environment.append_path 'app/assets/stylesheets'

  run environment
end
require File.dirname(__FILE__) + '/config/boot.rb'
use Rack::Deflater
run GRBTV::Main

./配置/的boot.rb

require 'rubygems'
gem 'psych'

# Require default environment config
require File.join(File.dirname(__FILE__), 'env')

# Bundler setup check 
require 'bundler'
begin
  Bundler.setup
rescue Bundler::BundlerError => e
  $stderr.puts e.message
  $stderr.puts "Run 'bundle install' to install missing gems"
  exit e.status_code
end


# Sinatra
require 'sinatra/base'
require 'sinatra/flash'
require 'json'

RACK_ENV = ENV['RACK_ENV'] ||= 'development' unless defined? RACK_ENV
ROOT_DIR = File.dirname(__FILE__).gsub(/config/,'')  unless defined? ROOT_DIR

# Path helper methods 
# root_path("config", "settings.yml")
def root_path(*args)
  File.join(ROOT_DIR, *args)
end

# relative_from_root_path("~/projects/project_name/config/settings.yml") #=> "config/settings.yml"
def relative_from_root_path(path)
  Pathname.new(path).relative_path_from(Pathname.new(ROOT_DIR)).to_s
end

# public_path("images") #=> "public/images"
def public_path(*args)
  root_path('public', *args)
end

# Module containers for autoloaded helpers and routes
module GRBTV
  module Helpers
  end
  module Routes
  end
end

# Attempts to require all dependencies with bundler, if this fails, bundle and then try again
require 'bundler'
Bundler.setup(:default)
Bundler.require(:default)

# Dependencies contains all required gems and core configuration
require root_path('config', 'dependencies.rb')

def app() GRBTV::Main end

./配置/ dependencies.rb

require File.join(File.dirname(__FILE__), './loader')

initializers = []
# Requires initializers
Loader.load_files('config/initializers/*.rb') do |file|
  require file
  begin
    # Save Initializer module in the initializers array
    file_class = File.basename(file, '.rb').camelize
    initializers << "#{file_class}Initializer".constantize
  rescue NameError
  end
end

require 'yaml'
YAML::ENGINE.yamler = 'psych'

# Load app components
['lib/*.rb', 'app/uploaders/*.rb', 'app/models/*.rb'].each do |glob|
  Loader.load_files(glob)
end


# Basic Sinatra::Application settingss
class GRBTV::Base < Sinatra::Application
  def options() settings end

  set :root, root_path
  set :views, root_path('app', 'views')
  set :public_folder, root_path('public')
  set :environment, RACK_ENV.to_sym if defined? RACK_ENV
    set :protection, :except => :frame_options

  use Rack::MethodOverride
  enable :sessions
  register Sinatra::NamedRoutes
  register MongoMapper
  helpers Sinatra::FieldBuilder
  helpers Sinatra::Warden::Helpers

  # Require all helpers
  Loader.load_files("app/helpers/**/*.rb") do |file|
    relative_path = relative_from_root_path(file).gsub(/\.rb$/, '').gsub('app/helpers/', '')
    module_name = "GRBTV::Helpers::#{relative_path.camelize}"
    Loader.define_namespaces(module_name)
    require file
    helpers module_name.constantize
  end


  # Sets the default layout for every route
  before do
    @default_layout = :'layout'
  end

  before '/assets/*' do
    headers['Cache-Control'] = 'public, max-age=86400'
  end
end

# Load mailers
Loader.load_files('app/mailers/*.rb')


class GRBTV::Main < GRBTV::Base
  # Requiring routes after extending path helpers
  Loader.load_files("app/routes/**/*.rb") do |file|
    relative_path= relative_from_root_path(file).gsub(/\.rb$/, '').gsub('app/routes/', '')
    module_name = "GRBTV::Routes::#{relative_path.camelize}"
    Loader.define_namespaces(module_name)
    require file
    use module_name.constantize
  end
end

# Load all initializers after setup is done
initializers.each do |initializer|
  GRBTV::Base.register initializer
end

1 个答案:

答案 0 :(得分:0)

这是一个Sinatra / Thin bug,应该在Sinatra 1.3.3(昨天发布)中修复。