uc 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/uc +20 -7
- data/lib/uc/event_stream.rb +2 -2
- data/lib/uc/paths.rb +2 -2
- data/lib/uc/server.rb +11 -4
- data/lib/uc/status.rb +32 -4
- data/lib/uc/templates/unicorn.erb +1 -2
- data/lib/uc/unicorn/api.rb +8 -0
- data/lib/uc/unicorn/init.rb +5 -1
- data/lib/uc/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fed4c2d361dbf1a6dd608eff3b44e40f236d565
|
4
|
+
data.tar.gz: dc26fe4d91b8cf96e9dbd1e57fe9868acb54b955
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39efff4eff670c6e8a1a87e801e3d478d792316ee9ccd7bdd5d6db00f32ac26a9762c0df889c5b43a4a51e50bbc8b8399191645dff3f2f005910c5a992f0ff33
|
7
|
+
data.tar.gz: 157870c3e3e8fac14cb918a270474591f053adc2c847e430ac074d77ae350e18afdffa0d1c274b254a213bec89cbc6ccac8baef9c7ebfa1151ea1b937b8273a2
|
data/bin/uc
CHANGED
@@ -19,10 +19,9 @@ opts_parser = OptionParser.new do |opts|
|
|
19
19
|
|
20
20
|
banner = []
|
21
21
|
banner << "Usage: uc [options] command"
|
22
|
-
banner << ""
|
23
|
-
banner << "
|
24
|
-
banner << "
|
25
|
-
banner << " rolling-restart "
|
22
|
+
banner << "Commands: start, stop, restart, status, "
|
23
|
+
banner << " rolling-restart, print-config "
|
24
|
+
banner << " "
|
26
25
|
|
27
26
|
opts.banner = banner.join("\n")
|
28
27
|
|
@@ -32,7 +31,7 @@ opts_parser = OptionParser.new do |opts|
|
|
32
31
|
options[:app_dir] = path
|
33
32
|
end
|
34
33
|
|
35
|
-
opts.on("-e", "--environment [RAILS_ENV]" , "Set rails env") do |e|
|
34
|
+
opts.on("-e", "--environment [RAILS_ENV]" , "Set rails env (default: production)") do |e|
|
36
35
|
options[:rails_env] = e
|
37
36
|
end
|
38
37
|
|
@@ -41,11 +40,22 @@ opts_parser = OptionParser.new do |opts|
|
|
41
40
|
exit
|
42
41
|
end
|
43
42
|
|
43
|
+
opts.on("--use-pid", "use pid instead of lock file to check server status") do
|
44
|
+
options[:use_pid] = true
|
45
|
+
end
|
46
|
+
|
44
47
|
opts.on("--debug", "Show debug messages") do
|
45
48
|
options[:debug] = true
|
46
49
|
logger.level = ::Logger::DEBUG
|
47
50
|
end
|
48
51
|
|
52
|
+
opts.on("--trace", "Show debug messages and exception stack trace") do
|
53
|
+
options[:debug] = true
|
54
|
+
options[:trace] = true
|
55
|
+
logger.level = ::Logger::DEBUG
|
56
|
+
end
|
57
|
+
|
58
|
+
|
49
59
|
opts.on_tail("-h", "--help", "Show this message") do
|
50
60
|
puts opts
|
51
61
|
exit
|
@@ -56,13 +66,16 @@ begin
|
|
56
66
|
opts_parser.parse!(ARGV)
|
57
67
|
app_dir = options[:app_dir] || Dir.pwd
|
58
68
|
server = ::Uc::Server.new(app_dir, rails_env: options[:rails_env], debug: options[:debug])
|
69
|
+
server.use_pid = options[:use_pid]
|
59
70
|
command = ARGV.shift
|
60
71
|
|
61
72
|
case command
|
62
|
-
when "start", "stop", "restart", "status"
|
73
|
+
when "start", "stop", "restart", "status"
|
63
74
|
server.send command.to_sym
|
64
75
|
when "rr", "rolling-restart"
|
65
76
|
server.rolling_restart
|
77
|
+
when "print-config"
|
78
|
+
server.print_config
|
66
79
|
when nil
|
67
80
|
logger.error "No command specified"
|
68
81
|
puts opts_parser
|
@@ -73,7 +86,7 @@ begin
|
|
73
86
|
end
|
74
87
|
|
75
88
|
rescue OptionParser::InvalidOption, OptionParser::MissingArgument, ::Uc::Error => e
|
76
|
-
if options[:
|
89
|
+
if options[:trace]
|
77
90
|
raise e
|
78
91
|
else
|
79
92
|
logger.error "#{e.message}"
|
data/lib/uc/event_stream.rb
CHANGED
@@ -43,7 +43,7 @@ module Uc
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
def
|
46
|
+
def expect(event_type, timeout: 30, recreate: true, &block)
|
47
47
|
mq.recreate if recreate
|
48
48
|
mq.clear
|
49
49
|
yield
|
@@ -52,7 +52,7 @@ module Uc
|
|
52
52
|
raise uc_error(e)
|
53
53
|
end
|
54
54
|
|
55
|
-
def
|
55
|
+
def expect_in_background(event_type, timeout: 30, recreate: true, &block)
|
56
56
|
begin
|
57
57
|
mq.recreate if recreate
|
58
58
|
mq.clear
|
data/lib/uc/paths.rb
CHANGED
data/lib/uc/server.rb
CHANGED
@@ -14,6 +14,7 @@ module Uc
|
|
14
14
|
include ::Uc::Logger
|
15
15
|
|
16
16
|
attr_reader :paths, :rails_env, :app_dir
|
17
|
+
attr_accessor :use_pid
|
17
18
|
|
18
19
|
def initialize(app_dir, rails_env: "production", debug: false)
|
19
20
|
@app_dir = app_dir
|
@@ -27,7 +28,7 @@ module Uc
|
|
27
28
|
puts server_status
|
28
29
|
return
|
29
30
|
end
|
30
|
-
event_stream.
|
31
|
+
event_stream.expect_in_background :fin do
|
31
32
|
cmd %{unicorn -c #{uconfig.path} -D -E #{rails_env} }, return_output: false,
|
32
33
|
error_msg: "error starting unicorn"
|
33
34
|
end
|
@@ -43,6 +44,8 @@ module Uc
|
|
43
44
|
end
|
44
45
|
|
45
46
|
def status
|
47
|
+
paths.validate_required
|
48
|
+
Dir.chdir app_dir
|
46
49
|
puts server_status
|
47
50
|
end
|
48
51
|
|
@@ -58,7 +61,7 @@ module Uc
|
|
58
61
|
start
|
59
62
|
return
|
60
63
|
end
|
61
|
-
event_stream.
|
64
|
+
event_stream.expect :fin do
|
62
65
|
Process.kill("USR2", server_status.pid)
|
63
66
|
end
|
64
67
|
end
|
@@ -74,7 +77,7 @@ module Uc
|
|
74
77
|
private
|
75
78
|
|
76
79
|
def server_status
|
77
|
-
@server_status ||= ::Uc::Status.new(unicorn_paths)
|
80
|
+
@server_status ||= ::Uc::Status.new(unicorn_paths, use_pid: use_pid)
|
78
81
|
end
|
79
82
|
|
80
83
|
def paths
|
@@ -99,6 +102,7 @@ module Uc
|
|
99
102
|
|
100
103
|
def init
|
101
104
|
paths.validate_required
|
105
|
+
Dir.chdir app_dir
|
102
106
|
lock.acquire
|
103
107
|
::Uc::Logger.event_queue = config.event_queue_name
|
104
108
|
config.load_env
|
@@ -106,7 +110,10 @@ module Uc
|
|
106
110
|
end
|
107
111
|
|
108
112
|
def init_once
|
109
|
-
@init_once ||=
|
113
|
+
@init_once ||= begin
|
114
|
+
init
|
115
|
+
true
|
116
|
+
end
|
110
117
|
end
|
111
118
|
|
112
119
|
end
|
data/lib/uc/status.rb
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
require 'uc/shell_helper'
|
2
|
+
require 'uc/logger'
|
2
3
|
|
3
4
|
module Uc
|
4
5
|
class Status
|
5
6
|
|
6
7
|
include ::Uc::ShellHelper
|
8
|
+
include ::Uc::Logger
|
7
9
|
|
8
10
|
attr_reader :paths
|
9
11
|
attr_accessor :use_pid
|
10
12
|
|
11
|
-
def initialize(unicorn_paths)
|
13
|
+
def initialize(unicorn_paths, use_pid: false)
|
12
14
|
@paths = unicorn_paths
|
15
|
+
@use_pid = use_pid
|
13
16
|
end
|
14
17
|
|
15
18
|
def running?
|
@@ -18,7 +21,18 @@ module Uc
|
|
18
21
|
end
|
19
22
|
|
20
23
|
def pid
|
21
|
-
|
24
|
+
pid = pid_from_file
|
25
|
+
if pid_valid?
|
26
|
+
return pid
|
27
|
+
else
|
28
|
+
logger.debug "pids holding unicorn.lock => #{fuser_pids.join(' ')}"
|
29
|
+
logger.debug "pid from file => #{pid}"
|
30
|
+
raise ::Uc::Error, "stale pid #{pid}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def pid_from_file
|
35
|
+
@pid_from_file ||= read_pid
|
22
36
|
end
|
23
37
|
|
24
38
|
def stopped?
|
@@ -26,9 +40,9 @@ module Uc
|
|
26
40
|
end
|
27
41
|
|
28
42
|
def to_s
|
29
|
-
status = ( running? ? "Running pid #{pid}" : "Stopped")
|
43
|
+
status = ( running? ? "Running pid #{pid}" : "Stopped" )
|
30
44
|
end
|
31
|
-
|
45
|
+
|
32
46
|
private
|
33
47
|
|
34
48
|
def ex_lock_available?
|
@@ -44,6 +58,20 @@ module Uc
|
|
44
58
|
return -1
|
45
59
|
end
|
46
60
|
|
61
|
+
def fuser_pids
|
62
|
+
@fuser_pids ||= begin
|
63
|
+
output = `fuser #{paths.lock_file} 2>/dev/null`
|
64
|
+
pids = output.strip.split.map { |pid| pid.to_i }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def pid_valid?
|
69
|
+
if use_pid
|
70
|
+
return true
|
71
|
+
else
|
72
|
+
fuser_pids.include?(pid_from_file)
|
73
|
+
end
|
74
|
+
end
|
47
75
|
|
48
76
|
end
|
49
77
|
end
|
@@ -35,6 +35,7 @@ before_fork do |server, worker|
|
|
35
35
|
end
|
36
36
|
|
37
37
|
after_fork do |server, worker|
|
38
|
+
uc.oom_adjust
|
38
39
|
defined?(ActiveRecord::Base) and
|
39
40
|
ActiveRecord::Base.establish_connection
|
40
41
|
|
@@ -51,8 +52,6 @@ end
|
|
51
52
|
if respond_to? :on_exec_fail
|
52
53
|
uc = uc
|
53
54
|
on_exec_fail do |server|
|
54
|
-
puts "runnign asdadad"
|
55
55
|
uc.on_exec_fail
|
56
|
-
puts "lol"
|
57
56
|
end
|
58
57
|
end
|
data/lib/uc/unicorn/api.rb
CHANGED
@@ -52,6 +52,14 @@ module Uc; module Unicorn
|
|
52
52
|
shared_lock.acquire
|
53
53
|
end
|
54
54
|
|
55
|
+
def oom_adjust
|
56
|
+
pid = Process.pid
|
57
|
+
oom_file = "/proc/#{pid}/oom_score_adj"
|
58
|
+
File.open(oom_file,"w") do |f|
|
59
|
+
f.write "100"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
55
63
|
def shared_env
|
56
64
|
@shared_env ||= {}
|
57
65
|
end
|
data/lib/uc/unicorn/init.rb
CHANGED
@@ -34,7 +34,7 @@ module Uc
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def acquire_lock
|
37
|
-
lock_acquired =
|
37
|
+
lock_acquired = lock_fd.flock(File::LOCK_SH | File::LOCK_NB )
|
38
38
|
rescue => e
|
39
39
|
stderr.error "#{e.class} #{e.message}\n #{e.backtrace.join("\n")}"
|
40
40
|
return false
|
@@ -46,6 +46,10 @@ module Uc
|
|
46
46
|
run
|
47
47
|
end
|
48
48
|
|
49
|
+
def lock_fd
|
50
|
+
@lock_fd ||= File.new(lock_file, "a+")
|
51
|
+
end
|
52
|
+
|
49
53
|
def end_run(worker)
|
50
54
|
@worker = worker
|
51
55
|
if last_worker?
|
data/lib/uc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neeraj
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|