eventhub-components 0.0.1 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cbba5765cb10c12c223815b1934db8b5e1d9e172
4
- data.tar.gz: e6b43f9c39845f88e426445f9ef3a80e384f6509
3
+ metadata.gz: 8c415e3a9835a07fecf3e5e28a8fa26d62ee58e1
4
+ data.tar.gz: 5bea0da5a68f6eb25437d7e48c1dbe5c5c4b32b5
5
5
  SHA512:
6
- metadata.gz: 8c6e9699a28d8927b8fb25f2b7f49712b13a8d11510c3bfd2bdddf0ab4a100812695b6edcac842472e7784ae48779da1727e313ce3e3d9cae4d3a604ec9f5d82
7
- data.tar.gz: 7e74d8599751dc01b6df9e7463fc39a8cc8050105cd39348cdbb2a4a199620ba642e16f31ea3abf5bab8185e3272ee249693c094c20adc44fdbed51f438278b3
6
+ metadata.gz: a0a2bcd8140e494793dcda886ede62a0ff4a3f8ccc6035042233cb046362a4546d8dc338b44131383db3df8e394a39bb932b39921d028be45b261e855895c090
7
+ data.tar.gz: 1a154f6db1b991d178f7f31a7de78979666447ef9541649d9faffb91a58eedeeb921eec4fdd267426ca00bc096804bcbcf161c011319c9310a21c37ac14743bf
data/.gitignore CHANGED
@@ -1,17 +1,17 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/README.md CHANGED
@@ -1,38 +1,73 @@
1
- # Eventhub::Components
2
-
3
- TODO: Write a gem description
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'eventhub-components'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install eventhub-components
18
-
19
- ## Usage
20
-
21
- This Gem provides shared code for processors and the console app.
22
-
23
- ### Logger
24
-
25
- Provides a second argument (a hash) to the log methods (debug, info, ...)
26
-
27
- ''''
28
- logger = Eventhub::Components::Logger.new(some_other_logger, 'app_name' => 'my fancy app', 'env' => 'staging')
29
- logger.info("my message", :foo => 1, :bar => 2)
30
- ''''
31
-
32
- ## Contributing
33
-
34
- 1. Fork it ( https://github.com/[my-github-username]/eventhub-components/fork )
35
- 2. Create your feature branch (`git checkout -b my-new-feature`)
36
- 3. Commit your changes (`git commit -am 'Add some feature'`)
37
- 4. Push to the branch (`git push origin my-new-feature`)
38
- 5. Create a new Pull Request
1
+ # EventHub::Components
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'eventhub-components'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install eventhub-components
18
+
19
+ ## Usage
20
+
21
+ This Gem provides shared code for processors and the console app.
22
+
23
+ ### Pid Files
24
+
25
+ Takes care of writing, reading and deleting a PID file
26
+
27
+ pid_file = EventHub::Components::PidFile.new('my_application.pid')
28
+ pid_file.write # writes Process.pid
29
+ # or
30
+ pid_file.write(some_process_pid)
31
+
32
+ pid_file.read # => "some_pid" (it's a string as it comes from a file)
33
+
34
+ pid_file.delete
35
+
36
+
37
+ ### Logging
38
+
39
+ #### StructuredDataLogger
40
+
41
+ Provides a second argument (a hash) to the log methods (debug, info, ...)
42
+
43
+ logger = EventHub::Components::StructuredDataLogger.new(some_other_logger, 'app_name' => 'my fancy app', 'env' => 'staging')
44
+ logger.info("my message", :foo => 1, :bar => 2)
45
+
46
+
47
+ #### MultiLogger
48
+
49
+ Forwards calls to all devices that have been added to the multilogger.
50
+
51
+ logger = EventHub::Components::MultiLogger.new
52
+ logger.add_device(some_other_logger_1)
53
+ logger.add_device(some_other_logger_2)
54
+ logger.info("Hans")
55
+
56
+ #### ExceptionWriter
57
+
58
+ Helps writing exceptions and log messages to files. It creates
59
+
60
+ writer = EventHub::Components::ExceptionWriter.new() # logs to ./exceptions
61
+ # or
62
+ writer = EventHub::Components::ExceptionWriter.new('some_dir', max_number_of_files) # logs to ./exceptions/some_dir
63
+
64
+ writer.write(e)
65
+
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/[my-github-username]/eventhub-components/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
@@ -5,11 +5,11 @@ require 'eventhub/components/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "eventhub-components"
8
- spec.version = Eventhub::Components::VERSION
8
+ spec.version = EventHub::Components::VERSION
9
9
  spec.authors = ["Novartis"]
10
10
  spec.email = ["pascal.betz@simplificator.com"]
11
- spec.summary = %q{Eventhub Components utilities}
12
- spec.description = %q{Eventhub Components utilities}
11
+ spec.summary = %q{EventHub Components utilities}
12
+ spec.description = %q{EventHub Components utilities}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
@@ -0,0 +1,62 @@
1
+ class EventHub::Components::ExceptionWriter
2
+
3
+ MAX_EXCEPTIONS_FILES = 500
4
+
5
+ attr_accessor :folder, :max_files
6
+
7
+ def initialize(base = nil, max_files = MAX_EXCEPTIONS_FILES)
8
+ base = base ||= Dir.pwd
9
+ @folder = File.join(base, 'exceptions')
10
+ @max_files = max_files
11
+ FileUtils.makedirs(folder)
12
+ end
13
+
14
+
15
+ def write(exception, message = nil)
16
+ time = Time.now
17
+ stamp = "#{time.strftime("%Y%m%d_%H%M%S")}_%i" % time.usec
18
+
19
+ write_exception("#{stamp}.log", exception)
20
+ write_message("#{stamp}.msg.raw", message)
21
+
22
+ restrict_to_max_files
23
+
24
+ stamp
25
+ end
26
+
27
+
28
+ private
29
+
30
+ def restrict_to_max_files
31
+ exception_files = Dir.glob(File.join(folder, '*.log'))
32
+ if exception_files.size > max_files
33
+ exception_files.reverse[max_files..-1].each do |file|
34
+ begin
35
+ File.delete(file)
36
+ raw = File.join(File.dirname(file), File.basename(file, ".*"), '.msg.raw')
37
+ File.delete(raw)
38
+ rescue
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def write_exception(filename, exception)
45
+ File.open("#{folder}/#{filename}", "w") do |output|
46
+ output.write("#{exception}\n\n")
47
+ output.write("Exception: #{exception.class.to_s}\n\n")
48
+ output.write("Call Stack:\n")
49
+ exception.backtrace.each do |line|
50
+ output.write("#{line}\n")
51
+ end if exception.backtrace
52
+ end
53
+ end
54
+
55
+ def write_message(filename, message)
56
+ return unless message
57
+ File.open("#{folder}/#{filename}","wb") do |output|
58
+ output.write(message)
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,7 @@
1
+ # format adaptation
2
+ class EventHub::Components::LogFormatter
3
+ def call(severity, time, progname, msg)
4
+ time_in_string = "#{time.strftime("%Y-%m-%d %H:%M:%S")}.#{"%04d" % (time.usec/100)}"
5
+ [time_in_string, Process.pid, severity, msg].join("\t") + "\n"
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ class EventHub::Components::MultiLogger < BasicObject
2
+
3
+ attr_accessor :devices
4
+
5
+ def initialize(folder=nil)
6
+ @devices = []
7
+ end
8
+
9
+ def add_device(device)
10
+ ::Kernel.raise ::ArgumentError.new("can not add nil device") if device.nil?
11
+ @devices << device
12
+ self
13
+ end
14
+
15
+
16
+ private
17
+
18
+ def method_missing(method, *args, &block)
19
+ devices.map do |target|
20
+ begin
21
+ target.send(method, *args, &block)
22
+ rescue => e
23
+ ::STDERR.puts "WARNING: Could not call #{method} in #{target} with #{args} because of #{e.message}"
24
+ e
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ class EventHub::Components::Pidfile
2
+ attr_reader :file_name
3
+ def initialize(file_name)
4
+ @file_name = file_name
5
+ end
6
+
7
+ # write the pid to the file specified in the initializer
8
+ # defaults to Process.pid
9
+ def write(pid = Process.pid)
10
+ FileUtils.makedirs(File.dirname(file_name))
11
+ IO.write(file_name, pid.to_s)
12
+ end
13
+
14
+ # Try to delete file, ignore all errors
15
+ def delete
16
+ begin
17
+ File.delete(file_name)
18
+ rescue
19
+ # ignore
20
+ end
21
+ end
22
+
23
+ # Read the PID from the file
24
+ def read
25
+ begin
26
+ File.read(file_name)
27
+ rescue Errno::ENOENT => e
28
+ # ignore, will return nil
29
+ end
30
+ end
31
+ end
@@ -3,7 +3,7 @@ require 'thread'
3
3
 
4
4
  # A wrapper for loggers to enrich the log message with structured data (a Hash).
5
5
  # All methods besides debug/info/warn/error/fatal are forwarded to the target by the means of method_missing.
6
- class Eventhub::Components::Logger < BasicObject
6
+ class EventHub::Components::StructuredDataLogger < BasicObject
7
7
  SEVERITY_DEBUG = 7
8
8
  SEVERITY_INFO = 6
9
9
  SEVERITY_WARNING = 4
@@ -1,5 +1,5 @@
1
- module Eventhub
1
+ module EventHub
2
2
  module Components
3
- VERSION = "0.0.1"
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
@@ -1,8 +1,14 @@
1
- module Eventhub
1
+ module EventHub
2
2
  module Components
3
3
  end
4
4
  end
5
5
 
6
+ require 'fileutils'
7
+ require 'logger'
6
8
  require_relative "components/version"
7
- require_relative "components/logger"
9
+ require_relative "components/log_formatter"
10
+ require_relative "components/multi_logger"
11
+ require_relative "components/structured_data_logger"
12
+ require_relative "components/exception_writer"
13
+ require_relative "components/pid_file"
8
14
 
@@ -0,0 +1,54 @@
1
+ require_relative '../spec_helper'
2
+
3
+ RSpec.describe EventHub::Components::StructuredDataLogger do
4
+ context 'logger' do
5
+
6
+ before(:each) do
7
+ remove_all_exception_files
8
+ end
9
+
10
+ after(:each) do
11
+ remove_all_exception_files
12
+ end
13
+
14
+ let(:writer) {
15
+ EventHub::Components::ExceptionWriter.new('tmp', 5)
16
+ }
17
+
18
+ it 'writes an exception' do
19
+ begin
20
+ raise
21
+ rescue => e
22
+ name = writer.write(e)
23
+ end
24
+ expect(Dir['tmp/exceptions/*'].size).to eq(1)
25
+ end
26
+
27
+ it 'writes an exception and a message' do
28
+ begin
29
+ raise
30
+ rescue => e
31
+ name = writer.write(e, "hi")
32
+ end
33
+ expect(Dir['tmp/exceptions/*'].size).to eq(2)
34
+ end
35
+
36
+ it 'limits number of files' do
37
+ begin
38
+ raise
39
+ rescue => e
40
+ (writer.max_files + 1).times do
41
+ writer.write(e)
42
+ end
43
+ end
44
+ expect(Dir['tmp/exceptions/*'].size).to eq(5)
45
+ end
46
+
47
+ end
48
+
49
+ def remove_all_exception_files
50
+ Dir['tmp/exceptions/*'].each do |file|
51
+ File.delete(file)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,47 @@
1
+ require_relative '../spec_helper'
2
+
3
+ RSpec.describe EventHub::Components::StructuredDataLogger do
4
+ context 'logger' do
5
+
6
+ let(:logger) {
7
+ EventHub::Components::MultiLogger.new
8
+ }
9
+
10
+
11
+ # it 'forwards calls to ALL devices' do
12
+ # device_1 = stub(:device_1)
13
+ # expect(device_1).to receive(:info).and_return(1)
14
+ # device_2 = stub(:device_2)
15
+ # expect(device_2).to receive(:info).and_return(2)
16
+
17
+ # logger.add_device(device_1)
18
+ # logger.add_device(device_2)
19
+
20
+ # result = logger.info("hans")
21
+ # expect(result).to eq([1, 2])
22
+ # end
23
+
24
+
25
+ # it 'forwards calls to ALL devices even if one raises' do
26
+ # device_1 = stub(:device_1)
27
+ # expect(device_1).to receive(:info).and_raise
28
+ # device_2 = stub(:device_2)
29
+ # expect(device_2).to receive(:info).and_return(2)
30
+
31
+ # logger.add_device(device_1)
32
+ # logger.add_device(device_2)
33
+
34
+ # result = logger.info("hans")
35
+ # end
36
+
37
+ # it 'returns self after adding device' do
38
+ # expect(logger.add_device("something")).to eq(logger)
39
+ # end
40
+
41
+ it 'does not allow nil as device' do
42
+ expect {
43
+ logger.add_device nil
44
+ }.to raise_error(ArgumentError)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe EventHub::Components::Pidfile do
5
+ before(:each) do
6
+ FileUtils.remove_dir('/tmp/eventhub_pid_test', true)
7
+ end
8
+
9
+ after(:each) do
10
+ FileUtils.remove_dir('/tmp/eventhub_pid_test', true)
11
+ end
12
+
13
+ let(:pidfile) { pidfile = EventHub::Components::Pidfile.new('/tmp/eventhub_pid_test/some.pid') }
14
+
15
+ it 'creates the folders if not existing' do
16
+ pidfile.write(1234)
17
+ expect(File.directory?('/tmp/eventhub_pid_test')).to be true
18
+ end
19
+
20
+ it 'writes the content to the file' do
21
+ pidfile.write(1234)
22
+ expect(IO.read('/tmp/eventhub_pid_test/some.pid')).to eq('1234')
23
+ end
24
+
25
+ it 'deletes the file' do
26
+ pidfile.write(1234)
27
+ pidfile.delete
28
+ expect(File.file?('/tmp/eventhub_pid_test/some.pid')).to be false
29
+ end
30
+
31
+ it 'does not choke when deleting a non-existing pid file' do
32
+ pidfile.delete
33
+ end
34
+
35
+ it 'does not choke when reading a non-existing pid file' do
36
+ expect(pidfile.read).to eq(nil)
37
+ end
38
+
39
+ it 'reads the pid written to the file' do
40
+ pidfile.write(1234)
41
+ expect(pidfile.read).to eq("1234")
42
+ end
43
+
44
+ it 'it writes the currents process pid as default' do
45
+ pidfile.write
46
+ expect(pidfile.read).to eq(Process.pid.to_s)
47
+ end
48
+ end
@@ -1,25 +1,25 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
- RSpec.describe Eventhub::Components::Logger do
3
+ RSpec.describe EventHub::Components::StructuredDataLogger do
4
4
  context 'initalize' do
5
5
  it 'no raise when all required arguments are given' do
6
6
  expect {
7
- Eventhub::Components::Logger.new("something not nil", 'app_name' => 'an app', 'env' => 'test')
7
+ EventHub::Components::StructuredDataLogger.new("something not nil", 'app_name' => 'an app', 'env' => 'test')
8
8
  }.to_not raise_error
9
9
  end
10
10
  it 'requires app_name' do
11
11
  expect {
12
- Eventhub::Components::Logger.new("something not nil", 'env' => 'test')
12
+ EventHub::Components::StructuredDataLogger.new("something not nil", 'env' => 'test')
13
13
  }.to raise_error(ArgumentError)
14
14
  end
15
15
  it 'requires app_name' do
16
16
  expect {
17
- Eventhub::Components::Logger.new("something not nil", 'app_name' => 'an app')
17
+ EventHub::Components::StructuredDataLogger.new("something not nil", 'app_name' => 'an app')
18
18
  }.to raise_error(ArgumentError)
19
19
  end
20
20
 
21
21
  it 'adds the default options' do
22
- logger = Eventhub::Components::Logger.new("something not nil", 'app_name' => 'an app', 'env' => 'test')
22
+ logger = EventHub::Components::StructuredDataLogger.new("something not nil", 'app_name' => 'an app', 'env' => 'test')
23
23
  expect(logger.options['pid']).to eq(::Process.pid)
24
24
  expect(logger.options['hostname']).to eq(::Socket.gethostname)
25
25
  end
@@ -30,15 +30,15 @@ RSpec.describe Eventhub::Components::Logger do
30
30
  # fake logger that stores a log message
31
31
  # in hash
32
32
  fake_logger = Hash.new
33
- def l.debug(message); self[:debug] = message; end
34
- def l.info(message); self[:info] = message; end
35
- def l.warn(message); self[:warn] = message; end
36
- def l.error(message); self[:error] = message; end
37
- def l.fatal(message); self[:fatal] = message; end
33
+ def fake_logger.debug(message); self[:debug] = message; end
34
+ def fake_logger.info(message); self[:info] = message; end
35
+ def fake_logger.warn(message); self[:warn] = message; end
36
+ def fake_logger.error(message); self[:error] = message; end
37
+ def fake_logger.fatal(message); self[:fatal] = message; end
38
38
  fake_logger
39
39
  end
40
40
  subject {
41
- Eventhub::Components::Logger.new(logger, 'app_name' => 'an app', 'env' => 'test')
41
+ EventHub::Components::StructuredDataLogger.new(logger, 'app_name' => 'an app', 'env' => 'test')
42
42
  }
43
43
 
44
44
  it 'forwards calls to the target when not related to log methods' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventhub-components
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Novartis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-07 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.1.0
55
- description: Eventhub Components utilities
55
+ description: EventHub Components utilities
56
56
  email:
57
57
  - pascal.betz@simplificator.com
58
58
  executables: []
@@ -66,10 +66,16 @@ files:
66
66
  - Rakefile
67
67
  - eventhub-components.gemspec
68
68
  - lib/eventhub/components.rb
69
- - lib/eventhub/components/logger.rb
69
+ - lib/eventhub/components/exception_writer.rb
70
+ - lib/eventhub/components/log_formatter.rb
71
+ - lib/eventhub/components/multi_logger.rb
72
+ - lib/eventhub/components/pid_file.rb
73
+ - lib/eventhub/components/structured_data_logger.rb
70
74
  - lib/eventhub/components/version.rb
71
- - lib/eventhub/version.rb
72
- - spec/components/logger_spec.rb
75
+ - spec/components/exception_writer_spec.rb
76
+ - spec/components/multi_logger_spec.rb
77
+ - spec/components/pid_file_spec.rb
78
+ - spec/components/structured_data_logger_spec.rb
73
79
  - spec/spec_helper.rb
74
80
  homepage: ''
75
81
  licenses:
@@ -94,7 +100,11 @@ rubyforge_project:
94
100
  rubygems_version: 2.2.2
95
101
  signing_key:
96
102
  specification_version: 4
97
- summary: Eventhub Components utilities
103
+ summary: EventHub Components utilities
98
104
  test_files:
99
- - spec/components/logger_spec.rb
105
+ - spec/components/exception_writer_spec.rb
106
+ - spec/components/multi_logger_spec.rb
107
+ - spec/components/pid_file_spec.rb
108
+ - spec/components/structured_data_logger_spec.rb
100
109
  - spec/spec_helper.rb
110
+ has_rdoc:
@@ -1,5 +0,0 @@
1
- module Eventhub
2
- module Components
3
- VERSION = "0.0.1"
4
- end
5
- end