bmc-daemon-lib 0.1.2 → 0.2.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -3
- data/bmc-daemon-lib.gemspec +1 -2
- data/lib/bmc-daemon-lib.rb +9 -1
- data/lib/bmc-daemon-lib/conf.rb +20 -17
- data/lib/bmc-daemon-lib/logger_helper.rb +0 -15
- data/lib/bmc-daemon-lib/logger_pool.rb +61 -0
- data/lib/bmc-daemon-lib/worker_base.rb +106 -0
- metadata +4 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd041b445612d4933a4f5f69ccb0158051892ed7
|
4
|
+
data.tar.gz: 5a4a9aa4dd013fb2369498d46380d5d815ebdaba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b98b2147d8abeb9b4f6e5214b363a14142c3beb7feff0d673b5b387a05d395f0b986f06df2735e7ea86b6dcb96c5994b01c5cd3568581a426fa2b4e1d552a813
|
7
|
+
data.tar.gz: 454958f16430d0be771750e5b88244d9619d3eb7e425b9c7f23bdce810fc2988c5ea22d01efc1a5b37d7c43c2c04be16894608009d61fc6ba525142860229d39
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
bmc-daemon-lib (0.
|
4
|
+
bmc-daemon-lib (0.2.0)
|
5
5
|
chamber (~> 2.9)
|
6
|
-
newrelic_rpm
|
7
6
|
|
8
7
|
GEM
|
9
8
|
remote: https://rubygems.org/
|
@@ -14,7 +13,6 @@ GEM
|
|
14
13
|
thor (~> 0.19.1)
|
15
14
|
diff-lcs (1.2.5)
|
16
15
|
hashie (3.4.4)
|
17
|
-
newrelic_rpm (3.16.0.318)
|
18
16
|
parser (2.3.1.2)
|
19
17
|
ast (~> 2.2)
|
20
18
|
powerpack (0.1.1)
|
data/bmc-daemon-lib.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
Gem::Specification.new do |spec|
|
3
3
|
# Project version
|
4
|
-
spec.version = "0.
|
4
|
+
spec.version = "0.2.0"
|
5
5
|
|
6
6
|
# Project description
|
7
7
|
spec.name = "bmc-daemon-lib"
|
@@ -27,5 +27,4 @@ Gem::Specification.new do |spec|
|
|
27
27
|
|
28
28
|
# Runtime dependencies
|
29
29
|
spec.add_runtime_dependency "chamber", "~> 2.9"
|
30
|
-
spec.add_runtime_dependency "newrelic_rpm"
|
31
30
|
end
|
data/lib/bmc-daemon-lib.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
+
# Global libs
|
2
|
+
require "rubygems"
|
3
|
+
require "syslog"
|
4
|
+
require "thread"
|
5
|
+
|
6
|
+
|
7
|
+
# Project's libs
|
1
8
|
require_relative "bmc-daemon-lib/conf"
|
2
9
|
require_relative "bmc-daemon-lib/logger_formatter"
|
3
10
|
require_relative "bmc-daemon-lib/logger_helper"
|
4
|
-
|
11
|
+
require_relative "bmc-daemon-lib/logger_pool"
|
12
|
+
require_relative "bmc-daemon-lib/worker_base"
|
data/lib/bmc-daemon-lib/conf.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
require "chamber"
|
3
3
|
|
4
4
|
module BmcDaemonLib
|
5
|
+
# Class exceptions
|
6
|
+
class ConfigInitiRequired < StandardError; end
|
5
7
|
class ConfigMissingParameter < StandardError; end
|
6
8
|
class ConfigOtherError < StandardError; end
|
7
9
|
class ConfigParseError < StandardError; end
|
@@ -53,8 +55,10 @@ module BmcDaemonLib
|
|
53
55
|
# Now we know app_name, initalize app_libs
|
54
56
|
@app_libs = File.expand_path("lib/#{@app_name}/", @app_root)
|
55
57
|
|
58
|
+
# By default, Newrelic is disabled
|
59
|
+
ENV["NEWRELIC_AGENT_ENABLED"] = "false"
|
60
|
+
|
56
61
|
# Add other config files
|
57
|
-
#add_default_config
|
58
62
|
add_config generate(:config_defaults)
|
59
63
|
add_config generate(:config_etc)
|
60
64
|
|
@@ -108,7 +112,7 @@ module BmcDaemonLib
|
|
108
112
|
|
109
113
|
def self.newrelic_enabled?
|
110
114
|
ensure_init
|
111
|
-
self[:newrelic] && self[:newrelic][:
|
115
|
+
!! (self[:newrelic] && self[:newrelic][:license])
|
112
116
|
end
|
113
117
|
|
114
118
|
# Defaults generators
|
@@ -156,21 +160,16 @@ module BmcDaemonLib
|
|
156
160
|
|
157
161
|
def self.prepare_newrelic section, logfile
|
158
162
|
# Disable NewRelic if no config present
|
159
|
-
unless self.newrelic_enabled?
|
160
|
-
ENV["NEWRELIC_AGENT_ENABLED"] = "false"
|
161
|
-
return
|
162
|
-
end
|
163
|
+
return unless self.newrelic_enabled?
|
163
164
|
|
164
165
|
# Enable GC profiler
|
165
166
|
GC::Profiler.enable
|
166
167
|
|
167
|
-
#
|
168
|
-
ENV["
|
168
|
+
# Set logfile, license, monitor mode
|
169
|
+
ENV["NEW_RELIC_LOG"] = logfile.to_s if logfile
|
170
|
+
ENV["NEW_RELIC_LICENSE_KEY"] = section[:license].to_s
|
169
171
|
ENV["NEW_RELIC_MONITOR_MODE"] = "true"
|
170
172
|
|
171
|
-
# License
|
172
|
-
ENV["NEW_RELIC_LICENSE_KEY"] = section[:licence].to_s
|
173
|
-
|
174
173
|
# Build NewRelic app_name if not provided as-is
|
175
174
|
if section[:app_name]
|
176
175
|
ENV["NEW_RELIC_APP_NAME"] = section[:app_name].to_s
|
@@ -183,18 +182,22 @@ module BmcDaemonLib
|
|
183
182
|
ENV["NEW_RELIC_APP_NAME"] = "#{text}-#{host};#{text}"
|
184
183
|
end
|
185
184
|
|
186
|
-
#
|
187
|
-
ENV["
|
185
|
+
# Enable module
|
186
|
+
ENV["NEWRELIC_AGENT_ENABLED"] = "true"
|
188
187
|
end
|
189
188
|
|
190
189
|
private
|
191
190
|
|
192
191
|
def self.ensure_init
|
193
|
-
|
194
|
-
|
192
|
+
unless @initialized
|
193
|
+
fail ConfigInitiRequired, "ensure_init: Conf.init(app_root) should be invoked beforehand"
|
194
|
+
end
|
195
|
+
|
196
|
+
# # Skip is already done
|
197
|
+
# return if @initialized
|
195
198
|
|
196
|
-
# Go through init if not already done
|
197
|
-
self.init
|
199
|
+
# # Go through init if not already done
|
200
|
+
# self.init
|
198
201
|
end
|
199
202
|
|
200
203
|
end
|
@@ -39,12 +39,6 @@ module BmcDaemonLib
|
|
39
39
|
|
40
40
|
def build_messages severity, message, details = nil
|
41
41
|
messages = []
|
42
|
-
# messages << "/---------------------------------------"
|
43
|
-
# messages << "severity: #{severity}"
|
44
|
-
# messages << "message: #{message.class}"
|
45
|
-
# messages << "details: #{details.class} #{details.inspect}"
|
46
|
-
# messages << "ARRAY(#{details.count})" if details.is_a? Array
|
47
|
-
# messages << "HASH(#{details.count})" if details.is_a? Hash
|
48
42
|
|
49
43
|
prefix = build_prefix
|
50
44
|
|
@@ -62,17 +56,8 @@ module BmcDaemonLib
|
|
62
56
|
end if details.is_a? Hash
|
63
57
|
|
64
58
|
# Return all that stuff
|
65
|
-
# messages << "\\---------------------------------------"
|
66
59
|
logger.add severity, messages
|
67
60
|
end
|
68
61
|
|
69
|
-
# def debug_lines lines, prefix = ''
|
70
|
-
# if lines.is_a? Array
|
71
|
-
# logger.debug lines.map{ |line| sprintf(LOG_MESSAGE_ARRAY, prefix, line) }
|
72
|
-
# elsif lines.is_a? Hash
|
73
|
-
# logger.debug lines.map{ |key, value| sprintf(LOG_MESSAGE_HASH, prefix, key, value) }
|
74
|
-
# end
|
75
|
-
# end
|
76
|
-
|
77
62
|
end
|
78
63
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "logger"
|
2
|
+
require "singleton"
|
3
|
+
|
4
|
+
# Logger interface class to access logger though symbolic names
|
5
|
+
module BmcDaemonLib
|
6
|
+
class LoggerPool
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
def get pipe
|
10
|
+
@loggers ||= {}
|
11
|
+
@loggers[pipe] ||= create(pipe)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create pipe
|
15
|
+
# Compute logfile or STDERR, and declare what we're doing
|
16
|
+
filename = logfile(pipe)
|
17
|
+
|
18
|
+
# Create the logger and return it
|
19
|
+
logger = Logger.new(filename, LOG_ROTATION) #, 10, 1024000)
|
20
|
+
logger.progname = pipe.to_s.downcase
|
21
|
+
logger.formatter = LoggerFormatter
|
22
|
+
|
23
|
+
# Finally return this logger
|
24
|
+
logger
|
25
|
+
|
26
|
+
rescue Errno::EACCES
|
27
|
+
puts "logging [#{pipe}] failed: access error"
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
|
32
|
+
def logfile pipe
|
33
|
+
# Disabled if no valid config
|
34
|
+
return nil unless Conf[:logs].is_a?(Hash)
|
35
|
+
|
36
|
+
# Compute logfile and check if we can write there
|
37
|
+
logfile = File.expand_path(Conf[:logs][pipe].to_s, Conf[:logs][:path].to_s)
|
38
|
+
|
39
|
+
# Check that we'll be able to create logfiles
|
40
|
+
if File.exists?(logfile)
|
41
|
+
# File is there, is it writable ?
|
42
|
+
unless File.writable?(logfile)
|
43
|
+
puts "logging [#{pipe}] disabled: file not writable [#{logfile}]"
|
44
|
+
return nil
|
45
|
+
end
|
46
|
+
else
|
47
|
+
# No file here, can we create it ?
|
48
|
+
logdir = File.dirname(logfile)
|
49
|
+
unless File.writable?(logdir)
|
50
|
+
puts "logging [#{pipe}] disabled: directory not writable [#{logdir}]"
|
51
|
+
return nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# OK, return a clean file path
|
56
|
+
puts "logging [#{pipe}] to [#{logfile}]"
|
57
|
+
return logfile
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module BmcDaemonLib
|
2
|
+
class WorkerBase
|
3
|
+
include LoggerHelper
|
4
|
+
|
5
|
+
# Class options
|
6
|
+
attr_reader :logger
|
7
|
+
attr_reader :pool
|
8
|
+
attr_reader :wid
|
9
|
+
|
10
|
+
def initialize wid, pool = nil
|
11
|
+
# Logger
|
12
|
+
@logger = LoggerPool.instance.get :workers
|
13
|
+
@log_worker_status_changes = true
|
14
|
+
|
15
|
+
# Configuration
|
16
|
+
@config = {}
|
17
|
+
|
18
|
+
# Set thread context
|
19
|
+
@pool = pool
|
20
|
+
@wid = wid
|
21
|
+
Thread.current.thread_variable_set :pool, pool
|
22
|
+
Thread.current.thread_variable_set :wid, wid
|
23
|
+
Thread.current.thread_variable_set :started_at, Time.now
|
24
|
+
worker_status WORKER_STATUS_STARTING
|
25
|
+
|
26
|
+
# Ask worker to init itself, and return if there are errors
|
27
|
+
if worker_init_result = worker_init
|
28
|
+
log_error "worker_init aborting: #{worker_init_result.inspect}", @config
|
29
|
+
else
|
30
|
+
# We're ok, let's start out loop
|
31
|
+
start_loop
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
# Worker methods prototypes
|
38
|
+
def worker_init
|
39
|
+
end
|
40
|
+
def worker_after
|
41
|
+
end
|
42
|
+
def worker_process
|
43
|
+
end
|
44
|
+
def worker_config
|
45
|
+
end
|
46
|
+
|
47
|
+
def log_prefix
|
48
|
+
[
|
49
|
+
Thread.current.thread_variable_get(:wid),
|
50
|
+
Thread.current.thread_variable_get(:jid),
|
51
|
+
nil
|
52
|
+
]
|
53
|
+
end
|
54
|
+
|
55
|
+
def start_loop
|
56
|
+
log_info "start_loop starting", @config
|
57
|
+
loop do
|
58
|
+
begin
|
59
|
+
# Do the hard work
|
60
|
+
worker_process
|
61
|
+
|
62
|
+
# Do the cleaning/sleeping stuff
|
63
|
+
worker_after
|
64
|
+
|
65
|
+
rescue StandardError => e
|
66
|
+
log_error "WORKER EXCEPTION: #{e.inspect}"
|
67
|
+
sleep 1
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def worker_status status, job = nil
|
73
|
+
# Update thread variables
|
74
|
+
Thread.current.thread_variable_set :status, status
|
75
|
+
Thread.current.thread_variable_set :updated_at, Time.now
|
76
|
+
|
77
|
+
# Nothin' to log if "silent"
|
78
|
+
return unless @log_worker_status_changes
|
79
|
+
|
80
|
+
# Log this status change
|
81
|
+
if job.is_a?(Job)
|
82
|
+
log_info "status [#{status}] on job[#{job.id}] status[#{job.status}] error[#{job.error}]"
|
83
|
+
else
|
84
|
+
log_info "status [#{status}]"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def worker_jid jid
|
89
|
+
Thread.current.thread_variable_set :jid, jid
|
90
|
+
Thread.current.thread_variable_set :updated_at, Time.now
|
91
|
+
end
|
92
|
+
|
93
|
+
def config_section key
|
94
|
+
# Debugging
|
95
|
+
@log_worker_status_changes = @debug
|
96
|
+
|
97
|
+
# Set my configuration
|
98
|
+
if (Conf[key].is_a? Hash) && Conf[key]
|
99
|
+
@config = Conf[key]
|
100
|
+
else
|
101
|
+
log_error "missing [#{key}] configuration"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bmc-daemon-lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno MEDICI
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.9'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: newrelic_rpm
|
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'
|
97
83
|
description: 'Shared utilities to build a daemon: logger, configuration, helpers'
|
98
84
|
email: bmc-daemon-lib@bmconseil.com
|
99
85
|
executables: []
|
@@ -111,6 +97,8 @@ files:
|
|
111
97
|
- lib/bmc-daemon-lib/conf.rb
|
112
98
|
- lib/bmc-daemon-lib/logger_formatter.rb
|
113
99
|
- lib/bmc-daemon-lib/logger_helper.rb
|
100
|
+
- lib/bmc-daemon-lib/logger_pool.rb
|
101
|
+
- lib/bmc-daemon-lib/worker_base.rb
|
114
102
|
homepage: http://github.com/bmedici/bmc-daemon-lib
|
115
103
|
licenses:
|
116
104
|
- MIT
|