servitude 1.2.1 → 2.0.0

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: 17a6491898797ad45347d09d899bd9ad38023115
4
- data.tar.gz: d0725d50f1c40dba41190a1ee4a980e0337765e4
3
+ metadata.gz: 80d47c3acb94c6f7d9e74314a4229743841a959c
4
+ data.tar.gz: 18004ef4ba391377f0fb380f126e53fb395f1e5f
5
5
  SHA512:
6
- metadata.gz: 0c507b9f2510bdc856ad094d855290d7b0714f74d8ccd76f1e4d0fbcb391e973576a551cfe3fcd287aff38d7abc64496e7d2266e873c74ed53b94d3cceb28200
7
- data.tar.gz: 6e90125ea1bdc65bc9c26990fde6ed414c1a552c311575115334fb05a7529aea6f33d44db4a51f4730ee2a5a56bdd4a95a76fa0392b327c9b6ca422ce701b891
6
+ metadata.gz: 6dba8c112362cfb824c127afa17d1bf72651e32e7472d9a322adf1eec9b968a1aced4ad3521422b96ffcc3592ff254da13f018cfb24286810f54319d06c96e39
7
+ data.tar.gz: f99cd105a9bb25c239e0fb2db98a50702678fab769a9f1634ef56c1775f54b3ad329701437883d8a9731ad2f2ed45db0d89a4cfbab31f29fd4fae5753f5e4371
@@ -22,13 +22,12 @@ module SimpleServer
22
22
 
23
23
  PROJECT_ROOT = File.expand_path( '../..', __FILE__ )
24
24
 
25
- boot host_namespace: SimpleServer,
26
- app_id: 'simple-server',
25
+ boot app_id: 'simple-server',
27
26
  app_name: 'Simple Server',
28
27
  attribution: "v#{VERSION} \u00A9#{Time.now.year} LFE",
29
28
  author: 'LFE',
30
29
  use_config: false,
31
- default_config_path: nil,
30
+ default_config_path: nil
32
31
 
33
32
  class Server
34
33
 
@@ -28,13 +28,12 @@ module EchoServer
28
28
 
29
29
  PROJECT_ROOT = File.expand_path( '../..', __FILE__ )
30
30
 
31
- boot host_namespace: EchoServer,
32
- app_id: 'echo-server',
31
+ boot app_id: 'echo-server',
33
32
  app_name: 'Echo Server',
34
33
  attribution: "v#{VERSION} \u00A9#{Time.now.year} LFE",
35
34
  author: 'LFE',
36
35
  use_config: false,
37
- default_config_path: nil,
36
+ default_config_path: nil
38
37
 
39
38
  class Server
40
39
 
@@ -39,13 +39,12 @@ module EchoServer
39
39
 
40
40
  PROJECT_ROOT = File.expand_path( '../..', __FILE__ )
41
41
 
42
- boot host_namespace: EchoServer,
43
- app_id: 'echo-server-with-cli',
42
+ boot app_id: 'echo-server-with-cli',
44
43
  app_name: 'Echo Server With CLI',
45
- attribution: "v#{VERSION} \u00A9#{Time.now.year} LFE",
46
44
  author: 'LFE',
45
+ attribution: "v#{VERSION} \u00A9#{Time.now.year} LFE",
47
46
  use_config: false,
48
- default_config_path: "#{PROJECT_ROOT}}/config/#{APP_FOLDER}.conf",
47
+ default_config_path: "#{PROJECT_ROOT}}/config/#{APP_FOLDER}.conf"
49
48
 
50
49
  class Cli < Servitude::Cli::Service
51
50
  end
@@ -39,13 +39,12 @@ module EchoServer
39
39
 
40
40
  PROJECT_ROOT = File.expand_path( '../..', __FILE__ )
41
41
 
42
- boot host_namespace: EchoServer,
43
- app_id: 'echo-server-with-cli-and-file-config',
42
+ boot app_id: 'echo-server-with-cli-and-file-config',
44
43
  app_name: 'Echo Server With CLI And File Config',
45
44
  attribution: "v#{VERSION} \u00A9#{Time.now.year} LFE",
46
45
  author: 'LFE',
47
46
  use_config: true,
48
- default_config_path: "#{PROJECT_ROOT}/config/4.conf",
47
+ default_config_path: "#{PROJECT_ROOT}/config/4.conf"
49
48
 
50
49
  class Cli < Servitude::Cli::Service
51
50
  end
@@ -47,8 +47,7 @@ module EchoServer
47
47
 
48
48
  PROJECT_ROOT = File.expand_path( '../..', __FILE__ )
49
49
 
50
- boot host_namespace: EchoServer,
51
- app_id: 'echo-server-with-cli-and-env-config',
50
+ boot app_id: 'echo-server-with-cli-and-env-config',
52
51
  app_name: 'Echo Server With CLI And Env Config',
53
52
  attribution: "v#{VERSION} \u00A9#{Time.now.year} LFE",
54
53
  author: 'LFE',
@@ -22,35 +22,4 @@ module Servitude
22
22
  INT = "INT"
23
23
  TERM = "TERM"
24
24
 
25
- class << self
26
- attr_accessor :boot_called, :configuration, :logger
27
-
28
- def initialize_loggers( log_level: nil, filename: nil )
29
- raise ArgumentError, 'log_level keyword is required' unless log_level
30
-
31
- logger.adapter.close if logger && logger.adapter
32
-
33
- self.logger = Yell.new do |l|
34
- l.level = log_level
35
- if filename
36
- l.adapter :file, filename, :level => [:debug, :info, :warn]
37
- else
38
- l.adapter $stdout, :level => [:debug, :info, :warn]
39
- l.adapter $stderr, :level => [:error, :fatal]
40
- end
41
- end
42
- end
43
-
44
- def server_class
45
- case SERVER_CLASS
46
- when String, Symbol
47
- eval SERVER_CLASS.to_s, binding, __FILE__, __LINE__
48
- else
49
- SERVER_CLASS
50
- end
51
- end
52
- end
53
-
54
- Servitude.initialize_loggers log_level: :info
55
-
56
25
  end
@@ -9,86 +9,80 @@ module Servitude
9
9
  base.extend( ClassMethods )
10
10
  base.class_eval do
11
11
  class << self
12
- attr_accessor :logger
12
+ attr_accessor :boot_called,
13
+ :configuration,
14
+ :logger
13
15
  end
14
16
  end
15
17
  end
16
18
 
17
19
  module ClassMethods
18
20
 
19
- def boot( host_namespace: nil,
20
- app_id: ( host_namespace.name.split( '::' ).join( '-' ).downcase rescue nil ),
21
+ def boot( app_id: ( host_namespace.name.split( '::' ).join( '-' ).downcase rescue nil ),
21
22
  app_name: ( host_namespace.name.split( '::' ).join( ' ' ) rescue nil ),
22
23
  author: nil,
23
- company: nil, # TODO: Remove when company keyword deprecation expires
24
- attribution: ( "v#{host_namespace::VERSION} Copyright © #{Time.now.year} #{author || company}" rescue nil ),
24
+ attribution: ( "v#{host_namespace::VERSION} Copyright © #{Time.now.year} #{author}" rescue nil ),
25
25
  use_config: false,
26
26
  default_config_path: nil,
27
- server_class: ( host_namespace::Server rescue "#{host_namespace.name}::Server" ),
28
- version_copyright: nil ) # TODO: Remove when version_copyright keyword deprecation expires
29
- unless host_namespace
30
- raise ArgumentError, 'host_namespace keyword is required'
31
- end
27
+ server_class: ( host_namespace::Server rescue "#{host_namespace.name}::Server" ))
32
28
  unless app_id
33
29
  raise ArgumentError, 'app_id keyword is required'
34
30
  end
35
31
  unless app_name
36
32
  raise ArgumentError, 'app_name keyword is required'
37
33
  end
38
- unless author || company
34
+ unless author
39
35
  raise ArgumentError, 'author keyword is required'
40
36
  end
41
- unless attribution || version_copyright
37
+ unless attribution
42
38
  raise ArgumentError, 'attribution keyword is required'
43
39
  end
44
40
 
45
- # TODO: Remove when company keyword deprecation expires
46
- if company
47
- Util.deprecate "#{Base.name}.boot company: #{company.inspect}",
48
- "#{Base.name}.boot author: #{company.inspect}"
49
- author = company
50
- end
51
-
52
41
  unless server_class
53
42
  raise ArgumentError, 'server_class keyword is required'
54
43
  end
55
44
 
56
- # TODO: Remove when version_copyright keyword deprecation expires
57
- if version_copyright
58
- Util.deprecate "#{Base.name}.boot version_copyright: #{version_copyright.inspect}",
59
- "#{Base.name}.boot attribution: #{version_copyright.inspect}"
60
- attribution = version_copyright
61
- end
62
-
63
- # TODO: Remove when host namespace deprecation expires
64
45
  const_set :APP_ID, app_id
65
46
  const_set :APP_NAME, app_name
66
47
  const_set :AUTHOR, author
67
- const_set :COMPANY, author # TODO: Remove when company keyword deprecation expires
68
48
  const_set :ATTRIBUTION, attribution
69
49
  const_set :DEFAULT_CONFIG_PATH, default_config_path
50
+ const_set :SERVER_CLASS, server_class
70
51
  const_set :USE_CONFIG, use_config
71
- const_set :VERSION_COPYRIGHT, attribution # TODO: Remove when version_copyright keyword deprecation expires
72
52
 
73
- Servitude.const_set :APP_ID, app_id
74
- Servitude.const_set :APP_NAME, app_name
75
- Servitude.const_set :AUTHOR, author
76
- Servitude.const_set :COMPANY, author # TODO: Remove when company keyword deprecation expires
77
- Servitude.const_set :ATTRIBUTION, attribution
78
- Servitude.const_set :DEFAULT_CONFIG_PATH, default_config_path
79
- Servitude.const_set :SERVER_CLASS, server_class
80
- Servitude.const_set :USE_CONFIG, use_config
81
- Servitude.const_set :VERSION_COPYRIGHT, attribution # TODO: Remove when version_copyright keyword deprecation expires
53
+ host_namespace.boot_called = true
54
+ end
82
55
 
83
- Servitude::boot_called = true
56
+ # Override to contradict convention of Server being nested
57
+ # in ::host_namespace
58
+ #
59
+ def host_namespace
60
+ self
84
61
  end
85
62
 
86
- def configuration
87
- @configuration
63
+ def server_class
64
+ case self::SERVER_CLASS
65
+ when String, Symbol
66
+ eval self::SERVER_CLASS.to_s, binding, __FILE__, __LINE__
67
+ else
68
+ self::SERVER_CLASS
69
+ end
88
70
  end
89
71
 
90
- def configuration=( configuration )
91
- @configuration = configuration
72
+ def initialize_loggers( log_level: nil, filename: nil )
73
+ raise ArgumentError, 'log_level keyword is required' unless log_level
74
+
75
+ logger.adapter.close if logger && logger.adapter
76
+
77
+ self.logger = Yell.new do |l|
78
+ l.level = log_level
79
+ if filename
80
+ l.adapter :file, filename, :level => [:debug, :info, :warn]
81
+ else
82
+ l.adapter $stdout, :level => [:debug, :info, :warn]
83
+ l.adapter $stderr, :level => [:error, :fatal]
84
+ end
85
+ end
92
86
  end
93
87
 
94
88
  def configure
@@ -11,7 +11,7 @@ module Servitude
11
11
  end
12
12
 
13
13
  def self.common_start_options
14
- method_option :config, type: :string, aliases: '-c', desc: "The path for the config file", default: Servitude::DEFAULT_CONFIG_PATH
14
+ method_option :config, type: :string, aliases: '-c', desc: "The path for the config file"
15
15
  environment_option
16
16
  method_option :log_level, desc: "The log level", type: :string, aliases: '-o'
17
17
  method_option :log, desc: "The path for the log file", type: :string, aliases: '-l'
@@ -41,12 +41,14 @@ module Servitude
41
41
  no_commands do
42
42
 
43
43
  def start_interactive
44
- server = Servitude::server_class.new( configuration( options, use_config: Servitude::USE_CONFIG, log: 'STDOUT' ))
44
+ server = host_namespace::server_class.new( configuration( options, use_config: host_namespace::USE_CONFIG, log: 'STDOUT' ))
45
45
  server.start
46
46
  end
47
47
 
48
48
  def start_daemon
49
- server = Servitude::Daemon.new( configuration( options, use_config: Servitude::USE_CONFIG ))
49
+ server = Servitude::Daemon.new( host_namespace::APP_NAME,
50
+ host_namespace::server_class,
51
+ configuration( options, use_config: host_namespace::USE_CONFIG ))
50
52
  server.start
51
53
  end
52
54
 
@@ -55,8 +57,16 @@ module Servitude
55
57
  end
56
58
 
57
59
  def configuration( options, additional_options={} )
60
+ unless options[:config]
61
+ options = options.merge( config: host_namespace::DEFAULT_CONFIG_PATH )
62
+ end
63
+
58
64
  options = options.merge( additional_options )
59
- Servitude.configuration = configuration_class.load( options )
65
+ host_namespace.configuration = configuration_class.load( host_namespace::DEFAULT_CONFIG_PATH, options )
66
+ end
67
+
68
+ def host_namespace
69
+ raise NotImplementedError
60
70
  end
61
71
 
62
72
  end
@@ -65,7 +75,9 @@ module Servitude
65
75
  pid_option
66
76
  method_option :quiet, type: :boolean, aliases: '-q', desc: "Do not prompt to remove an old PID file", default: false
67
77
  def status
68
- result = Servitude::Daemon.new( configuration( options, use_config: Servitude::USE_CONFIG )).status
78
+ result = Servitude::Daemon.new( host_namespace::APP_NAME,
79
+ host_namespace::server_class,
80
+ configuration( options, use_config: host_namespace::USE_CONFIG )).status
69
81
  at_exit { exit result }
70
82
  end
71
83
 
@@ -73,7 +85,9 @@ module Servitude
73
85
  pid_option
74
86
  method_option :quiet, type: :boolean, aliases: '-q', desc: "Do not prompt to remove an old PID file", default: false
75
87
  def stop
76
- server = Servitude::Daemon.new( configuration( options, use_config: Servitude::USE_CONFIG ))
88
+ server = Servitude::Daemon.new( host_namespace::APP_NAME,
89
+ host_namespace::server_class,
90
+ configuration( options, use_config: host_namespace::USE_CONFIG ))
77
91
  server.stop
78
92
  end
79
93
 
@@ -2,7 +2,11 @@ module Servitude
2
2
  module ConfigHelper
3
3
 
4
4
  def config
5
- Servitude.configuration
5
+ host_namespace.configuration
6
+ end
7
+
8
+ def host_namespace
9
+ raise NotImplementedError
6
10
  end
7
11
 
8
12
  end
@@ -1,25 +1,16 @@
1
1
  require 'hashie'
2
- #require 'oj'
3
2
  require 'pathname'
4
3
  require 'yaml'
5
4
 
6
5
  module Servitude
7
6
  class Configuration < Hashie::Mash
8
7
 
9
- def self.load( options={} )
10
- merged_options = defaults.merge( file_options )
8
+ def self.load( config_filepath, options={} )
9
+ merged_options = defaults.merge( file_options( config_filepath ))
11
10
  merged_options = merged_options.merge( options )
12
11
  new( merged_options )
13
12
  end
14
13
 
15
- def self.config_filepath
16
- Servitude::DEFAULT_CONFIG_PATH
17
- end
18
-
19
- def config_filepath
20
- Pathname.new( self.class.config_filepath )
21
- end
22
-
23
14
  def slice( *keys )
24
15
  klass.new( select { |k,v| keys.map( &:to_s ).include?( k ) } )
25
16
  end
@@ -39,15 +30,15 @@ module Servitude
39
30
  }
40
31
  end
41
32
 
42
- def self.file_options
33
+ def self.file_options( config_filepath )
43
34
  return {} unless config_filepath
44
35
 
45
36
  File.exists?( config_filepath ) ?
46
- load_file_options :
37
+ load_file_options( config_filepath ) :
47
38
  {}
48
39
  end
49
40
 
50
- def self.load_file_options
41
+ def self.load_file_options( config_filepath )
51
42
  YAML::load( File.read( config_filepath ))
52
43
  end
53
44
 
@@ -5,18 +5,22 @@ require 'timeout'
5
5
  module Servitude
6
6
  class Daemon
7
7
 
8
- attr_reader :name,
8
+ attr_reader :app_name,
9
+ :name,
9
10
  :options,
10
11
  :pid,
11
12
  :pid_path,
12
13
  :script,
14
+ :server_class,
13
15
  :timeout
14
16
 
15
- def initialize( options )
17
+ def initialize( app_name, server_class, options )
16
18
  @options = options
17
- @name = options[:name] || Servitude::APP_NAME
19
+ @app_name = app_name
20
+ @name = options[:name] || app_name
18
21
  @pid_path = options[:pid] || '.'
19
22
  @pid = get_pid
23
+ @server_class = server_class
20
24
  @timeout = options[:timeout] || 10
21
25
  end
22
26
 
@@ -37,7 +41,7 @@ module Servitude
37
41
  end
38
42
 
39
43
  def run
40
- Servitude::server_class.new( options ).start
44
+ server_class.new( options ).start
41
45
  end
42
46
 
43
47
  def stop
@@ -46,7 +50,7 @@ module Servitude
46
50
  remove_pid
47
51
  when :failed_to_stop
48
52
  when :does_not_exist
49
- puts "#{Servitude::APP_NAME} process is not running"
53
+ puts "#{app_name} process is not running"
50
54
  prompt_and_remove_pid_file if pid_file_exists? && !options[:quiet]
51
55
  else
52
56
  raise 'Unknown return code from #kill_process'
@@ -55,10 +59,10 @@ module Servitude
55
59
 
56
60
  def status
57
61
  if process_exists?
58
- puts "#{Servitude::APP_NAME} process running with PID: #{pid}"
62
+ puts "#{app_name} process running with PID: #{pid}"
59
63
  true
60
64
  else
61
- puts "#{Servitude::APP_NAME} process does not exist"
65
+ puts "#{app_name} process does not exist"
62
66
  prompt_and_remove_pid_file if pid_file_exists? && !options[:quiet]
63
67
  false
64
68
  end
@@ -117,7 +121,7 @@ module Servitude
117
121
  def kill_process
118
122
  return :does_not_exist unless process_exists?
119
123
 
120
- $stdout.write "Attempting to stop #{Servitude::APP_NAME} process #{pid}..."
124
+ $stdout.write "Attempting to stop #{app_name} process #{pid}..."
121
125
  Process.kill INT, pid
122
126
 
123
127
  iteration_num = 0
@@ -128,10 +132,10 @@ module Servitude
128
132
  end
129
133
 
130
134
  if process_exists?
131
- $stderr.puts "\nFailed to stop #{Servitude::APP_NAME} process #{pid}"
135
+ $stderr.puts "\nFailed to stop #{app_name} process #{pid}"
132
136
  return :failed_to_stop
133
137
  else
134
- $stdout.puts "\nSuccessfuly stopped #{Servitude::APP_NAME} process #{pid}"
138
+ $stdout.puts "\nSuccessfuly stopped #{app_name} process #{pid}"
135
139
  end
136
140
 
137
141
  return :success
@@ -6,7 +6,7 @@ module Servitude
6
6
  class EnvironmentConfiguration < Servitude::Configuration
7
7
 
8
8
  def for_env
9
- _config.send( _config.environment )
9
+ send( environment )
10
10
  end
11
11
 
12
12
  end
@@ -11,11 +11,15 @@ module Servitude
11
11
 
12
12
  define_method level do |*messages|
13
13
  messages.each do |message|
14
- Servitude.logger.send level, message
14
+ host_namespace.logger.send level, message
15
15
  end
16
16
  end
17
17
 
18
18
  end
19
19
 
20
+ def host_namespace
21
+ raise NotImplementedError
22
+ end
23
+
20
24
  end
21
25
  end
@@ -22,7 +22,7 @@ module Servitude
22
22
  end
23
23
 
24
24
  def initialize( cli_options={} )
25
- unless Servitude.boot_called
25
+ unless host_namespace.boot_called
26
26
  raise 'You must call boot before starting server'
27
27
  end
28
28
 
@@ -35,19 +35,33 @@ module Servitude
35
35
 
36
36
  def start
37
37
  log_startup
38
+ setup_signal_handling
39
+ run_sequence
40
+ sleep_sequence
41
+ end
38
42
 
39
- trap( INT ) { stop }
40
- trap( TERM ) { stop }
43
+ protected
44
+
45
+ def run
46
+ raise NotImplementedError
47
+ end
41
48
 
49
+ def run_sequence
42
50
  run_hook :before_run
43
51
  run
52
+ end
53
+
54
+ def sleep_sequence
44
55
  run_hook :before_sleep
45
56
  sleep
46
57
  end
47
58
 
48
- protected
59
+ def setup_signal_handling
60
+ trap( INT ) { stop }
61
+ trap( TERM ) { stop }
62
+ end
49
63
 
50
- def run
64
+ def host_namespace
51
65
  raise NotImplementedError
52
66
  end
53
67
 
@@ -6,12 +6,12 @@ module Servitude
6
6
  protected
7
7
 
8
8
  def initialize_loggers
9
- Servitude.initialize_loggers log_level: log_level
9
+ host_namespace.initialize_loggers log_level: log_level
10
10
  end
11
11
 
12
12
  def log_startup
13
13
  start_banner.each do |line|
14
- Servitude.logger.info line
14
+ host_namespace.logger.info line
15
15
  end
16
16
  end
17
17
 
@@ -19,19 +19,19 @@ module Servitude
19
19
  [
20
20
  "",
21
21
  "***",
22
- "* #{Servitude::APP_NAME} started",
22
+ "* #{host_namespace::APP_NAME} started",
23
23
  "*",
24
- "* #{Servitude::VERSION_COPYRIGHT}",
24
+ "* #{host_namespace::ATTRIBUTION}",
25
25
  "*",
26
- (Servitude.configuration.empty? ? nil : "* Configuration"),
27
- PrettyPrint::configuration_lines( Servitude.configuration, "* ", all_config_filters ),
28
- (Servitude.configuration.empty? ? nil : "*"),
26
+ ((host_namespace.configuration.nil? || host_namespace.configuration.empty?) ? nil : "* Configuration"),
27
+ PrettyPrint::configuration_lines( host_namespace.configuration, "* ", all_config_filters ),
28
+ ((host_namespace.configuration.nil? || host_namespace.configuration.empty?) ? nil : "*"),
29
29
  "***",
30
30
  ].flatten.reject( &:nil? )
31
31
  end
32
32
 
33
33
  def log_level
34
- (Servitude.configuration.log_level.to_sym rescue :info)
34
+ (host_namespace.configuration.log_level.to_sym rescue :info)
35
35
  end
36
36
 
37
37
  def all_config_filters
@@ -51,8 +51,12 @@ module Servitude
51
51
  []
52
52
  end
53
53
 
54
+ def host_namespace
55
+ raise NotImplementedError
56
+ end
57
+
54
58
  #def config_value( key )
55
- #value = Servitude.configuration.send( key )
59
+ #value = host_namespace.configuration.send( key )
56
60
 
57
61
  #return value unless value.is_a?( Hash )
58
62
 
@@ -1,3 +1,3 @@
1
1
  module Servitude
2
- VERSION = "1.2.1"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servitude
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Harrelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-16 00:00:00.000000000 Z
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler