module Sails

Sails

You can custom Sails configs in config/application.rb

module Sails
  config.app_name = 'you_app_name'
  config.thrift.port = 7075
  config.thrift.processor = Thrift::YouAppName::Processor

  # Thrift Protocols can be use [:binary, :compact, :json]
  # http://jnb.ociweb.com/jnb/jnbJun2009.html#protocols
  config.thrift.procotol = :binary

  config.autoload_paths += %W(app/workers)

  config.i18n.default_locale = 'zh-CN'

  # cache store
  config.cache_store = [:dalli_store, '127.0.0.1' }]
end

Public Class Methods

cache() click to toggle source

Sails.cache

An abstract cache store class. There are multiple cache store implementations, each having its own additional features. See the classes under the ActiveSupport::Cache module, e.g. ActiveSupport::Cache::MemCacheStore. MemCacheStore is currently the most popular cache store for large production websites.

Some implementations may not support all methods beyond the basic cache methods of fetch, write, read, exist?, and delete.

ActiveSupport::Cache::Store can store any serializable Ruby object.

Sails.cache.read('city')   # => nil
Sails.cache.write('city', "Duckburgh")
Sails.cache.read('city')   # => "Duckburgh"

Keys are always translated into Strings and are case sensitive. When an object is specified as a key and has a cache_key method defined, this method will be called to define the key. Otherwise, the to_param method will be called. Hashes and Arrays can also be used as keys. The elements will be delimited by slashes, and the elements within a Hash will be sorted by key so they are consistent.

Sails.cache.read('city') == Sails.cache.read(:city)   # => true

Nil values can be cached.

# File lib/sails/base.rb, line 55
def cache
  return @cache if defined?(@cache)
  @cache = ActiveSupport::Cache.lookup_store(self.config.cache_store)
end
check_create_dirs() click to toggle source
# File lib/sails/base.rb, line 248
def check_create_dirs
  %w(log tmp tmp/cache tmp/pids).each do |name|
    if not Dir.exist? Sails.root.join(name)
      require "fileutils"
      dir_path = Sails.root.join(name)
      FileUtils.mkdir_p dir_path
      FileUtils.touch [dir_path,".keep"].join("/")
    end
  end
end
config() click to toggle source

Sails.config

Configs with Sails For example:

Sails.config.app_name
# => returns "You App Name"

Sails.config.autoload_paths
# => returns ['app/models','app/models/concerns', 'app/workers', 'app/services'...]

Sails.config.cache_store = [:dalli_store, '127.0.0.1', { pool_size: 100 }]
Sails.config.i18n.default_locale = 'zh-CN'
# File lib/sails/base.rb, line 23
def config
  return @config if defined?(@config)
  @config = Config.new.config
end
env() click to toggle source

Sails.env

returns a string representing the current Sails environment. This will read from ENV like Rails

For example:

Sails.env # in development mode
=> "development"
Sails.env.development?
=> true
# File lib/sails/base.rb, line 92
def env
  @env ||= ActiveSupport::StringInquirer.new(ENV['RAILS_ENV'].presence || 'development')
end
init() click to toggle source
# File lib/sails/base.rb, line 117
def init
  # init root
  return false if @inited == true
  $:.unshift self.root.join("lib")

  self.root
  self.check_create_dirs

  ActiveSupport::Dependencies.autoload_paths += Sails.config.autoload_paths

  env_file = self.root.join('config/environments/',Sails.env + ".rb")
  if File.exist?(env_file)
    require env_file
  end

  require "sails/service"

  self.load_initialize
  @inited = true
end
load_initialize() click to toggle source
# File lib/sails/base.rb, line 242
def load_initialize
  Dir["#{Sails.root}/config/initializers/*.rb"].each do |f|
    require f
  end
end
logger() click to toggle source

Sails.logger

returns a Logger class For example:

Sails.logger.info "Hello world"
Sails.logger.error "Hello world"
# File lib/sails/base.rb, line 104
def logger
  return @logger if defined?(@logger)
  @logger = Logger.new(logger_path)
  @logger.formatter = proc { |severity, datetime, progname, msg|
    "#{msg}\n"
  }
  @logger
end
logger_path() click to toggle source
# File lib/sails/base.rb, line 113
def logger_path
  @logger_path ||= Sails.root.join("log/#{self.env}.log")
end
reload!(opts = {}) click to toggle source

Force reload Sails cache classes in config.autoload_paths

# File lib/sails/base.rb, line 161
def reload!(opts = {})
  force = opts[:force] || false
  if force || config.cache_classes == false
    # @service = nil
    ActiveSupport::Dependencies.clear
    # reload_server!
  end
  true
end
reload_server!() click to toggle source
# File lib/sails/base.rb, line 171
def reload_server!
  if @server
    new_processor = config.processor.new(self.service)
    @server.instance_variable_set(:@processor, new_processor)
  end
end
root() click to toggle source

Sails.root

This method returns a Pathname object which handles paths starting with a / as absolute (starting from the root of the filesystem). Compare:

For example:

>> Sails.root
=> #<Pathname:/some/path/to/project>
>> Sails.root + "file"
=> #<Pathname:/some/path/to/project/file>
# File lib/sails/base.rb, line 70
def root
  return @root if defined?(@root)
  path = `pwd -L`.sub(/\n/,'') rescue Dir.pwd
  @root ||= Pathname.new(path)
end
root=(root) click to toggle source
# File lib/sails/base.rb, line 76
def root=(root)
  @root = Pathname.new(root)
end
service() click to toggle source

Sails.service

return a instance of Sails Service layer

for example:

class UsersService < Sails::Service::Base
  def check_name_exist?(name)
    User.check_name_exist?(name)
  end
end

class UsersServiceTest
  def test_check_name_exist?
    assert_equal(Sails.service.check_name_exist?(name), true)
  end
end
# File lib/sails/base.rb, line 156
def service
  @service ||= Sails::Service::Interface.new
end
start!(type) click to toggle source
# File lib/sails/base.rb, line 178
def start!(type)
  logger.info "ENV: #{Sails.env}"

  @server_type = type
  if @server_type == "thread"
    start_thread_pool_server!
  else
    start_non_blocking_server!
  end
end
start_non_blocking_server!() click to toggle source

Start Thrift Server with Event drive mode

# File lib/sails/base.rb, line 222
def start_non_blocking_server!
  transport = ::Thrift::ServerSocket.new(nil, config.port)
  transport_factory = ::Thrift::FramedTransportFactory.new
  protocol_factory = thrift_protocol_class.new
  processor = config.processor.new(self.service)
  @server = ::Thrift::NonblockingServer.new(processor, transport, transport_factory, protocol_factory, config.thread_size)

  logger.info "Boot on: #{Sails.root}"
  logger.info "[#{Time.now}] Starting the Sails with NonBlocking..."
  logger.info "Protocol: #{thrift_protocol_class.name}"
  logger.info "serve: 127.0.0.1:#{config.port}"

  begin
    @server.serve
  rescue => e
    puts "Start thrift server exception! \n  #{e.inspect}"
    puts e.backtrace
  end
end
start_thread_pool_server!() click to toggle source

Start Thrift Server with Threaded mode

# File lib/sails/base.rb, line 202
def start_thread_pool_server!
  transport = ::Thrift::ServerSocket.new(nil, config.thread_port)
  transport_factory = ::Thrift::BufferedTransportFactory.new
  protocol_factory = thrift_protocol_class.new
  processor = config.processor.new(self.service)
  @server = ::Thrift::ThreadPoolServer.new(processor, transport, transport_factory, protocol_factory, config.thread_size)

  logger.info "Boot on: #{Sails.root}"
  logger.info "[#{Time.now}] Starting the Sails with ThreadPool size: #{Setting.pool_size}..."
  logger.info "serve: 127.0.0.1:#{config.thread_port}"

  begin
    @server.serve
  rescue => e
    puts "Start thrift server exception! \n  #{e.inspect}"
    puts e.backtrace
  end
end
thrift_protocol_class() click to toggle source
# File lib/sails/base.rb, line 189
def thrift_protocol_class
  case config.protocol
  when :compact
    return ::Thrift::CompactProtocolFactory
  when :json
    return ::Thrift::JsonProtocolFactory
  else
    return ::Thrift::BinaryProtocolFactory
  end
end
version() click to toggle source
# File lib/sails/version.rb, line 2
def self.version
  "0.2.3"
end