madvertise-ext 0.4.0 → 0.5.0

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.
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ madvertise-ext
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p392
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ # load a bunch of common classes here, so we don't have to track and repeat it
4
+ # everywhere
5
+ require 'active_support'
6
+ require 'cgi'
7
+ require 'date'
8
+ require 'json'
9
+ require 'servolux'
10
+ require 'socket'
11
+
12
+ # load all madvertise extensions
13
+ Dir[File.join(File.dirname(__FILE__), 'ext', '*.rb')].each do |f|
14
+ require f
15
+ end
16
+
17
+ Dir[File.join(File.dirname(__FILE__), '*.rb')].each do |f|
18
+ require f
19
+ end
20
+
21
+ require 'madvertise/logging' # dedicated gem
22
+
23
+ # initialize configuration and logger with hardcoded defaults
24
+ $conf = Conf = Configuration.new
25
+ $conf.callback do
26
+ ImprovedLogger::Formatter.format = $conf.log_format
27
+ $log = MultiLogger.new
28
+ $log.attach(ImprovedLogger.new($conf.log_backend.to_sym, File.basename($0)))
29
+ $log.level = $conf.log_level.downcase.to_sym
30
+ $log.log_caller = $conf.log_caller
31
+ end
32
+
33
+ # trigger log callback with defaults
34
+ $conf.reload!
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+
3
+ require 'madvertise/boot'
4
+ require 'mixlib/cli'
5
+
6
+ $0 = File.basename($0)
7
+
8
+ class CLI
9
+ include Mixlib::CLI
10
+
11
+ option :configfile,
12
+ short: '-c FILE',
13
+ long: '--config FILE',
14
+ description: 'Configuration File to load'
15
+
16
+ option :name,
17
+ :short => '-n NAME',
18
+ :long => '--name NAME',
19
+ :description => 'Process name',
20
+ :default => $0,
21
+ :proc => ->(value) { $0 = value }
22
+
23
+ option :environment,
24
+ :short => '-e ENVIRONMENT',
25
+ :long => '--environment ENVIRONMENT',
26
+ :description => "Set the daemon environment",
27
+ :default => "development",
28
+ :proc => ->(value) { Env.set(value) }
29
+
30
+ option :debug,
31
+ :short => '-D',
32
+ :long => '--debug',
33
+ :description => "Enable debug output",
34
+ :boolean => true,
35
+ :default => false,
36
+ :proc => ->(value) { $conf.mixin(log_level: :debug) }
37
+
38
+ option :help,
39
+ :short => '-h',
40
+ :long => '--help',
41
+ :description => "Show this message",
42
+ :on => :tail,
43
+ :boolean => true,
44
+ :show_options => true,
45
+ :exit => 0
46
+
47
+ def option(name, args)
48
+ args[:on] ||= :on
49
+ args[:boolean] ||= false
50
+ args[:required] ||= false
51
+ args[:proc] ||= nil
52
+ args[:show_options] ||= false
53
+ args[:exit] ||= nil
54
+
55
+ if args.has_key?(:default)
56
+ config[name.to_sym] = args[:default]
57
+ end
58
+
59
+ options[name.to_sym] = args
60
+ end
61
+
62
+ def self.for(cls, &block)
63
+ cli = new
64
+ cli.instance_eval(&block) if block_given?
65
+ cli.parse_options
66
+
67
+ $log.info("cli:initialize", cli.config)
68
+ $conf.reload!
69
+
70
+ opts = cli.config.merge({
71
+ interval: 1,
72
+ logger: $log,
73
+ })
74
+
75
+ cls.new(opts)
76
+ end
77
+
78
+ end
@@ -1,8 +1,10 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'yaml'
2
4
  require 'set'
3
5
 
4
6
  require 'madvertise/ext/hash'
5
- require 'madvertise/ext/environment'
7
+ require 'madvertise/environment'
6
8
 
7
9
  ##
8
10
  # A {Configuration} consists of one or more Sections. A section is a hash-like
@@ -58,30 +60,14 @@ class Section < Hash
58
60
 
59
61
  end
60
62
 
61
- # Build the call chain including NilSections.
62
- #
63
63
  # @private
64
64
  def method_missing(name, *args)
65
65
  if name.to_s =~ /(.*)=$/
66
66
  self[$1.to_sym] = Section.from_value(args.first)
67
67
  else
68
68
  value = self[name]
69
- value = value.call if value.is_a?(Proc)
70
-
71
- if value.nil?
72
- case self.class.nil_action
73
- when :nil, nil
74
- # do nothing
75
- when :raise
76
- raise "value is nil for key #{name}"
77
- when :section
78
- value = NilSection.new if value.nil?
79
- else
80
- raise "unknown nil handling: #{self.class.nil_action}"
81
- end
82
- end
83
-
84
- self[name] = value
69
+ self[name] = value.call if value.is_a?(Proc)
70
+ self[name]
85
71
  end
86
72
  end
87
73
  end
@@ -92,28 +78,20 @@ end
92
78
  #
93
79
  class Configuration < Section
94
80
 
81
+ DEFAULTS = {
82
+ log_backend: :stdout,
83
+ log_caller: false,
84
+ log_format: "%{time} %{progname}(%{pid}) [%{severity}] %{msg}\n",
85
+ log_level: :info,
86
+ }
87
+
95
88
  # Create a new {Configuration} object.
96
89
  #
97
- # @param [Symbol] mode The mode to load from the configurtion file
98
- # (production, development, etc)
99
90
  # @yield [config] The new configuration object.
100
91
  def initialize
101
92
  @mixins = Set.new
102
93
  @callbacks = []
103
- yield self if block_given?
104
- end
105
-
106
- # Load given mixins from +path+.
107
- #
108
- # @param [String] path The path to mixin files.
109
- # @param [Array] mixins_to_use A list of mixins to load from +path+.
110
- # @return [void]
111
- def load_mixins(path, mixins_to_use)
112
- mixins_to_use.map do |mixin_name|
113
- File.join(path, "#{mixin_name}.yml")
114
- end.each do |mixin_file|
115
- mixin(mixin_file)
116
- end
94
+ mixin(DEFAULTS)
117
95
  end
118
96
 
119
97
  # Mixin a configuration snippet into the current section.
@@ -124,24 +102,20 @@ class Configuration < Section
124
102
  # contain a YAML hash.
125
103
  # @return [void]
126
104
  def mixin(value)
105
+ @mixins << value
106
+
127
107
  if value.is_a?(String)
128
- @mixins << value
129
108
  value = YAML.load(File.read(value))
130
109
  end
131
110
 
132
111
  value = Section.from_hash(value)
133
112
 
134
- self.deep_merge!(value[:default]) if value.has_key?(:default)
135
- self.deep_merge!(value[:generic]) if value.has_key?(:generic)
113
+ deep_merge!(value[:generic]) if value.has_key?(:generic)
136
114
 
137
115
  if value.has_key?(Env.to_sym)
138
- self.deep_merge!(value[Env.to_sym])
116
+ deep_merge!(value[Env.to_sym])
139
117
  else
140
- self.deep_merge!(value)
141
- end
142
-
143
- @callbacks.each do |callback|
144
- callback.call
118
+ deep_merge!(value)
145
119
  end
146
120
  end
147
121
 
@@ -149,9 +123,14 @@ class Configuration < Section
149
123
  #
150
124
  # @return [void]
151
125
  def reload!
152
- self.clear
126
+ clear
127
+
153
128
  @mixins.each do |file|
154
- self.mixin(file)
129
+ mixin(file)
130
+ end
131
+
132
+ @callbacks.each do |callback|
133
+ callback.call
155
134
  end
156
135
  end
157
136
 
@@ -163,45 +142,3 @@ class Configuration < Section
163
142
  end
164
143
 
165
144
  end
166
-
167
- ##
168
- # A NilSection is returned for all missing/empty values in the config file. This
169
- # allows for terse code when accessing values that have not been configured by
170
- # the user.
171
- #
172
- # Consider code like this:
173
- #
174
- # config.server.listen.tap do |listen|
175
- # open_socket(listen.host, listen.port)
176
- # end
177
- #
178
- # Given that your server component is optional and does not appear in the
179
- # configuration file at all, +config.server.listen+ will return a NilSection
180
- # that does not call the block given to tap _at all_.
181
- #
182
- class NilSection
183
- # @return true
184
- def nil?
185
- true
186
- end
187
-
188
- # @return true
189
- def empty?
190
- true
191
- end
192
-
193
- # @return false
194
- def present?
195
- false
196
- end
197
-
198
- # @return nil
199
- def tap
200
- nil
201
- end
202
-
203
- # @private
204
- def method_missing(*args, &block)
205
- self
206
- end
207
- end
@@ -0,0 +1,49 @@
1
+ module Memory
2
+ WORD_SIZE = 8
3
+ OBJ_SIZE = 40 # some are smaller
4
+ OBJ_OVERHEAD = WORD_SIZE + OBJ_SIZE
5
+
6
+ def self.size(obj)
7
+ return WORD_SIZE if obj.is_a?(Fixnum)
8
+
9
+ case obj
10
+ when String
11
+ obj.size
12
+ when Array
13
+ obj.size * WORD_SIZE
14
+ when Hash
15
+ obj.size * WORD_SIZE * 2
16
+ #when Enumerable
17
+ # result = 0
18
+ # obj.each do |ref|
19
+ # result += WORD_SIZE
20
+ # end
21
+ # result
22
+ else
23
+ 0
24
+ end + OBJ_OVERHEAD
25
+ rescue => e
26
+ puts "failed to get object size for #{obj.inspect}: #{e}"
27
+ return OBJ_OVERHEAD
28
+ end
29
+ end
30
+
31
+ module ObjectSpace
32
+ def self.memory_stats(*args)
33
+ stats = {}
34
+
35
+ self.each_object do |obj|
36
+ stats[obj.class] ||= []
37
+ stats[obj.class] << Memory.size(obj)
38
+ end
39
+
40
+ stats.map do |cls, sizes|
41
+ cnt = sizes.length
42
+ sum = sizes.reduce(:+)
43
+ avg = sum / cnt
44
+ [cls, [cnt, avg, sum]]
45
+ end.sort_by do |cls, sizes|
46
+ sizes[2]
47
+ end.reverse
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ module Signal
2
+ def self.register_shutdown_handler(&block)
3
+ %w(QUIT INT TERM).each do |sig|
4
+ old = trap(sig) {}
5
+ trap(sig) { block.call; old.call }
6
+ end
7
+ end
8
+ end
@@ -1,6 +1,6 @@
1
1
  # @private
2
2
  module Madvertise
3
3
  module Ext
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
6
6
  end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ module FromFile
4
+
5
+ module ClassMethods
6
+ def from_file(path)
7
+ new.tap do |obj|
8
+ obj.from_file(path)
9
+ end
10
+ end
11
+ end
12
+
13
+ def from_file(path)
14
+ if File.exists?(path) && File.readable?(path)
15
+ self.instance_eval(IO.read(path), path, 1)
16
+ else
17
+ raise IOError, "Cannot open or read #{path}!"
18
+ end
19
+ end
20
+
21
+ def self.included(receiver)
22
+ receiver.extend(ClassMethods)
23
+ end
24
+
25
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ require 'metriks'
3
+ require 'singleton'
4
+
5
+ class GCStats
6
+ include Singleton
7
+
8
+ # RUBY 1.9.X-Implementation
9
+ module Yarv
10
+ def enable
11
+ GC::Profiler.enable
12
+ end
13
+
14
+ def clear
15
+ GC::Profiler.clear
16
+ end
17
+
18
+ def gather
19
+ count_collections
20
+ count_allocations
21
+ count_objects
22
+ Metriks.timer('gc_stats.total_time').update(GC::Profiler.total_time)
23
+ end
24
+
25
+ def count_collections
26
+ Metriks.histogram('gc_stats.collections').update(GC.count)
27
+ end
28
+
29
+ def count_allocations
30
+ allocated_size = GC.respond_to?(:malloc_allocated_size) ? GC.malloc_allocated_size / 1000.0 : 0
31
+ Metriks.histogram('gc_stats.allocated').update(allocated_size)
32
+ end
33
+
34
+ def count_objects
35
+ objects = ObjectSpace.count_objects
36
+ objects = objects[:TOTAL] - objects[:FREE]
37
+ Metriks.histogram('gc_stats.objects').update(objects)
38
+ end
39
+ end
40
+
41
+ if RUBY_VERSION =~ /^1\.9/
42
+ extend Yarv
43
+ end
44
+
45
+ def self.available?
46
+ respond_to?(:gather)
47
+ end
48
+
49
+ def self.start!
50
+ if available?
51
+ enable
52
+ $log.info("gc:stats", enabled: true)
53
+
54
+ EventMachine.next_tick do
55
+ EventMachine::PeriodicTimer.new(60*60) do
56
+ GC.start
57
+ end
58
+
59
+ EventMachine::PeriodicTimer.new(60) do
60
+ gather
61
+ clear
62
+ end
63
+ end
64
+ else
65
+ $log.info("gc:stats", enabled: false)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ require 'digest'
4
+
5
+ class HashHelper
6
+ def self.numeric_hash(val)
7
+ # Use a the first 15 bytes of a MD5 bytes - small enough to be represented as Fixnum => no garbage collection
8
+ Digest::MD5.hexdigest(val.to_s)[0..14].to_i(16)
9
+ end
10
+ end
@@ -0,0 +1,56 @@
1
+ require 'madvertise/sysconf'
2
+
3
+ ProcStat = Struct.new(
4
+ :ppid,
5
+ :pgrp,
6
+ :session,
7
+ :tty_nr,
8
+ :tpgid,
9
+ :flags,
10
+ :minflt,
11
+ :cminflt,
12
+ :majflt,
13
+ :cmajflt,
14
+ :utime,
15
+ :stime,
16
+ :cutime,
17
+ :cstime,
18
+ :priority,
19
+ :nice,
20
+ :num_threads,
21
+ :itrealvalue,
22
+ :starttime,
23
+ :vsize,
24
+ :rss,
25
+ :rsslim,
26
+ :startcode,
27
+ :endcode,
28
+ :startstack,
29
+ :kstkesp,
30
+ :kstkeip,
31
+ :signal,
32
+ :blocked,
33
+ :sigignore,
34
+ :sigcatch,
35
+ :wchan,
36
+ :nswap,
37
+ :cnswap,
38
+ :exit_signal,
39
+ :processor,
40
+ :rt_priority,
41
+ :policy,
42
+ :delayacct_blkio_ticks,
43
+ :guest_time,
44
+ :cguest_time
45
+ )
46
+
47
+ class ProcStat
48
+ def self.read
49
+ stat = File.read("/proc/self/stat").chomp.split
50
+ new(*stat[3..40].map(&:to_i))
51
+ end
52
+
53
+ def pagesize
54
+ Sysconf.sysconf(:page_size)
55
+ end
56
+ end
@@ -0,0 +1,25 @@
1
+ require 'ffi'
2
+ require 'ffi/tools/const_generator'
3
+
4
+ module Sysconf
5
+ extend FFI::Library
6
+ ffi_lib ["c"]
7
+
8
+ fcg = FFI::ConstGenerator.new do |gen|
9
+ gen.include 'unistd.h'
10
+
11
+ %w[
12
+ _SC_PAGE_SIZE
13
+ _SC_VERSION
14
+ ].each do |const|
15
+ ruby_name = const.sub(/^_SC_/, '').downcase.to_sym
16
+ gen.const(const, "%d", nil, ruby_name, &:to_i)
17
+ end
18
+ end
19
+
20
+ CONF = enum(*fcg.constants.map{|_, const|
21
+ [const.ruby_name, const.converted_value]
22
+ }.flatten)
23
+
24
+ attach_function :sysconf, [CONF], :long
25
+ end
@@ -10,6 +10,8 @@ Gem::Specification.new do |gem|
10
10
  gem.summary = %q{Ruby extensions}
11
11
  gem.homepage = "https://github.com/madvertise/ext"
12
12
 
13
+ gem.add_dependency "activesupport"
14
+ gem.add_dependency "ffi"
13
15
  gem.add_dependency "madvertise-logging"
14
16
  gem.add_dependency "mixlib-cli"
15
17
  gem.add_dependency "servolux"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: madvertise-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-10 00:00:00.000000000 Z
12
+ date: 2013-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: ffi
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
14
46
  - !ruby/object:Gem::Dependency
15
47
  name: madvertise-logging
16
48
  requirement: !ruby/object:Gem::Requirement
@@ -67,27 +99,33 @@ extensions: []
67
99
  extra_rdoc_files: []
68
100
  files:
69
101
  - .gitignore
70
- - .rvmrc
102
+ - .ruby-gemset
103
+ - .ruby-version
71
104
  - .travis.yml
72
105
  - .yardopts
73
106
  - Gemfile
74
107
  - LICENSE
75
108
  - README.md
76
109
  - Rakefile
110
+ - lib/madvertise/boot.rb
111
+ - lib/madvertise/cli.rb
112
+ - lib/madvertise/configuration.rb
113
+ - lib/madvertise/environment.rb
77
114
  - lib/madvertise/ext/array.rb
78
- - lib/madvertise/ext/config.rb
79
115
  - lib/madvertise/ext/enumerable.rb
80
- - lib/madvertise/ext/environment.rb
81
116
  - lib/madvertise/ext/hash.rb
82
- - lib/madvertise/ext/logging.rb
83
- - lib/madvertise/ext/mask.reek
84
117
  - lib/madvertise/ext/nil.rb
118
+ - lib/madvertise/ext/object_space.rb
119
+ - lib/madvertise/ext/signal.rb
85
120
  - lib/madvertise/ext/string.rb
86
- - lib/madvertise/ext/transaction_id.rb
87
121
  - lib/madvertise/ext/version.rb
88
- - lib/servolux/cli.rb
122
+ - lib/madvertise/from_file.rb
123
+ - lib/madvertise/gc_stats.rb
124
+ - lib/madvertise/hash_helper.rb
125
+ - lib/madvertise/proc_stat.rb
126
+ - lib/madvertise/sysconf.rb
127
+ - lib/madvertise/transaction_id.rb
89
128
  - lib/servolux/eventmachine.rb
90
- - lib/servolux/wrapper.rb
91
129
  - madvertise-ext.gemspec
92
130
  - spec/enumerable_spec.rb
93
131
  - spec/spec.opts
@@ -109,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
147
  version: '0'
110
148
  segments:
111
149
  - 0
112
- hash: 1335580830932580809
150
+ hash: 3215958011008345483
113
151
  required_rubygems_version: !ruby/object:Gem::Requirement
114
152
  none: false
115
153
  requirements:
@@ -118,10 +156,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
156
  version: '0'
119
157
  segments:
120
158
  - 0
121
- hash: 1335580830932580809
159
+ hash: 3215958011008345483
122
160
  requirements: []
123
161
  rubyforge_project:
124
- rubygems_version: 1.8.24
162
+ rubygems_version: 1.8.25
125
163
  signing_key:
126
164
  specification_version: 3
127
165
  summary: Ruby extensions
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use --create ruby-1.9.3-p327@madvertise-ext
@@ -1,26 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'madvertise/ext/environment'
4
- require 'madvertise-logging'
5
-
6
- def init_logger(progname=$0, filename=nil)
7
- progname = File.basename(progname)
8
- filename ||= "#{Env.mode}.log"
9
-
10
- MultiLogger.new.tap do |logger|
11
- init_multi_logger(logger, progname, filename)
12
- end
13
- end
14
-
15
- def init_multi_logger(logger, progname=$0, filename=nil)
16
- if Env.dev? or Env.test?
17
- logger.attach(ImprovedLogger.new(STDERR, progname))
18
- else
19
- logger.attach(ImprovedLogger.new(:syslog, progname))
20
- end
21
-
22
- # default log level
23
- logger.level = Logger::INFO
24
- end
25
-
26
- $log = init_logger
@@ -1,7 +0,0 @@
1
- ---
2
- NestedIterators:
3
- exclude:
4
- - Section#from_hash
5
- FeatureEnvy:
6
- exclude:
7
- - Hash#deep_merge
data/lib/servolux/cli.rb DELETED
@@ -1,73 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'madvertise/ext/environment'
4
- require 'mixlib/cli'
5
- require 'servolux'
6
-
7
- module Servolux
8
- class BaseCLI
9
- include Mixlib::CLI
10
-
11
- def self.inherited(subclass)
12
- subclass.option :environment,
13
- :short => '-e ENVIRONMENT',
14
- :long => '--environment ENVIRONMENT',
15
- :description => "Set the daemon environment",
16
- :default => "development",
17
- :proc => ->(value) { Env.set(value) }
18
-
19
- subclass.option :debug,
20
- :short => '-D',
21
- :long => '--debug',
22
- :description => "Enable debug output",
23
- :boolean => true,
24
- :default => false
25
-
26
- subclass.option :help,
27
- :short => '-h',
28
- :long => '--help',
29
- :description => "Show this message",
30
- :on => :tail,
31
- :boolean => true,
32
- :show_options => true,
33
- :exit => 0
34
- end
35
-
36
- def self.parse_options
37
- new.tap do |cli|
38
- cli.parse_options
39
- end.config
40
- end
41
- end
42
-
43
- class CLI < BaseCLI
44
- end
45
-
46
- class DaemonCLI < BaseCLI
47
- option :name,
48
- :short => '-n NAME',
49
- :long => '--name NAME',
50
- :description => 'Process name',
51
- :default => $0,
52
- :proc => ->(value) { $0 = value }
53
-
54
- option :pidfile,
55
- :short => '-p PIDFILE',
56
- :long => '--pidfile PIDFILE',
57
- :description => "The daemon pidfile",
58
- :default => "#{$0}.pid"
59
-
60
- option :daemonize,
61
- :short => '-d',
62
- :long => '--daemonize',
63
- :description => "Daemonize the server process",
64
- :boolean => true,
65
- :default => false
66
-
67
- option :kill,
68
- :short => '-k',
69
- :long => '--kill',
70
- :description => "Kill the currently running daemon instance",
71
- :boolean => true
72
- end
73
- end
@@ -1,51 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'madvertise/ext/logging'
4
- require 'servolux'
5
- require 'servolux/cli'
6
-
7
- module Servolux
8
- def self.parse_opts(cli_class)
9
- opts = cli_class.parse_options
10
-
11
- # CLI.parse_options may have changed $0
12
- # so we reload the logger for good measure
13
- $log = init_logger
14
- $log.level = opts[:debug] ? :debug : :info
15
-
16
- return opts
17
- end
18
-
19
- def self.wrap(server_class)
20
- opts = self.parse_opts(Servolux::CLI)
21
- server_class.new(opts).run
22
- rescue => e
23
- $log.exception(e)
24
- raise e
25
- end
26
-
27
- def self.wrap_daemon(server_class)
28
- opts = self.parse_opts(Servolux::DaemonCLI)
29
-
30
- server = server_class.new(opts[:name], opts.merge({
31
- interval: 1,
32
- logger: $log,
33
- pid_file: opts[:pidfile]
34
- }))
35
-
36
- if opts[:daemonize] or opts[:kill]
37
- daemon = Servolux::Daemon.new(:server => server)
38
-
39
- if opts[:kill]
40
- daemon.shutdown
41
- else
42
- daemon.startup
43
- end
44
- else
45
- server.startup
46
- end
47
- rescue => e
48
- $log.exception(e)
49
- raise e
50
- end
51
- end