mamiya 0.0.1.alpha22 → 0.0.1.alpha23

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 132e823632be17c5738d084b0c0137d92846408f
4
- data.tar.gz: 19a669e42c52946e603884bf9a27816e7b22bbf7
3
+ metadata.gz: 46e8f4b3cd898fb6448e6cd4d5e8161c2fac101b
4
+ data.tar.gz: d921dadc048b7bfc587ec71a25d2cf14323dbe53
5
5
  SHA512:
6
- metadata.gz: d1ad837469c4bf7ddbeb9bb2d740070e9705344df425c2047b8f9ccec4e3a0a75626533f19f7b7a189407f147be538d7b628d884633bf68c5f969f2773df20c2
7
- data.tar.gz: 80e843665be039f845917ce97bc9c47bced11d2f147096868617d6c0d0306e407e600a95516dbab64a4e08ad3cd976358dd11df75939baa4a6297e8aa594113a
6
+ metadata.gz: d1169ca11bfc342862c8fc46fff5e91423a4d06ef2cab4f80b36275129e4b8f12047719b0459fc513d0e6091d1f43ccaa22834f9fa88ae0696a91e44e53268fc
7
+ data.tar.gz: b4906b0a1110d80ac1e82f7fecdbe22fc4b782203fdfcb83840cf3ecca4a3126334f02159e3966d93984e06ae556b0afb6c41dddb3786b9af603c924b74d986b
data/lib/mamiya/agent.rb CHANGED
@@ -18,6 +18,8 @@ require 'mamiya/agent/tasks/clean'
18
18
  require 'mamiya/agent/handlers/task'
19
19
  require 'mamiya/agent/actions'
20
20
 
21
+ require 'sigdump/setup' unless ENV['DISABLE_SIGDUMP'] == '1'
22
+
21
23
  module Mamiya
22
24
  class Agent
23
25
  include Mamiya::Agent::Actions
@@ -205,8 +207,10 @@ module Mamiya
205
207
 
206
208
  class_name = type.capitalize.gsub(/-./) { |_| _[1].upcase }
207
209
 
208
- logger.debug "Received user event #{type}"
209
- logger.debug payload.inspect
210
+ if config.debug_all_events
211
+ logger.debug "Received user event #{type}"
212
+ logger.debug payload.inspect
213
+ end
210
214
 
211
215
  if Handlers.const_defined?(class_name)
212
216
  handler = Handlers.const_get(class_name).new(self, event)
@@ -18,7 +18,7 @@ module Mamiya
18
18
  @queueing_thread = nil
19
19
  @lifecycle_mutex = Mutex.new
20
20
  @terminate = false
21
- @logger = logger['task_queue']
21
+ @logger = logger.with_clean_progname['task_queue']
22
22
  end
23
23
 
24
24
  attr_reader :worker_threads, :task_classes, :agent
@@ -118,7 +118,8 @@ module Mamiya
118
118
  status[:pending].delete task
119
119
  status[:working] = task
120
120
  end
121
- task_class.new(self, task, agent: @agent, logger: @logger).execute
121
+ task_logger = @logger.with_clean_progname
122
+ task_class.new(self, task, agent: @agent, logger: task_logger).execute
122
123
  rescue Exception => e
123
124
  @logger.error "#{task_class} worker catched error: #{e}\n\t#{e.backtrace.join("\n\t")}"
124
125
  ensure
@@ -133,7 +133,7 @@ not distributed: #{dist['not_distributed_count']} agents
133
133
  params = options[:labels] ?
134
134
  {labels: Mamiya::Util::LabelMatcher.parse_string_expr(options[:labels])} : {}
135
135
 
136
- p master_post("/packages/#{application}/#{package}/distribute", params, type: :json)
136
+ p master_post("/packages/#{application}/#{package}/distribute", params.merge(type: :json))
137
137
  end
138
138
 
139
139
  desc "prepare PACKAGE", "order preparing package to agents"
@@ -142,7 +142,7 @@ not distributed: #{dist['not_distributed_count']} agents
142
142
  params = options[:labels] ?
143
143
  {labels: Mamiya::Util::LabelMatcher.parse_string_expr(options[:labels])} : {}
144
144
 
145
- p master_post("/packages/#{application}/#{package}/prepare", params, type: :json)
145
+ p master_post("/packages/#{application}/#{package}/prepare", params.merge(type: :json))
146
146
  end
147
147
 
148
148
  desc "refresh", "order refreshing agent status"
@@ -181,17 +181,17 @@ not distributed: #{dist['not_distributed_count']} agents
181
181
  end
182
182
  end
183
183
 
184
- def master_post(path, data='', type: :text)
184
+ def master_post(path, data='')
185
185
  response = nil
186
186
  master_http.start do |http|
187
187
  headers = {}
188
188
 
189
189
  if Hash === data
190
- case type
190
+ case data.delete(:type) || :query
191
191
  when :json
192
192
  data = data.to_json
193
193
  headers['Content-Type'] = 'application/json'
194
- when :text
194
+ when :query
195
195
  data = Rack::Utils.build_nested_query(data)
196
196
  end
197
197
  end
@@ -8,7 +8,9 @@ module Mamiya
8
8
  # common
9
9
  set_default :serf, {}
10
10
  set_default :storage, {}
11
+
11
12
  set_default :show_backtrace_in_fatal, true
13
+ set_default :debug_all_events, false
12
14
 
13
15
  set_default :application, nil
14
16
 
data/lib/mamiya/logger.rb CHANGED
@@ -55,11 +55,21 @@ module Mamiya
55
55
  @logdev.remove_output(*outputs)
56
56
  end
57
57
 
58
+ def with_clean_progname
59
+ self.dup.tap do |new|
60
+ new.progname = nil
61
+ end
62
+ end
63
+
58
64
  def [](progname)
59
65
  self.dup.tap do |new_logger|
60
66
  new_logger.instance_eval do
61
67
  @logger = @logger.dup
62
- @logger.progname = progname
68
+ if @logger.progname && !@logger.progname.empty?
69
+ @logger.progname += "/#{progname}"
70
+ else
71
+ @logger.progname = progname
72
+ end
63
73
  end
64
74
  end
65
75
  end
@@ -181,7 +181,7 @@ module Mamiya
181
181
  private
182
182
 
183
183
  def logger
184
- @logger ||= @master.logger['agent-monitor']
184
+ @logger ||= @master.logger.with_clean_progname['agent-monitor']
185
185
  end
186
186
  end
187
187
  end
data/lib/mamiya/script.rb CHANGED
@@ -56,7 +56,9 @@ module Mamiya
56
56
  def run(*args, allow_failure: false)
57
57
  # TODO: Stop when fail
58
58
  actual = -> do
59
- logger = self.logger['RUN']
59
+ started_at = Time.now
60
+ run_id = generate_run_id()
61
+ logger = self.logger["run:#{run_id}"]
60
62
 
61
63
  logger.info("$ #{args.shelljoin}")
62
64
 
@@ -68,18 +70,32 @@ module Mamiya
68
70
  [out_w, err_w].each(&:close)
69
71
 
70
72
  buf = ""
73
+ last_out = Time.now
71
74
 
72
- ths = {:debug => out_r, :warn => err_r}.map do |severity, io|
75
+ ths = {:info => out_r, :warn => err_r}.map do |severity, io|
73
76
  Thread.new {
74
77
  until io.eof?
75
78
  str = io.gets
76
79
  logger.__send__(severity, str.chomp)
77
80
  buf << str
81
+ last_out = Time.now
78
82
  end
79
83
  }.tap { |_| _.abort_on_exception = true }
80
84
  end
81
85
 
86
+ timekeeper_th = Thread.new do
87
+ l = logger['timekeeper']
88
+ loop do
89
+ if 90 < (Time.now - last_out)
90
+ l.warn "pid #{pid} still running; since #{started_at}"
91
+ end
92
+ sleep 60
93
+ end
94
+ end
95
+ timekeeper_th.abort_on_exception = true
96
+
82
97
  pid, status = Process.waitpid2(pid)
98
+ timekeeper_th.kill if timekeeper_th.alive?
83
99
 
84
100
  begin
85
101
  timeout(3) { ths.each(&:join) }
@@ -90,15 +106,20 @@ module Mamiya
90
106
  [out_r, err_r].each(&:close)
91
107
 
92
108
  unless allow_failure || status.success?
93
- raise CommandFailed,
94
- "Excecution failed (" \
109
+ failure_msg = "Execution failed (" \
95
110
  "status=#{status.exitstatus}" \
96
111
  " pid=#{status.pid}" \
97
112
  "#{status.signaled? ? "termsig=#{status.termsig.inspect} stopsig=#{status.stopsig.inspect}" : nil}" \
98
113
  "#{status.stopped? ? " stopped" : nil}" \
99
114
  "): #{args.inspect}"
115
+
116
+ logger.error failure_msg
117
+ raise CommandFailed, failure_msg
118
+
100
119
  end
101
120
 
121
+ logger.info "pid #{pid} completed: #{args.inspect}"
122
+
102
123
  buf
103
124
  end
104
125
 
@@ -133,5 +154,28 @@ module Mamiya
133
154
  def current_path
134
155
  deploy_to && deploy_to.join('current')
135
156
  end
157
+
158
+ private
159
+
160
+ RUN_ID_BASE_TIME = Time.new(2014,01,01,0,0,0).to_i
161
+ def generate_run_id
162
+ (@run_id_mutex ||= Mutex.new).synchronize do
163
+ t = Time.now.to_i
164
+ id = (t - RUN_ID_BASE_TIME).to_i.to_s(36)
165
+
166
+ @last_run_id_time ||= 0
167
+ if (t - @last_run_id_time) < 1
168
+ @run_id_seq ||= 0
169
+ @run_id_seq += 1
170
+ id << @run_id_seq.to_s(36)
171
+ else
172
+ @run_id_seq = nil
173
+ id << '0'
174
+ end
175
+
176
+ @last_run_id_time = t
177
+ id
178
+ end
179
+ end
136
180
  end
137
181
  end
@@ -10,6 +10,7 @@ module Mamiya
10
10
  script.before_switch(labels)[]
11
11
 
12
12
  # TODO: link with relative if available?
13
+ # TODO: Restore this if FAILED
13
14
  File.unlink script.current_path if script.current_path.symlink?
14
15
  script.current_path.make_symlink(target.realpath)
15
16
 
@@ -1,3 +1,3 @@
1
1
  module Mamiya
2
- VERSION = "0.0.1.alpha22"
2
+ VERSION = "0.0.1.alpha23"
3
3
  end
data/mamiya.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  end
27
27
 
28
28
  spec.add_runtime_dependency "sinatra", ">= 1.4.5"
29
+ spec.add_runtime_dependency "sigdump"
29
30
 
30
31
  spec.add_development_dependency "rspec", "2.14.1"
31
32
  spec.add_development_dependency 'rack-test', '~> 0.6.2'
data/spec/script_spec.rb CHANGED
@@ -15,7 +15,7 @@ describe Mamiya::Script do
15
15
  let(:logger) {
16
16
  double("logger").tap do |_|
17
17
  _.stub(:[]) { _ }
18
- %i(info warn debug).each do |severity|
18
+ %i(info warn debug error).each do |severity|
19
19
  _.stub(severity) { |*args| log << [severity, *args]; _ }
20
20
  end
21
21
  end
@@ -38,6 +38,14 @@ describe Mamiya::Script do
38
38
  end
39
39
 
40
40
  context "when the command failed" do
41
+ it "logs error" do
42
+ begin
43
+ script.run("false")
44
+ rescue Mamiya::Script::CommandFailed
45
+ end
46
+ expect(log.map(&:first)).to include(:error)
47
+ end
48
+
41
49
  it "raises error" do
42
50
  expect {
43
51
  script.run("false")
@@ -53,15 +61,14 @@ describe Mamiya::Script do
53
61
  end
54
62
  end
55
63
 
56
-
57
64
  it "logs command as information" do
58
65
  script.run("echo", "foo", "bar'", " baz")
59
66
  expect(log).to include([:info, "$ echo foo bar\\' \\ baz"])
60
67
  end
61
68
 
62
- it "logs stdout as debug" do
69
+ it "logs stdout as info" do
63
70
  script.run("echo", "foo")
64
- expect(log).to include([:debug, "foo"])
71
+ expect(log).to include([:info, "foo"])
65
72
  end
66
73
 
67
74
  it "logs stderr as warn" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mamiya
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha22
4
+ version: 0.0.1.alpha23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shota Fukumori (sora_h)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
11
+ date: 2014-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.4.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: sigdump
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement