sails 0.1.4 → 0.1.5
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 +4 -4
- data/.gitignore +2 -16
- data/CHANGELOG.md +13 -0
- data/Gemfile.lock +2 -2
- data/README.md +37 -11
- data/lib/sails/base.rb +225 -208
- data/lib/sails/cli.rb +13 -14
- data/lib/sails/config.rb +5 -5
- data/lib/sails/console.rb +9 -9
- data/lib/sails/daemon.rb +72 -27
- data/lib/sails/log_subscriber.rb +40 -0
- data/lib/sails/rails.rb +5 -1
- data/lib/sails/service/base.rb +8 -8
- data/lib/sails/service/callbacks.rb +2 -3
- data/lib/sails/service/interface.rb +34 -27
- data/lib/sails/service.rb +2 -2
- data/lib/sails/tasks.rb +61 -0
- data/lib/sails/templates/Gemfile +4 -4
- data/lib/sails/templates/Rakefile.tt +1 -41
- data/lib/sails/templates/app/services/application_service.rb.tt +6 -2
- data/lib/sails/templates/app/services/gen-rb/%app_name%.rb.tt +78 -3
- data/lib/sails/templates/app/services/gen-rb/%app_name%_constants.rb.tt +11 -0
- data/lib/sails/templates/app/services/gen-rb/%app_name%_types.rb.tt +46 -0
- data/lib/sails/templates/config/application.rb.tt +8 -8
- data/lib/sails/templates/config/{database.yml → database.yml.tt} +2 -5
- data/lib/sails/templates/config/initializers/active_record.rb +6 -0
- data/lib/sails/templates/config/locales/rails.zh-CN.yml +5 -5
- data/lib/sails/templates/config/locales/zh-CN.yml +1 -2
- data/lib/sails/templates/lib/tasks/client.rake.tt +25 -0
- data/lib/sails/version.rb +2 -2
- data/lib/sails.rb +12 -6
- data/sails.gemspec +1 -1
- data/spec/cli_spec.rb +6 -6
- data/spec/dummy/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/tmp/.keep +0 -0
- data/spec/dummy/tmp/cache/.keep +0 -0
- data/spec/dummy/tmp/pids/.keep +0 -0
- data/spec/rails_spec.rb +1 -0
- data/spec/sails_spec.rb +39 -11
- data/spec/spec_helper.rb +1 -1
- data/spec/support/service_support.rb +2 -2
- metadata +28 -8
- data/lib/sails/templates/Gemfile.lock +0 -45
data/lib/sails/cli.rb
CHANGED
@@ -4,7 +4,7 @@ require 'sails/version'
|
|
4
4
|
module Sails
|
5
5
|
class CLI < Thor
|
6
6
|
include Thor::Actions
|
7
|
-
|
7
|
+
|
8
8
|
map '-v' => :version
|
9
9
|
map 's' => :start
|
10
10
|
map 'c' => :console
|
@@ -12,12 +12,12 @@ module Sails
|
|
12
12
|
def self.source_root
|
13
13
|
__dir__
|
14
14
|
end
|
15
|
-
|
16
|
-
no_commands
|
15
|
+
|
16
|
+
no_commands do
|
17
17
|
def app_name
|
18
18
|
@app_name
|
19
19
|
end
|
20
|
-
|
20
|
+
end
|
21
21
|
|
22
22
|
# sails start
|
23
23
|
#
|
@@ -25,9 +25,9 @@ module Sails
|
|
25
25
|
option :daemon, type: :boolean, default: false
|
26
26
|
option :mode, default: 'nonblocking'
|
27
27
|
desc "start", "Start Thrift server"
|
28
|
-
def start
|
29
|
-
Sails::Daemon.init(mode: options[:mode])
|
30
|
-
Sails::Daemon.start_process
|
28
|
+
def start
|
29
|
+
Sails::Daemon.init(mode: options[:mode], daemon: options[:daemon])
|
30
|
+
Sails::Daemon.start_process
|
31
31
|
end
|
32
32
|
|
33
33
|
# sails stop
|
@@ -35,29 +35,28 @@ module Sails
|
|
35
35
|
# Stop thrift app server
|
36
36
|
option :mode, default: 'nonblocking'
|
37
37
|
desc "stop", "Stop Thrift server"
|
38
|
-
def stop
|
38
|
+
def stop
|
39
39
|
Sails::Daemon.init(mode: options[:mode])
|
40
40
|
Sails::Daemon.stop_process
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
# sails restart
|
44
44
|
#
|
45
45
|
# Restart thrift app server
|
46
46
|
option :mode, default: 'nonblocking'
|
47
47
|
desc "restart", "Restart Thrift server"
|
48
|
-
def restart
|
48
|
+
def restart
|
49
49
|
Sails::Daemon.init(mode: options[:mode])
|
50
50
|
Sails::Daemon.restart_process
|
51
51
|
end
|
52
52
|
|
53
53
|
desc "new APP_NAME", "Create a project"
|
54
|
-
def new
|
54
|
+
def new(name)
|
55
55
|
require 'fileutils'
|
56
56
|
|
57
57
|
app_dir = File.expand_path File.join(Dir.pwd, name)
|
58
58
|
@rel_dir = name
|
59
59
|
@app_name = File.basename app_dir
|
60
|
-
templte_dir = File.join(File.dirname(__FILE__), "templates")
|
61
60
|
|
62
61
|
directory 'templates', name
|
63
62
|
%W(log tmp/pids tmp/cache lib/tasks app/models/concerns config/initializers log).each do |dir_name|
|
@@ -68,12 +67,12 @@ module Sails
|
|
68
67
|
@app_name = nil
|
69
68
|
@rel_dir = nil
|
70
69
|
end
|
71
|
-
|
70
|
+
|
72
71
|
desc "console", "Enter Sails console"
|
73
72
|
def console
|
74
73
|
Sails::Console.start(Sails.root.join("config/application.rb"))
|
75
74
|
end
|
76
|
-
|
75
|
+
|
77
76
|
desc "version", "Show Sails version"
|
78
77
|
def version
|
79
78
|
puts "Sails #{Sails.version}"
|
data/lib/sails/config.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
module Sails
|
2
2
|
class Config
|
3
3
|
include ActiveSupport::Configurable
|
4
|
-
|
4
|
+
|
5
5
|
def initialize
|
6
6
|
init_defaults!
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def init_defaults!
|
10
10
|
config.app_name = "Sails"
|
11
11
|
config.cache_store = [:memory_store]
|
12
|
-
config.autoload_paths = %
|
12
|
+
config.autoload_paths = %w(app/models app/models/concerns app/workers app/services app/services/concerns lib)
|
13
13
|
config.i18n = I18n
|
14
14
|
config.i18n.load_path += Dir[Sails.root.join('config', 'locales', '*.{rb,yml}').to_s]
|
15
15
|
config.i18n.default_locale = :en
|
16
16
|
config.cache_classes = false
|
17
|
-
|
17
|
+
|
18
18
|
config.port = 4000
|
19
19
|
config.thread_port = 4001
|
20
20
|
config.processor = nil
|
@@ -22,4 +22,4 @@ module Sails
|
|
22
22
|
config.protocol = :binary
|
23
23
|
end
|
24
24
|
end
|
25
|
-
end
|
25
|
+
end
|
data/lib/sails/console.rb
CHANGED
@@ -4,32 +4,32 @@ require "irb/ext/loader"
|
|
4
4
|
module Sails
|
5
5
|
module ConsoleMethods
|
6
6
|
extend ActiveSupport::Concern
|
7
|
-
|
7
|
+
|
8
8
|
included do
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def service
|
12
12
|
Sails.service
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def reload!
|
16
16
|
puts "Reloading..."
|
17
17
|
Sails.reload!(force: true)
|
18
|
-
|
18
|
+
true
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
class Console
|
23
23
|
class << self
|
24
24
|
def start(app_path)
|
25
25
|
new(app_path).start
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def initialize(app_path)
|
30
30
|
@app_path = app_path
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def start
|
34
34
|
puts "Loading #{Sails.env} environment (Sails #{Sails.version})"
|
35
35
|
IRB.conf[:IRB_NAME] = "Sails console"
|
@@ -38,7 +38,7 @@ module Sails
|
|
38
38
|
if defined?(IRB::ExtendCommandBundle)
|
39
39
|
IRB::ExtendCommandBundle.send :include, Sails::ConsoleMethods
|
40
40
|
end
|
41
|
-
IRB.start
|
41
|
+
IRB.start
|
42
42
|
end
|
43
43
|
end
|
44
|
-
end
|
44
|
+
end
|
data/lib/sails/daemon.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
module Sails
|
2
2
|
class Daemon
|
3
3
|
class << self
|
4
|
-
attr_accessor :options, :mode, :app_name, :pid_file
|
4
|
+
attr_accessor :options, :daemon, :mode, :app_name, :pid_file
|
5
5
|
|
6
6
|
def init(opts = {})
|
7
7
|
self.app_name = Sails.config.app_name
|
8
8
|
self.mode = opts[:mode]
|
9
9
|
self.app_name = "#{Sails::Daemon.app_name}-thread" if self.mode == "thread"
|
10
10
|
self.pid_file = Sails.root.join("tmp/#{Sails::Daemon.app_name}.pid")
|
11
|
-
self.
|
11
|
+
self.daemon = opts[:daemon]
|
12
|
+
self.options = opts
|
12
13
|
end
|
13
14
|
|
14
15
|
def read_pid
|
@@ -22,13 +23,13 @@ module Sails
|
|
22
23
|
rescue
|
23
24
|
pid = nil
|
24
25
|
end
|
25
|
-
|
26
|
+
pid
|
26
27
|
end
|
27
28
|
|
28
|
-
def start_process
|
29
|
+
def start_process
|
29
30
|
old_pid = read_pid
|
30
|
-
if old_pid
|
31
|
-
|
31
|
+
if !old_pid.nil?
|
32
|
+
puts colorize("Current have #{app_name} process in running on pid #{old_pid}", :red)
|
32
33
|
return
|
33
34
|
end
|
34
35
|
|
@@ -38,11 +39,10 @@ module Sails
|
|
38
39
|
f.puts @master_pid
|
39
40
|
end
|
40
41
|
|
41
|
-
|
42
|
+
puts "Started #{app_name} on pid: #{@master_pid}"
|
43
|
+
# puts "in init: #{Sails.service.object_id}"
|
42
44
|
|
43
|
-
if
|
44
|
-
log_file = Sails.root.join("log/#{Sails.env}.log")
|
45
|
-
system "tail -f #{log_file}"
|
45
|
+
if not self.daemon
|
46
46
|
Process.waitpid(@master_pid)
|
47
47
|
else
|
48
48
|
exit
|
@@ -52,32 +52,35 @@ module Sails
|
|
52
52
|
def restart_process(options = {})
|
53
53
|
old_pid = read_pid
|
54
54
|
if old_pid == nil
|
55
|
-
|
55
|
+
puts colorize("#{app_name} process not found on pid #{old_pid}", :red)
|
56
56
|
return
|
57
57
|
end
|
58
58
|
|
59
|
+
print "Restarting #{app_name}..."
|
59
60
|
Process.kill("USR2", old_pid)
|
61
|
+
puts colorize(" [OK]", :green)
|
60
62
|
end
|
61
63
|
|
62
64
|
def fork_master_process!
|
63
65
|
fork do
|
66
|
+
# WARN: DO NOT CALL Sails IN THIS BLOCK!
|
64
67
|
$PROGRAM_NAME = self.app_name + " [sails master]"
|
65
68
|
@child_pid = fork_child_process!
|
66
69
|
|
67
|
-
Signal.trap("QUIT")
|
70
|
+
Signal.trap("QUIT") do
|
68
71
|
Process.kill("QUIT", @child_pid)
|
69
72
|
exit
|
70
|
-
|
73
|
+
end
|
71
74
|
|
72
|
-
Signal.trap("USR2")
|
75
|
+
Signal.trap("USR2") do
|
73
76
|
Process.kill("USR2", @child_pid)
|
74
|
-
|
77
|
+
end
|
75
78
|
|
76
79
|
loop do
|
77
80
|
sleep 1
|
78
81
|
begin
|
79
82
|
Process.getpgid(@child_pid)
|
80
|
-
rescue Errno::ESRCH
|
83
|
+
rescue Errno::ESRCH
|
81
84
|
@child_pid = fork_child_process!
|
82
85
|
end
|
83
86
|
end
|
@@ -87,35 +90,77 @@ module Sails
|
|
87
90
|
def fork_child_process!
|
88
91
|
pid = fork do
|
89
92
|
$PROGRAM_NAME = self.app_name + " [sails child]"
|
90
|
-
Signal.trap("QUIT")
|
93
|
+
Signal.trap("QUIT") do
|
91
94
|
exit
|
92
|
-
|
93
|
-
|
94
|
-
Signal.trap("USR2")
|
95
|
+
end
|
96
|
+
|
97
|
+
Signal.trap("USR2") do
|
95
98
|
# TODO: reload Sails in current process
|
96
99
|
exit
|
97
|
-
|
98
|
-
|
100
|
+
end
|
101
|
+
|
102
|
+
if self.daemon == true
|
103
|
+
redirect_stdout
|
104
|
+
else
|
105
|
+
log_to_stdout
|
106
|
+
end
|
107
|
+
# puts "in child: #{Sails.service.object_id}"
|
108
|
+
Sails.start!(self.mode)
|
99
109
|
end
|
100
110
|
# http://ruby-doc.org/core-1.9.3/Process.html#detach-method
|
101
111
|
Process.detach(pid)
|
102
|
-
|
112
|
+
pid
|
103
113
|
end
|
104
114
|
|
105
115
|
def stop_process
|
106
116
|
pid = read_pid
|
107
|
-
if pid
|
108
|
-
|
117
|
+
if pid.nil?
|
118
|
+
puts colorize("#{app_name} process not found, pid #{pid}", :red)
|
109
119
|
return
|
110
120
|
end
|
111
121
|
|
112
|
-
|
122
|
+
print "Stoping #{app_name}..."
|
113
123
|
begin
|
114
124
|
Process.kill("QUIT", pid)
|
115
125
|
ensure
|
116
126
|
File.delete(pid_file)
|
117
127
|
end
|
118
|
-
|
128
|
+
puts colorize(" [OK]", :green)
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
# Sync Sails.logger output in file to STDOUT
|
133
|
+
def log_to_stdout
|
134
|
+
console = ActiveSupport::Logger.new($stdout)
|
135
|
+
console.formatter = Sails.logger.formatter
|
136
|
+
console.level = Sails.logger.level
|
137
|
+
|
138
|
+
Sails.logger.extend(ActiveSupport::Logger.broadcast(console))
|
139
|
+
end
|
140
|
+
|
141
|
+
# Redirect stdout, stderr to log file,
|
142
|
+
# If we not do this, stdout will block sails daemon, for example `puts`.
|
143
|
+
def redirect_stdout
|
144
|
+
redirect_io($stdout, Sails.logger_path)
|
145
|
+
redirect_io($stderr, Sails.logger_path)
|
146
|
+
end
|
147
|
+
|
148
|
+
def redirect_io(io, path)
|
149
|
+
File.open(path, 'ab') { |fp| io.reopen(fp) } if path
|
150
|
+
io.sync = true
|
151
|
+
end
|
152
|
+
|
153
|
+
def colorize(text, c)
|
154
|
+
case c
|
155
|
+
when :red
|
156
|
+
return ["\033[31m",text,"\033[0m"].join("")
|
157
|
+
when :green
|
158
|
+
return ["\033[32m",text,"\033[0m"].join("")
|
159
|
+
when :blue
|
160
|
+
return ["\033[34m",text,"\033[0m"].join("")
|
161
|
+
else
|
162
|
+
return text
|
163
|
+
end
|
119
164
|
end
|
120
165
|
end
|
121
166
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Sails
|
2
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
3
|
+
INTERNAL_PARAMS = %w(controller action format _method only_path)
|
4
|
+
|
5
|
+
def start_processing(event)
|
6
|
+
return unless logger.info?
|
7
|
+
|
8
|
+
payload = event.payload
|
9
|
+
params = payload[:params]
|
10
|
+
|
11
|
+
info ""
|
12
|
+
info "Processing by #{payload[:controller]}##{payload[:action]} at #{Time.now}"
|
13
|
+
info " Parameters: #{params.inspect}" unless params.empty?
|
14
|
+
|
15
|
+
ActiveRecord::LogSubscriber.reset_runtime if defined?(ActiveRecord::LogSubscriber)
|
16
|
+
end
|
17
|
+
|
18
|
+
def process_action(event)
|
19
|
+
info do
|
20
|
+
payload = event.payload
|
21
|
+
additions = []
|
22
|
+
|
23
|
+
additions << ("DB: %.1fms" % payload[:db_runtime].to_f) if payload[:db_runtime]
|
24
|
+
additions << ("Views: %.1fms" % payload[:view_runtime].to_f) if payload[:view_runtime]
|
25
|
+
status = payload[:status]
|
26
|
+
payload[:runtime] = event.duration
|
27
|
+
|
28
|
+
message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round(2)}ms"
|
29
|
+
message << " (#{additions.join(" | ")})"
|
30
|
+
message
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def logger
|
35
|
+
Sails.logger
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
Sails::LogSubscriber.attach_to :sails
|
data/lib/sails/rails.rb
CHANGED
data/lib/sails/service/base.rb
CHANGED
@@ -3,13 +3,13 @@ module Sails
|
|
3
3
|
# Like ActionController::Base
|
4
4
|
class Base
|
5
5
|
include Callbacks
|
6
|
-
|
6
|
+
|
7
7
|
class << self
|
8
8
|
def internal_methods
|
9
9
|
controller = self.superclass
|
10
10
|
controller.public_instance_methods(true)
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def action_methods
|
14
14
|
@action_methods ||= begin
|
15
15
|
# All public instance methods of this class, including ancestors
|
@@ -17,18 +17,18 @@ module Sails
|
|
17
17
|
# Except for public instance methods of Base and its ancestors
|
18
18
|
internal_methods +
|
19
19
|
# Be sure to include shadowed public instance methods of this class
|
20
|
-
public_instance_methods(false)).uniq.map
|
20
|
+
public_instance_methods(false)).uniq.map(&:to_s)
|
21
21
|
|
22
22
|
# Clear out AS callback method pollution
|
23
23
|
Set.new(methods.reject { |method| method =~ /_one_time_conditions/ })
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
# action params to Hash
|
29
29
|
#
|
30
30
|
# example:
|
31
|
-
#
|
31
|
+
#
|
32
32
|
# class FooService < Sails::Service::Base
|
33
33
|
# def foo(name, age)
|
34
34
|
# # you can use params in any instance methods
|
@@ -40,13 +40,13 @@ module Sails
|
|
40
40
|
def params
|
41
41
|
@params ||= {}
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
# Raise a Sails::Service::Exception (Thrift::Exception)
|
45
45
|
# if you want custom error you can override this method in you ApplicationService
|
46
46
|
def raise_error(code, msg = nil)
|
47
47
|
raise Exception.new(code: code, message: msg)
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
def action_methods
|
51
51
|
self.class.action_methods
|
52
52
|
end
|
@@ -58,4 +58,4 @@ module Sails
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
61
|
-
end
|
61
|
+
end
|
@@ -2,9 +2,9 @@ module Sails
|
|
2
2
|
module Service
|
3
3
|
module Callbacks
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
include ActiveSupport::Callbacks
|
7
|
-
|
7
|
+
|
8
8
|
included do
|
9
9
|
define_callbacks :action
|
10
10
|
|
@@ -12,7 +12,6 @@ module Sails
|
|
12
12
|
end
|
13
13
|
|
14
14
|
set_callback :action, :after do |object|
|
15
|
-
ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base)
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
@@ -2,7 +2,7 @@ module Sails
|
|
2
2
|
module Service
|
3
3
|
class Interface
|
4
4
|
attr_accessor :services
|
5
|
-
|
5
|
+
|
6
6
|
def initialize
|
7
7
|
@services = []
|
8
8
|
Dir["#{Sails.root.join("app/services")}/*_service.rb"].each do |f|
|
@@ -26,9 +26,10 @@ module Sails
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def set_params_with_method_args(instance, method_name, args)
|
31
31
|
method_args = instance.method(method_name.to_sym).parameters.map { |arg| arg[1] }
|
32
|
+
instance.params[:method_name] = method_name
|
32
33
|
method_args.each_with_index do |arg, idx|
|
33
34
|
instance.params[arg] = args[idx]
|
34
35
|
end
|
@@ -37,36 +38,42 @@ module Sails
|
|
37
38
|
def run_action(instance, method_name, *args, &block)
|
38
39
|
set_params_with_method_args(instance, method_name, args)
|
39
40
|
instance.run_callbacks :action do
|
40
|
-
|
41
|
+
raw_payload = {
|
42
|
+
controller: instance.class.to_s,
|
43
|
+
action: method_name,
|
44
|
+
params: instance.params,
|
45
|
+
}
|
46
|
+
|
47
|
+
ActiveSupport::Notifications.instrument("start_processing.sails", raw_payload.dup)
|
48
|
+
ActiveSupport::Notifications.instrument("process_action.sails", raw_payload) do |payload|
|
49
|
+
begin
|
50
|
+
res = instance.send(method_name, *args, &block)
|
51
|
+
payload[:status] = 200
|
52
|
+
return res
|
53
|
+
rescue Thrift::Exception => e
|
54
|
+
payload[:status] = e.try(:code)
|
55
|
+
raise e
|
56
|
+
rescue => e
|
57
|
+
if e.class.to_s == "ActiveRecord::RecordNotFound"
|
58
|
+
payload[:status] = 404
|
59
|
+
else
|
60
|
+
payload[:status] = 500
|
41
61
|
|
42
|
-
|
43
|
-
|
62
|
+
Sails.logger.info " ERROR #{e.inspect} backtrace: \n #{e.backtrace.join("\n ")}"
|
63
|
+
end
|
44
64
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
if defined?(ActiveRecord) && e.is_a?(ActiveRecord::RecordNotFound)
|
54
|
-
status = "Not Found"
|
55
|
-
code = 404
|
56
|
-
else
|
57
|
-
status = "Error 500"
|
58
|
-
code = 500
|
65
|
+
instance.raise_error(payload[:status])
|
66
|
+
ensure
|
67
|
+
ActiveRecord::Base.clear_active_connections! if defined?(ActiveRecord::Base)
|
68
|
+
if defined?(ActiveRecord::RuntimeRegistry)
|
69
|
+
payload[:db_runtime] = ActiveRecord::RuntimeRegistry.sql_runtime || 0
|
70
|
+
else
|
71
|
+
payload[:db_runtime] = nil
|
72
|
+
end
|
59
73
|
end
|
60
|
-
|
61
|
-
Sails.logger.info "\"#{method_name}\" error : #{e.inspect}\n\n"
|
62
|
-
Sails.logger.info %Q(backtrace: #{e.backtrace.join("\n")}\n)
|
63
|
-
instance.raise_error(code)
|
64
|
-
ensure
|
65
|
-
elapsed = format('%.3f', (Time.now.to_f - time) * 1000)
|
66
|
-
Sails.logger.info "#{status} in (#{elapsed}ms).\n\n" unless Sails.env.test?
|
67
74
|
end
|
68
75
|
end
|
69
76
|
end
|
70
77
|
end
|
71
78
|
end
|
72
|
-
end
|
79
|
+
end
|
data/lib/sails/service.rb
CHANGED
data/lib/sails/tasks.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
if defined?(ActiveRecord::Base)
|
2
|
+
load 'active_record/railties/databases.rake'
|
3
|
+
|
4
|
+
class << ActiveRecord::Tasks::DatabaseTasks
|
5
|
+
def env
|
6
|
+
@env ||= Sails.env
|
7
|
+
end
|
8
|
+
|
9
|
+
def migrations_paths
|
10
|
+
[Sails.root.join("db/migrate")]
|
11
|
+
end
|
12
|
+
|
13
|
+
def database_configuration
|
14
|
+
YAML.load open(Sails.root.join('config/database.yml')).read
|
15
|
+
end
|
16
|
+
|
17
|
+
def db_dir
|
18
|
+
Sails.root.join("db")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
namespace :db do
|
24
|
+
namespace :migrate do
|
25
|
+
desc "Create new migration file. use `rake db:migrate:create NAME=create_users`"
|
26
|
+
task :create do
|
27
|
+
name = ENV['NAME']
|
28
|
+
abort("no NAME specified. use `rake db:migrate:create NAME=create_users`") if !name
|
29
|
+
|
30
|
+
migrations_dir = File.join("db", "migrate")
|
31
|
+
version = ENV["VERSION"] || Time.now.utc.strftime("%Y%m%d%H%M%S")
|
32
|
+
filename = "#{version}_#{name}.rb"
|
33
|
+
migration_name = name.gsub(/_(.)/) { $1.upcase }.gsub(/^(.)/) { $1.upcase }
|
34
|
+
|
35
|
+
FileUtils.mkdir_p(migrations_dir)
|
36
|
+
|
37
|
+
open(File.join(migrations_dir, filename), 'w') do |f|
|
38
|
+
f << (<<-EOS).gsub(" ", "")
|
39
|
+
class #{migration_name} < ActiveRecord::Migration
|
40
|
+
def self.change
|
41
|
+
end
|
42
|
+
end
|
43
|
+
EOS
|
44
|
+
end
|
45
|
+
puts filename
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "Generate code from thrift IDL file"
|
51
|
+
task :generate do
|
52
|
+
puts "Generating Thrift code..."
|
53
|
+
out = Sails.root.join("app/services/gen-rb")
|
54
|
+
|
55
|
+
cmd = "thrift --gen rb -out #{out} -strict #{Sails.config.app_name}.thrift"
|
56
|
+
puts "> #{cmd}"
|
57
|
+
system cmd
|
58
|
+
puts "[Done]"
|
59
|
+
end
|
60
|
+
|
61
|
+
task :gen => :generate
|
data/lib/sails/templates/Gemfile
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gem 'sails'
|
4
|
-
gem 'activerecord'
|
5
|
-
gem '
|
6
|
-
|
4
|
+
gem 'activerecord'
|
5
|
+
gem 'sqlite3'
|
6
|
+
gem 'eventmachine'
|
7
|
+
gem 'thrift'
|
7
8
|
|
8
9
|
group :development, :test do
|
9
10
|
# gem 'rspec'
|
10
11
|
# gem 'factory_girl'
|
11
|
-
# gem 'database_cleaner'
|
12
12
|
# gem 'capistrano'
|
13
13
|
end
|