Module: Sails

Extended by:
ActiveSupport::Autoload
Defined in:
lib/sails.rb,
lib/sails/cli.rb,
lib/sails/base.rb,
lib/sails/config.rb,
lib/sails/daemon.rb,
lib/sails/service.rb,
lib/sails/version.rb,
lib/sails/console.rb,
lib/sails/service/base.rb,
lib/sails/log_subscriber.rb,
lib/sails/service/callbacks.rb,
lib/sails/service/exception.rb,
lib/sails/service/interface.rb,
lib/sails/templates/config/environments/test.rb,
lib/sails/templates/config/environments/production.rb,
lib/sails/templates/config/environments/development.rb

Overview

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

Defined Under Namespace

Modules: ConsoleMethods, Service Classes: CLI, Config, Console, Daemon, LogSubscriber

Class Method Summary collapse

Class Method Details

.cacheObject

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.


55
56
57
58
# 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_dirsObject


248
249
250
251
252
253
254
255
256
257
# 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

.configObject

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'

23
24
25
26
# File 'lib/sails/base.rb', line 23

def config
  return @config if defined?(@config)
  @config = Config.new.config
end

.envObject

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

92
93
94
# File 'lib/sails/base.rb', line 92

def env
  @env ||= ActiveSupport::StringInquirer.new(ENV['RAILS_ENV'].presence || 'development')
end

.initObject


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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_initializeObject


242
243
244
245
246
# File 'lib/sails/base.rb', line 242

def load_initialize
  Dir["#{Sails.root}/config/initializers/*.rb"].each do |f|
    require f
  end
end

.loggerObject

Sails.logger

returns a Logger class For example:

Sails.logger.info "Hello world"
Sails.logger.error "Hello world"

104
105
106
107
108
109
110
111
# 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_pathObject


113
114
115
# File 'lib/sails/base.rb', line 113

def logger_path
  @logger_path ||= Sails.root.join("log/#{self.env}.log")
end

.reload!(opts = {}) ⇒ Object

Force reload Sails cache classes in config.autoload_paths


161
162
163
164
165
166
167
168
169
# 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!Object


171
172
173
174
175
176
# 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

.rootObject

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>

70
71
72
73
74
# 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) ⇒ Object


76
77
78
# File 'lib/sails/base.rb', line 76

def root=(root)
  @root = Pathname.new(root)
end

.serviceObject

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

156
157
158
# File 'lib/sails/base.rb', line 156

def service
  @service ||= Sails::Service::Interface.new
end

.start!(type) ⇒ Object


178
179
180
181
182
183
184
185
186
187
# 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!Object

Start Thrift Server with Event drive mode


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# 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!Object

Start Thrift Server with Threaded mode


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# 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_classObject


189
190
191
192
193
194
195
196
197
198
# 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

.versionObject


2
3
4
# File 'lib/sails/version.rb', line 2

def self.version
  "0.1.5"
end