sails 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9910e5d27ef628ca22aec968edd2ed2a94604295
4
- data.tar.gz: 6e72d2491ad1950a6a2e6946205a6ac84e266914
3
+ metadata.gz: fdca82274162d55762d2eddeb183930b7e0b77a5
4
+ data.tar.gz: 6008de27f43aed4eec5c4d2935abb318a5e64f46
5
5
  SHA512:
6
- metadata.gz: 91c9dc674800da1fcb081bafa44438b5a673f68300df5ddbfdbc207b1c216de6354972a9dcb9866db5af245bf18dbb2d51b983b85c9c89c53e52768116561840
7
- data.tar.gz: df6d3b9110cf3d8c3c71e31ae5c0f0c9e0744b2cd6826dd98517356b0e8b2835637f5565904e186e292aa118d33d8df7d59a7326280ec5ad1e7ee44688cd9c7e
6
+ metadata.gz: 0a8a8dedb82895595fa18613dd058ed6c4f91f8db83cd10d8e3a02a51d85c3490c5a364d38cfa1c8f1ed55c7dd2535763b593223c61bd62d54d092a6758cba53
7
+ data.tar.gz: e7ee9fd640638548ed95a81a65bf1443c84fb09531d74c8cf70397f233e022979d4b4c16780400491a2885e900c14eec7f7944b6fc23d27607798f561f8d93c8
data/.gitignore CHANGED
@@ -8,6 +8,7 @@
8
8
  /test/tmp/
9
9
  /test/version_tmp/
10
10
  /tmp/
11
+ log/*
11
12
 
12
13
  ## Specific to RubyMotion:
13
14
  .dat*
data/CHANGELOG.md CHANGED
@@ -1,4 +1,11 @@
1
- ## 0.1.1
1
+ ## 0.1.2 / 2014-12-17
2
+
3
+ - `sails restart` use kill -USR2 signal, not kill master process.
4
+ - Use tail log file to instead of direct stdout.
5
+ - Add `before_action` support for Service layout;
6
+ - Add `params` like same name in ActionController for Service layout;
7
+
8
+ ## 0.1.1 / 2014-12-10
2
9
 
3
10
  - Add `sails s`, `sails c` commands.
4
11
  - Refactor service layer, use class to instead module.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Sails
2
2
  =====
3
3
 
4
- Sails, create [Thrift](thrift.apache.org) app server like Rails.
4
+ Sails, create [Thrift](http://thrift.apache.org) app server like Rails.
5
5
 
6
6
  [![Build Status](https://travis-ci.org/huacnlee/sails.svg)](https://travis-ci.org/huacnlee/sails) [![Gem Version](https://badge.fury.io/rb/sails.svg)](http://badge.fury.io/rb/sails)
7
7
 
data/lib/sails/base.rb CHANGED
@@ -2,7 +2,7 @@ Bundler.require()
2
2
 
3
3
  module Sails
4
4
  extend ActiveSupport::Autoload
5
-
5
+
6
6
  autoload :Config
7
7
 
8
8
  # Sails.config
@@ -69,7 +69,7 @@ module Sails
69
69
  def self.root
70
70
  @root ||= Pathname.new(Dir.pwd)
71
71
  end
72
-
72
+
73
73
  def self.root=(root)
74
74
  @root = Pathname.new(root)
75
75
  end
@@ -100,18 +100,18 @@ module Sails
100
100
  #
101
101
  def self.logger
102
102
  return @logger if defined?(@logger)
103
- @logger = Logger.new(File.join(Sails.root, "log/#{self.env}.log"))
103
+ log_file = File.join(Sails.root, "log/#{self.env}.log")
104
+ @logger = Logger.new(log_file)
104
105
  @logger.formatter = proc { |severity, datetime, progname, msg|
105
- self.stdout_logger.info msg if !Sails.env.test?
106
106
  "#{msg}\n"
107
107
  }
108
108
  @logger
109
109
  end
110
110
 
111
111
  def self.init
112
- $:.unshift self.root.join("lib")
113
112
  # init root
114
113
  return false if @inited == true
114
+ $:.unshift self.root.join("lib")
115
115
 
116
116
  self.root
117
117
 
@@ -124,8 +124,6 @@ module Sails
124
124
 
125
125
  require "sails/service"
126
126
 
127
- puts "ENV: #{Sails.env}"
128
-
129
127
  load_initialize
130
128
  @inited = true
131
129
  end
@@ -144,22 +142,23 @@ module Sails
144
142
  #
145
143
  # class UsersServiceTest
146
144
  # def test_check_name_exist?
147
- # assert_equal(Sails.service.check_name_exist?(name), true)
145
+ # assert_equal(Sails.service.check_name_exist?(name), true)
148
146
  # end
149
147
  # end
150
148
  #
151
149
  def self.service
152
150
  @service ||= Sails::Service::Interface.new
153
151
  end
154
-
152
+
155
153
  # Force reload Sails cache classes in config.autoload_paths
156
154
  def self.reload!(opts = {})
157
155
  force = opts[:force] || false
158
156
  if force || config.cache_classes == false
159
- @service = nil
157
+ # @service = nil
160
158
  ActiveSupport::Dependencies.clear
161
- reload_server!
159
+ # reload_server!
162
160
  end
161
+ return true
163
162
  end
164
163
 
165
164
  def self.reload_server!
@@ -168,8 +167,10 @@ module Sails
168
167
  @server.instance_variable_set(:@processor, new_processor)
169
168
  end
170
169
  end
171
-
170
+
172
171
  def self.start!(type)
172
+ logger.info "ENV: #{Sails.env}"
173
+
173
174
  @server_type = type
174
175
  if @server_type == "thread"
175
176
  start_thread_pool_server!
@@ -177,14 +178,14 @@ module Sails
177
178
  start_non_blocking_server!
178
179
  end
179
180
  end
180
-
181
+
181
182
  def self.thrift_protocol_class
182
183
  case config.protocol
183
184
  when :compact
184
185
  return ::Thrift::CompactProtocolFactory
185
186
  when :json
186
187
  return ::Thrift::JsonProtocolFactory
187
- else
188
+ else
188
189
  return ::Thrift::BinaryProtocolFactory
189
190
  end
190
191
  end
@@ -198,20 +199,15 @@ module Sails
198
199
  processor = config.processor.new(self.service)
199
200
  @server = ::Thrift::ThreadPoolServer.new(processor, transport, transport_factory, protocol_factory, config.thread_size)
200
201
 
201
- puts "Boot on: #{Sails.root}"
202
- puts "[#{Time.now}] Starting the Sails with ThreadPool size: #{Setting.pool_size}..."
203
- puts "serve: 127.0.0.1:#{config.thread_port}"
202
+ logger.info "Boot on: #{Sails.root}"
203
+ logger.info "[#{Time.now}] Starting the Sails with ThreadPool size: #{Setting.pool_size}..."
204
+ logger.info "serve: 127.0.0.1:#{config.thread_port}"
204
205
 
205
206
  begin
206
207
  @server.serve
207
208
  rescue => e
208
209
  puts "Start thrift server exception! \n #{e.inspect}"
209
210
  puts e.backtrace
210
-
211
- if self.env != "development"
212
- sleep 2
213
- retry
214
- end
215
211
  end
216
212
  end
217
213
 
@@ -223,33 +219,18 @@ module Sails
223
219
  processor = config.processor.new(self.service)
224
220
  @server = ::Thrift::NonblockingServer.new(processor, transport, transport_factory, protocol_factory, config.thread_size)
225
221
 
226
- puts "Boot on: #{Sails.root}"
227
- puts "[#{Time.now}] Starting the Sails with NonBlocking..."
228
- puts "Protocol: #{thrift_protocol_class.name}"
229
- puts "serve: 127.0.0.1:#{config.port}"
222
+ logger.info "Boot on: #{Sails.root}"
223
+ logger.info "[#{Time.now}] Starting the Sails with NonBlocking..."
224
+ logger.info "Protocol: #{thrift_protocol_class.name}"
225
+ logger.info "serve: 127.0.0.1:#{config.port}"
230
226
 
231
227
  begin
232
228
  @server.serve
233
229
  rescue => e
234
230
  puts "Start thrift server exception! \n #{e.inspect}"
235
231
  puts e.backtrace
236
-
237
- if self.env != "development"
238
- sleep 2
239
- retry
240
- end
241
232
  end
242
233
  end
243
-
244
- private
245
- def self.stdout_logger
246
- return @stdout_logger if defined?(@stdout_logger)
247
- @stdout_logger = Logger.new(STDOUT)
248
- @stdout_logger.formatter = proc { |severity, datetime, progname, msg|
249
- "#{msg}\n"
250
- }
251
- @stdout_logger
252
- end
253
234
 
254
235
  def self.load_initialize
255
236
  Dir["#{Sails.root}/config/initializers/*.rb"].each do |f|
@@ -258,4 +239,4 @@ module Sails
258
239
  end
259
240
  end
260
241
 
261
- Sails.init()
242
+ Sails.init()
data/lib/sails/cli.rb CHANGED
@@ -47,8 +47,7 @@ module Sails
47
47
  desc "restart", "Restart Thrift server"
48
48
  def restart()
49
49
  Sails::Daemon.init(mode: options[:mode])
50
- Sails::Daemon.stop_process
51
- Sails::Daemon.start_process(daemon: true)
50
+ Sails::Daemon.restart_process
52
51
  end
53
52
 
54
53
  desc "new APP_NAME", "Create a project"
data/lib/sails/daemon.rb CHANGED
@@ -28,7 +28,7 @@ module Sails
28
28
  def start_process(options = {})
29
29
  old_pid = read_pid
30
30
  if old_pid != nil
31
- puts "Current have #{app_name} process in running on pid #{old_pid}"
31
+ Sails.logger.info "Current have #{app_name} process in running on pid #{old_pid}"
32
32
  return
33
33
  end
34
34
 
@@ -37,28 +37,41 @@ module Sails
37
37
  File.open(pid_file, "w+") do |f|
38
38
  f.puts @master_pid
39
39
  end
40
-
41
- puts "Started #{app_name} on pid: #{@master_pid}"
40
+
41
+ Sails.logger.info "Started #{app_name} on pid: #{@master_pid}"
42
42
 
43
43
  if options[:daemon] == false
44
+ log_file = Sails.root.join("log/#{Sails.env}.log")
45
+ system "tail -f #{log_file}"
44
46
  Process.waitpid(@master_pid)
45
47
  end
46
48
  end
47
-
49
+
50
+ def restart_process(options = {})
51
+ old_pid = read_pid
52
+ if old_pid == nil
53
+ Sails.logger.info "#{app_name} process not found on pid #{old_pid}"
54
+ return
55
+ end
56
+
57
+ Process.kill("USR2", old_pid)
58
+ end
59
+
48
60
  def fork_master_process!
49
61
  fork do
50
62
  $PROGRAM_NAME = self.app_name + " [master]"
51
63
  @child_pid = fork_child_process!
52
-
53
- Signal.trap("QUIT") {
64
+
65
+ Signal.trap("QUIT") {
54
66
  Process.kill("QUIT", @child_pid)
55
67
  exit
56
68
  }
57
-
69
+
58
70
  Signal.trap("USR2") {
59
- Process.kill("USR2", @child_pid)
71
+ puts @child_pid.inspect
72
+ Process.kill("QUIT", @child_pid)
60
73
  }
61
-
74
+
62
75
  loop do
63
76
  sleep 1
64
77
  begin
@@ -70,34 +83,34 @@ module Sails
70
83
  end
71
84
  end
72
85
  end
73
-
86
+
74
87
  def fork_child_process!
75
88
  fork do
76
89
  $PROGRAM_NAME = self.app_name
77
90
  Sails.start!(self.mode)
78
-
91
+
79
92
  Signal.trap("USR2") {
80
93
  # TODO: reload Sails in current process
81
94
  exit
82
95
  }
83
96
  end
84
97
  end
85
-
98
+
86
99
  def stop_process
87
100
  pid = read_pid
88
101
  if pid == nil
89
- puts "#{app_name} process not found, pid #{pid}"
102
+ Sails.logger.info "#{app_name} process not found, pid #{pid}"
90
103
  return
91
104
  end
92
105
 
93
- print "Stopping #{app_name} with pid: #{pid}..."
106
+ Sails.logger.info "Stopping #{app_name} with pid: #{pid}..."
94
107
  begin
95
108
  Process.kill("QUIT", pid)
96
109
  ensure
97
110
  File.delete(pid_file)
98
111
  end
99
- puts " [Done]"
112
+ Sails.logger.info " [Done]"
100
113
  end
101
114
  end
102
115
  end
103
- end
116
+ end
@@ -2,18 +2,7 @@ module Sails
2
2
  module Service
3
3
  # Like ActionController::Base
4
4
  class Base
5
- include ActiveSupport::Callbacks
6
-
7
- define_callbacks :action
8
-
9
- set_callback :action, :before do |object|
10
- # TODO: only reload on files changed
11
- Sails.reload!
12
- end
13
-
14
- set_callback :action, :after do |object|
15
- ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base)
16
- end
5
+ include Callbacks
17
6
 
18
7
  class << self
19
8
  def internal_methods
@@ -36,6 +25,22 @@ module Sails
36
25
  end
37
26
  end
38
27
 
28
+ # action params to Hash
29
+ #
30
+ # example:
31
+ #
32
+ # class FooService < Sails::Service::Base
33
+ # def foo(name, age)
34
+ # # you can use params in any instance methods
35
+ # puts params[:name]
36
+ # puts params[:age]
37
+ # end
38
+ # end
39
+ #
40
+ def params
41
+ @params ||= {}
42
+ end
43
+
39
44
  # Raise a Sails::Service::Exception (Thrift::Exception)
40
45
  # if you want custom error you can override this method in you ApplicationService
41
46
  def raise_error(code, msg = nil)
@@ -0,0 +1,28 @@
1
+ module Sails
2
+ module Service
3
+ module Callbacks
4
+ extend ActiveSupport::Concern
5
+
6
+ include ActiveSupport::Callbacks
7
+
8
+ included do
9
+ define_callbacks :action
10
+
11
+ set_callback :action, :before do |object|
12
+ end
13
+
14
+ set_callback :action, :after do |object|
15
+ ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base)
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ def before_action(*names, &blk)
21
+ names.each do |name|
22
+ set_callback(:action, :before, name, &blk)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -27,7 +27,15 @@ module Sails
27
27
  end
28
28
  end
29
29
 
30
+ def set_params_with_method_args(instance, method_name, args)
31
+ method_args = instance.method(method_name.to_sym).parameters.map { |arg| arg[1] }
32
+ method_args.each_with_index do |arg, idx|
33
+ instance.params[arg] = args[idx]
34
+ end
35
+ end
36
+
30
37
  def run_action(instance, method_name, *args, &block)
38
+ set_params_with_method_args(instance, method_name, args)
31
39
  instance.run_callbacks :action do
32
40
  time = Time.now.to_f
33
41
 
@@ -38,17 +46,22 @@ module Sails
38
46
  res = instance.send(method_name, *args, &block)
39
47
  status = "Completed"
40
48
  return res
41
- rescue ActiveRecord::RecordNotFound => e
42
- status = "Not Found"
43
- instance.raise_error(404)
44
49
  rescue Thrift::Exception => e
45
50
  status = "Failed #{e.try(:code)}"
46
51
  raise e
47
52
  rescue => e
48
- status = "Error 500"
53
+ puts "------- #{e.inspect}"
54
+ if defined?(ActiveRecord) && e.is_a?(ActiveRecord::RecordNotFound)
55
+ status = "Not Found"
56
+ code = 404
57
+ else
58
+ status = "Error 500"
59
+ code = 500
60
+ end
61
+
49
62
  Sails.logger.info "\"#{method_name}\" error : #{e.inspect}\n\n"
50
63
  Sails.logger.info %Q(backtrace: #{e.backtrace.join("\n")}\n)
51
- instance.raise_error(500)
64
+ instance.raise_error(code)
52
65
  ensure
53
66
  elapsed = format('%.3f', (Time.now.to_f - time) * 1000)
54
67
  Sails.logger.info "#{status} in (#{elapsed}ms).\n\n" unless Sails.env.test?
data/lib/sails/service.rb CHANGED
@@ -6,5 +6,6 @@ module Sails
6
6
  autoload :Config
7
7
  autoload :Exception
8
8
  autoload :Interface
9
+ autoload :Callbacks
9
10
  end
10
11
  end
data/lib/sails/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Sails
2
2
  def self.version
3
- "0.1.1"
3
+ "0.1.2"
4
4
  end
5
5
  end
data/spec/cli_spec.rb CHANGED
@@ -24,8 +24,7 @@ describe 'Sails::CLI' do
24
24
  it { expect(cli).to respond_to(:restart) }
25
25
  it {
26
26
  # expect(Sails::Daemon).to receive(:init)
27
- expect(Sails::Daemon).to receive(:stop_process)
28
- expect(Sails::Daemon).to receive(:start_process)
27
+ expect(Sails::Daemon).to receive(:restart_process)
29
28
  cli.restart
30
29
  }
31
30
  end