zeusd 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/zeusd/daemon.rb CHANGED
@@ -1,37 +1,13 @@
1
- require 'logger'
2
1
  require 'thread'
3
2
  require 'childprocess'
4
3
  require 'pathname'
5
- require 'hooks'
6
4
  require 'file-tail'
7
- require 'json'
8
5
 
9
- module Zeusd
10
- class DaemonException < StandardError; end
6
+ require 'zeusd/daemon_logging'
11
7
 
8
+ module Zeusd
12
9
  class Daemon
13
- include Hooks
14
-
15
- define_hooks :before_action, :after_action, :after_output
16
-
17
- before_action do |action|
18
- details = {}
19
- details[:process] = process.attributes if process
20
- log_event("Before: #{action}", details)
21
- end
22
-
23
- after_action do |action|
24
- details = {}
25
- details[:process] = process.attributes if process
26
- log_event("After: #{action}", details)
27
- end
28
-
29
- after_output do |output|
30
- interpreter.translate(output)
31
- puts output if verbose
32
- end
33
-
34
- attr_reader :cwd, :verbose, :log_file, :interpreter, :child_process
10
+ attr_reader :cwd, :verbose, :interpreter, :child_process
35
11
 
36
12
  def initialize(options = {})
37
13
  @cwd = Pathname.new(options[:cwd] || Dir.pwd).realpath
@@ -40,8 +16,6 @@ module Zeusd
40
16
  end
41
17
 
42
18
  def start!(options = {})
43
- run_hook :before_action, __method__
44
-
45
19
  start_child_process!
46
20
 
47
21
  @process = Zeusd::Process.find(child_process.pid)
@@ -50,44 +24,31 @@ module Zeusd
50
24
  sleep(0.1) until loaded?
51
25
  end
52
26
 
53
- run_hook :after_action, __method__
54
-
55
27
  self
56
28
  end
57
29
 
58
30
  def restart!(options = {})
59
- run_hook :before_action, __method__
60
-
61
31
  stop!.start!(options)
62
-
63
- run_hook :after_action, __method__
64
-
65
- self
66
32
  end
67
33
 
68
34
  def stop!
69
- run_hook :before_action, __method__
70
-
71
35
  return self unless process
72
36
 
73
37
  # Kill process tree and wait for exits
74
- process.kill!(:recursive => true, :wait => true)
38
+ process.kill!(:recursive => true, :signal => "KILL", :wait => true)
75
39
 
76
40
  # Clean up socket file if stil exists
77
41
  (zeus_socket_file.delete rescue nil) if zeus_socket_file.exist?
78
42
 
79
- # Check for remaining processes
80
- if[process, process.descendants].flatten.select(&:alive?).any?
81
- raise DaemonException, "Unable to KILL processes: " + alive_processes.join(', ')
82
- end
83
-
84
43
  @process = nil
85
44
 
86
- run_hook :after_action, __method__
87
-
88
45
  self
89
46
  end
90
47
 
48
+ def processes
49
+ process ? [process, process.descendants].flatten : []
50
+ end
51
+
91
52
  def process
92
53
  @process ||= Process.all.find {|p| !!p.command[/zeus.*start$/] && p.cwd == cwd }
93
54
  end
@@ -106,27 +67,22 @@ module Zeusd
106
67
  end
107
68
  end
108
69
 
109
- protected
110
-
111
- def log_event(type, details = nil)
112
- logger.info do
113
- "\e[35m[Event] (#{type})\e[0m" + (!details.empty? ? " " + JSON.pretty_generate(details) : "")
114
- end
70
+ def to_json(*args)
71
+ {
72
+ :class => self.class.name,
73
+ :cwd => cwd.to_path,
74
+ :verbose => verbose,
75
+ :process => process
76
+ }.to_json(*args)
115
77
  end
116
78
 
117
- def logger
118
- @logger ||= Logger.new(cwd.join('log', 'zeusd.log').to_path).tap do |x|
119
- x.formatter = proc do |severity, datetime, progname, msg|
120
- prefix = "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}]"
121
- "\e[36m#{prefix}\e[0m" + " #{msg}\n"
122
- end
123
- end
124
- end
79
+ protected
125
80
 
126
81
  def start_child_process!
127
- # Truncate and cast to File
82
+ # Truncate and cast to File instance
128
83
  zeus_log_file.open("w") {}
129
- std_file = File.new(zeus_log_file, 'w+').tap{|x| x.sync = true}
84
+ std_file = File.new(zeus_log_file, 'w+')
85
+ std_file.sync = true
130
86
 
131
87
  # Prep and Start child process
132
88
  @child_process = ChildProcess.build("zeus", "start")
@@ -143,7 +99,10 @@ module Zeusd
143
99
  log.extend(File::Tail)
144
100
  log.interval = 0.1
145
101
  log.backward(100)
146
- log.tail {|line| run_hook(:after_output, line) }
102
+ log.tail do |line|
103
+ interpreter.translate(line)
104
+ puts line if verbose
105
+ end
147
106
  end
148
107
  end
149
108
 
@@ -153,5 +112,6 @@ module Zeusd
153
112
  @child_process
154
113
  end
155
114
 
115
+ include Zeusd::DaemonLogging
156
116
  end
157
117
  end
@@ -0,0 +1,44 @@
1
+ require 'logger'
2
+ require 'json'
3
+
4
+ module Zeusd
5
+ module DaemonLogging
6
+
7
+ def log_file
8
+ cwd.join('log', 'zeusd.log')
9
+ end
10
+
11
+ def track(occurred, method, details = nil)
12
+ logger.info do
13
+ "\e[35m[Track] [#{occurred.to_s.upcase}] .#{method}()\e[0m" + (details ? " " + JSON.pretty_generate(details) : "")
14
+ end
15
+ end
16
+
17
+ def logger
18
+ @logger ||= Logger.new(log_file.to_path).tap do |l|
19
+ l.formatter = proc do |severity, datetime, progname, msg|
20
+ "\e[36m[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}]\e[0m" + " #{msg}\n"
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.included(base)
26
+ tracked_methods = [:start!, :stop!, :restart!, :start_child_process!]
27
+ base.instance_eval do
28
+ tracked_methods.each do |method_name|
29
+ original_method = instance_method(method_name)
30
+ track = instance_method(:track)
31
+
32
+ define_method(method_name) do |*args, &block|
33
+ track.bind(self).call(:before, method_name, :args => args)
34
+ original_method.bind(self).call(*args, &block).tap do |x|
35
+ track.bind(self).call(:after, method_name, :return => x)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+
43
+ end
44
+ end
@@ -83,6 +83,10 @@ module Zeusd
83
83
  end
84
84
  end
85
85
 
86
+ def color?
87
+ !!color
88
+ end
89
+
86
90
  end
87
91
 
88
92
  end
data/lib/zeusd/process.rb CHANGED
@@ -18,6 +18,10 @@ module Zeusd
18
18
  end
19
19
  end
20
20
 
21
+ def to_json(*args)
22
+ attributes.to_json(*args)
23
+ end
24
+
21
25
  def self.ps(options = {})
22
26
  keywords = Array(options[:keywords]) | %w[pid ppid pgid stat command]
23
27
  command = ["ps"].tap do |ps|
data/lib/zeusd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Zeusd
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zeusd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-28 00:00:00.000000000 Z
12
+ date: 2014-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -158,6 +158,7 @@ files:
158
158
  - bin/zeusd
159
159
  - lib/zeusd.rb
160
160
  - lib/zeusd/daemon.rb
161
+ - lib/zeusd/daemon_logging.rb
161
162
  - lib/zeusd/interpreter.rb
162
163
  - lib/zeusd/process.rb
163
164
  - lib/zeusd/version.rb
@@ -231,7 +232,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
231
232
  version: '0'
232
233
  segments:
233
234
  - 0
234
- hash: -4127736754607950828
235
+ hash: -1735692127611811224
235
236
  required_rubygems_version: !ruby/object:Gem::Requirement
236
237
  none: false
237
238
  requirements:
@@ -240,7 +241,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
240
241
  version: '0'
241
242
  segments:
242
243
  - 0
243
- hash: -4127736754607950828
244
+ hash: -1735692127611811224
244
245
  requirements: []
245
246
  rubyforge_project:
246
247
  rubygems_version: 1.8.25