vx-lib-logger 0.3.3 → 0.3.4
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/lib/vx/lib/logger/logstash_device.rb +33 -10
- data/lib/vx/lib/logger/logstash_logger.rb +4 -4
- data/lib/vx/lib/logger/version.rb +1 -1
- data/spec/lib/logstash_device_spec.rb +39 -1
- data/spec/lib/logstash_logger_spec.rb +1 -0
- data/spec/spec_helper.rb +15 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2dea1390dc5c902ae600c1bcb84384ad3267051c
|
4
|
+
data.tar.gz: 4409bc16d3e05078667782762bcd4a74dd6585bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 598e0782b467101c053444c70b1d80313237d92a8a51372b1149e0c8d8fe82dfe214ab5e7484400cca31362ac5951aae43aea90acbf83418eb4eac115aba9559
|
7
|
+
data.tar.gz: bbd5a90922d82352bc070b205b12a0f587a2a829433e776d3105a958f4767840ad2c7e393c92af36cbc1685354c9f28f706bdf3b3add0af1d514129768b76fb1
|
@@ -8,6 +8,7 @@ module Vx ; module Lib ; module Logger
|
|
8
8
|
|
9
9
|
def initialize
|
10
10
|
@mutex = Mutex.new
|
11
|
+
@queue = Queue.new
|
11
12
|
end
|
12
13
|
|
13
14
|
def uri
|
@@ -35,16 +36,34 @@ module Vx ; module Lib ; module Logger
|
|
35
36
|
|
36
37
|
def write(message)
|
37
38
|
if enabled?
|
38
|
-
|
39
|
+
main_thread
|
40
|
+
@queue.push message
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def flush
|
45
|
+
@io && @io.flush
|
46
|
+
end
|
47
|
+
|
48
|
+
def main_thread
|
49
|
+
@main_thread ||= Thread.new do
|
50
|
+
loop do
|
39
51
|
with_connection do
|
40
|
-
@io.write
|
52
|
+
@io.write @queue.pop
|
41
53
|
end
|
42
54
|
end
|
43
55
|
end
|
44
56
|
end
|
45
57
|
|
46
|
-
def
|
47
|
-
@
|
58
|
+
def close_main_thread
|
59
|
+
@main_thread && (
|
60
|
+
@main_thread.kill
|
61
|
+
@main_thread = nil
|
62
|
+
)
|
63
|
+
end
|
64
|
+
|
65
|
+
def empty?
|
66
|
+
@queue.empty?
|
48
67
|
end
|
49
68
|
|
50
69
|
private
|
@@ -62,12 +81,16 @@ module Vx ; module Lib ; module Logger
|
|
62
81
|
end
|
63
82
|
|
64
83
|
def with_connection(&block)
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
84
|
+
begin
|
85
|
+
if enabled?
|
86
|
+
connect unless connected?
|
87
|
+
yield
|
88
|
+
end
|
89
|
+
rescue Exception => e
|
90
|
+
warn "#{self.class} - #{e.class} - #{e.message}"
|
91
|
+
close
|
92
|
+
@io = nil
|
93
|
+
end
|
71
94
|
end
|
72
95
|
|
73
96
|
def reconnect
|
@@ -20,11 +20,11 @@ module Vx ; module Lib ; module Logger
|
|
20
20
|
logstash_device.close
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
def logstash_device
|
24
|
+
Lib::Logger.logstash_device
|
25
|
+
end
|
24
26
|
|
25
|
-
|
26
|
-
Lib::Logger.logstash_device
|
27
|
-
end
|
27
|
+
private
|
28
28
|
|
29
29
|
def format_message(level, message, payload)
|
30
30
|
LogstashFormatter.call(level, progname, message, payload)
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'timeout'
|
2
3
|
|
3
4
|
describe Vx::Lib::Logger::LogstashDevice do
|
4
5
|
|
@@ -19,6 +20,25 @@ describe Vx::Lib::Logger::LogstashDevice do
|
|
19
20
|
assert_equal re, "Hello\n"
|
20
21
|
end
|
21
22
|
|
23
|
+
it "should successfuly write in multhreaded" do
|
24
|
+
re = with_socket do
|
25
|
+
log = Vx::Lib::Logger::LogstashDevice.new
|
26
|
+
ths = (0...3).to_a.map do |n|
|
27
|
+
Thread.new do
|
28
|
+
log.write("Hello\n")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
ths.map(&:join)
|
32
|
+
Timeout.timeout(3) do
|
33
|
+
while !log.empty?
|
34
|
+
sleep 0.1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
log.close
|
38
|
+
end
|
39
|
+
assert_equal "Hello\n" * 10, re
|
40
|
+
end
|
41
|
+
|
22
42
|
it "should successfuly lost connection" do
|
23
43
|
log = Vx::Lib::Logger::LogstashDevice.new
|
24
44
|
|
@@ -27,15 +47,33 @@ describe Vx::Lib::Logger::LogstashDevice do
|
|
27
47
|
log.close
|
28
48
|
end
|
29
49
|
|
50
|
+
Timeout.timeout(3) do
|
51
|
+
while !log.empty?
|
52
|
+
sleep 0.1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
30
56
|
log.write("Lost\n")
|
31
57
|
|
58
|
+
Timeout.timeout(3) do
|
59
|
+
while !log.empty?
|
60
|
+
sleep 0.1
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
32
64
|
re << with_socket do
|
33
65
|
log.write("World\n")
|
34
66
|
end
|
35
67
|
|
68
|
+
Timeout.timeout(3) do
|
69
|
+
while !log.empty?
|
70
|
+
sleep 0.1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
36
74
|
log.close
|
37
75
|
|
38
|
-
assert_equal
|
76
|
+
assert_equal "Hello\nWorld\n", re
|
39
77
|
end
|
40
78
|
|
41
79
|
|
data/spec/spec_helper.rb
CHANGED
@@ -2,25 +2,32 @@ require File.expand_path("../../lib/vx/lib/logger", __FILE__)
|
|
2
2
|
|
3
3
|
require 'minitest/spec'
|
4
4
|
require 'minitest/autorun'
|
5
|
+
require 'thread'
|
5
6
|
|
6
7
|
def with_socket
|
7
|
-
out
|
8
|
+
out = ""
|
8
9
|
server = TCPServer.new 9999
|
10
|
+
lock = Mutex.new
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
ths = (0..10).to_a.map do |n|
|
13
|
+
Thread.new do
|
14
|
+
loop do
|
15
|
+
client = server.accept
|
16
|
+
lock.synchronize do
|
17
|
+
out << client.gets
|
18
|
+
puts "#{n}: !!!#{out}!!!"
|
19
|
+
client.close
|
20
|
+
end
|
21
|
+
end
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
18
25
|
begin
|
19
26
|
yield
|
20
|
-
sleep
|
27
|
+
sleep 10
|
21
28
|
out
|
22
29
|
ensure
|
23
|
-
|
30
|
+
ths.map(&:kill)
|
24
31
|
server.close
|
25
32
|
end
|
26
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vx-lib-logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Galinsky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|