dmexe-thrifty 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02bb1afbf7a8fd33c10b8a8f0fa7a3b48ddf884a
4
+ data.tar.gz: d3280242048e29b3648fac63751251dfde9f0181
5
+ SHA512:
6
+ metadata.gz: 156e21274643c268bc42f4d226f466e7635a5f0b3aad3fd0735cd222f81e28b95f8370b0633ff2418919b6ba6f6016383d73ba788aae558d8e15cf8d23b455a7
7
+ data.tar.gz: 0fd456ab3cf539ae3ccdcffff4bc7b756144c183181d099b74c43d028da1d5b05c6b238d1f3046233dc5f625ca0c6c327062e9845b281b75867dd232ed1d3b95
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at dima.exe@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thrifty-logger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dmitry Galinsky
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Thrifty
2
+
3
+ Under heavy development.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ t.verbose = true
7
+ t.libs << "test"
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "thrifty/logger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thrifty/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dmexe-thrifty"
8
+ spec.version = Thrifty::VERSION
9
+ spec.authors = ["Dmitry Galinsky"]
10
+ spec.email = ["dima.exe@gmail.com"]
11
+
12
+ spec.summary = %q{Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "minitest"
33
+ spec.add_development_dependency "minitest-reporters"
34
+ spec.add_development_dependency "puma"
35
+ spec.add_development_dependency "rack"
36
+ spec.add_development_dependency "hutch"
37
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path("../thrifty", __FILE__)
@@ -0,0 +1,34 @@
1
+ module Thrifty::HTTP::Server
2
+ class Builder
3
+
4
+ def initialize
5
+ require 'rack'
6
+ require 'puma'
7
+
8
+ @port = nil
9
+ @ip = nil
10
+ @name = nil
11
+ end
12
+
13
+ def with_port(value)
14
+ @port = value.to_i
15
+ self
16
+ end
17
+
18
+ def with_ip(value)
19
+ @ip = value
20
+ self
21
+ end
22
+
23
+ def with_name(value)
24
+ @name = value
25
+ self
26
+ end
27
+
28
+ def build(&block)
29
+ server = PumaServer.new(ip: @ip, port: @port, &block)
30
+ server.start
31
+ server
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,45 @@
1
+ module Thrifty::HTTP::Server
2
+ class ErrMiddleware
3
+
4
+ def initialize(app, name)
5
+ @log = Thrifty.get_logger(name)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ @app.call(env)
11
+ rescue Exception => e
12
+ @log.error(e, clean_env(env))
13
+ body = dump_exception(e)
14
+ [
15
+ 500,
16
+ {
17
+ Rack::CONTENT_TYPE => 'text/plain',
18
+ Rack::CONTENT_LENGTH => Rack::Utils.bytesize(body).to_s,
19
+ },
20
+ [body],
21
+ ]
22
+ end
23
+
24
+ private
25
+
26
+ def clean_env(env)
27
+ env.inject({}) do |ac, pair|
28
+ case
29
+ when pair[0] =~ /\A(rack|puma)\./
30
+ ac
31
+ else
32
+ ac.merge! pair[0] => pair[1]
33
+ end
34
+ end
35
+ end
36
+
37
+ def dump_exception(exception)
38
+ string = "#{exception.class}: #{exception.message}\n"
39
+ string << exception.backtrace.map { |l| "\t#{l}" }.join("\n")
40
+ string
41
+ end
42
+
43
+ end
44
+ end
45
+
@@ -0,0 +1,44 @@
1
+ module Thrifty::HTTP::Server
2
+
3
+ class LogMiddleware
4
+ FORMAT = %{%s "%s %s%s %s" %d %s %0.4f}
5
+
6
+ def initialize(app, name)
7
+ @log = Thrifty.get_logger name
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ began_at = Time.now
13
+ status, header, body = @app.call(env)
14
+ header = Rack::Utils::HeaderHash.new(header)
15
+ body = Rack::BodyProxy.new(body) { log(env, status, header, began_at) }
16
+ [status, header, body]
17
+ end
18
+
19
+ private
20
+
21
+ def log(env, status, header, began_at)
22
+ now = Time.now
23
+ length = extract_content_length(header)
24
+ peer = env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"]
25
+
26
+ payload = {
27
+ peer: peer,
28
+ status: status.to_s[0..3],
29
+ method: env[Rack::REQUEST_METHOD],
30
+ path: env[Rack::PATH_INFO],
31
+ query: env[Rack::QUERY_STRING],
32
+ len: length,
33
+ duration: now - began_at
34
+ }
35
+
36
+ @log.info nil, payload
37
+ end
38
+
39
+ def extract_content_length(headers)
40
+ headers[Rack::CONTENT_LENGTH] || 0
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,67 @@
1
+ require 'thread'
2
+
3
+ module Thrifty::HTTP::Server
4
+ class PumaServer
5
+
6
+ attr_reader :log
7
+
8
+ DEFAULT_PORT = ENV['PORT'].to_i > 0 ? ENV['PORT'] : 8080
9
+ DEFAULT_IP = '0.0.0.0'
10
+ DEFAULT_MIN_TH = 20
11
+ DEFAULT_MAX_TH = 20
12
+
13
+ def initialize(options={})
14
+ port = options[:port] || DEFAULT_PORT
15
+ ip = options[:ip] || DEFAULT_IP
16
+ min = options[:min] || DEFAULT_MIN_TH
17
+ max = options[:max] || DEFAULT_MAX_TH
18
+ name = options[:name] || 'Puma'
19
+
20
+ @log = Thrifty.get_logger(name)
21
+ @bind = "#{ip}:#{port}"
22
+ @lock = Mutex.new
23
+
24
+ app = ::Rack::Builder.new do
25
+ if options[:err] != false
26
+ use ErrMiddleware, name
27
+ end
28
+
29
+ if options[:log] != false
30
+ use LogMiddleware, name
31
+ end
32
+
33
+ use Rack::Lint
34
+ yield self
35
+ end
36
+
37
+ @server = Puma::Server.new(app)
38
+ @server.add_tcp_listener ip, port
39
+ @server.min_threads = min
40
+ @server.max_threads = max
41
+
42
+ Thrifty::Signals.register(method(:stop))
43
+ end
44
+
45
+ def start
46
+ @lock.synchronize do
47
+ unless @thread
48
+ log.info "starting", version: Puma::Server::VERSION, bind: @bind, threads: "#{@server.min_threads}:#{@server.max_threads}"
49
+ @thread = @server.run
50
+ end
51
+ end
52
+ end
53
+
54
+ def stop
55
+ @lock.synchronize do
56
+ if @thread
57
+ log.info "stopping"
58
+ @server.stop(true)
59
+ @thread.join
60
+ @thread = nil
61
+ log.info "stopped"
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,11 @@
1
+ module Thrifty ; module HTTP ; module Server
2
+ end ; end ; end
3
+
4
+ %w{
5
+ log_middleware
6
+ err_middleware
7
+ puma_server
8
+ builder
9
+ }.each do |f|
10
+ require File.expand_path("../http_server/#{f}", __FILE__)
11
+ end
@@ -0,0 +1,50 @@
1
+ require 'thread'
2
+
3
+ module Thrifty::Hutch
4
+ class Builder
5
+ def initialize
6
+ require 'hutch'
7
+
8
+ @url = ENV['RABBITMQ_URL'] || 'amqp://guest:guest@localhost:5672/'
9
+
10
+ @lock = Mutex.new
11
+
12
+ Thread.main[:signal_queue] = []
13
+
14
+ Thrifty::Signals.register(method(:stop))
15
+ end
16
+
17
+ def with_url(url)
18
+ @url = url
19
+ self
20
+ end
21
+
22
+ def build
23
+ ::Hutch::Logging.logger = Thrifty.get_logger "Hutch"
24
+ ::Hutch::Config.set(:uri, @url)
25
+ ::Hutch::Config.set(:enable_http_api_use, false)
26
+ ::Hutch::Config.set(:error_handlers, [ErrorHandler.new])
27
+ start
28
+ end
29
+
30
+ def stop
31
+ @lock.synchronize do
32
+ if @worker
33
+ @worker.stop
34
+ ::Hutch.disconnect
35
+ @worker = nil
36
+ end
37
+ end
38
+ end
39
+
40
+ def start
41
+ @lock.synchronize do
42
+ unless @worker
43
+ ::Hutch.connect
44
+ @worker = ::Hutch::Worker.new(::Hutch.broker, ::Hutch.consumers)
45
+ @worker.setup_queues
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module Thrifty::Hutch
2
+ class ErrorHandler
3
+ def handle(message_id, payload, consumer, ex)
4
+ Thrifty::Logger::App.handle_exception(
5
+ ex,
6
+ "Hutch",
7
+ payload: payload,
8
+ consumer: consumer,
9
+ message_id: message_id
10
+ )
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Thrifty ; module Hutch
2
+ end ; end
3
+
4
+ %w{
5
+ error_handler
6
+ builder
7
+ }.each do |f|
8
+ require File.expand_path("../hutch/#{f}", __FILE__)
9
+ end
@@ -0,0 +1,89 @@
1
+ require 'thread'
2
+ require 'singleton'
3
+
4
+ module Thrifty::Logger
5
+ class App
6
+
7
+ include Singleton
8
+
9
+ def initialize
10
+ @queue = Queue.new
11
+ @lock = Mutex.new
12
+
13
+ Thrifty::Signals.register(method(:stop))
14
+ end
15
+
16
+ def start
17
+ @lock.synchronize do
18
+ unless @thread
19
+ @thread = main_loop
20
+ end
21
+ end
22
+ end
23
+
24
+ def started?
25
+ !!@thread
26
+ end
27
+
28
+ def stop
29
+ @lock.synchronize do
30
+ return unless @thread
31
+ @queue.push :shutdown
32
+ @thread.join
33
+ @thread = nil
34
+ end
35
+ end
36
+
37
+ def append(entry)
38
+ @queue.push(entry) if @thread
39
+ end
40
+
41
+ private
42
+
43
+ def main_loop ; Thread.new do
44
+ begin
45
+ loop do
46
+ entry = @queue.pop
47
+ if entry == :shutdown
48
+ break
49
+ end
50
+ if entry == :boom
51
+ raise RuntimeError.new("ignore me")
52
+ end
53
+ App.append(entry)
54
+ end
55
+ rescue Exception => ex
56
+ App.handle_exception(ex, self.class)
57
+ retry
58
+ end
59
+ end ; end
60
+ end
61
+
62
+ class App
63
+ class << self
64
+ @@appenders = [IoAppender.new]
65
+ @@exception_handlers = [StderrExceptionHanlder.new]
66
+
67
+ def appenders(*fn)
68
+ @@appenders = fn.flatten
69
+ end
70
+
71
+ def exception_handlers(*fn)
72
+ @@exception_handlers = fn.flatten
73
+ end
74
+
75
+ def append(entry)
76
+ @@appenders.each{|fn| fn.call(entry) }
77
+ end
78
+
79
+ def handle_exception(ex, scope = nil, context = {})
80
+ @@exception_handlers.each{|fn| fn.call(ex, scope, context) }
81
+ end
82
+
83
+ def reset!
84
+ @@appenders = [IoAppender.new]
85
+ @@exception_handlers = [StderrExceptionHanlder.new]
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,13 @@
1
+ module Thrifty::Logger
2
+ class IoAppender
3
+ def initialize(io = nil, formatter = nil)
4
+ @io = io || STDOUT
5
+ is_tty = @io.respond_to?(:tty?) && @io.tty?
6
+ @formatter = formatter || (is_tty ? SimpleFormatter.new : LogfmtFormatter.new)
7
+ end
8
+
9
+ def call(entry)
10
+ @formatter.call(entry).each{|line| @io.puts line }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,63 @@
1
+ module Thrifty::Logger
2
+ class Context
3
+
4
+ attr_reader :level
5
+
6
+ def initialize(app, scope, context = nil)
7
+ @app = app
8
+ @scope = scope
9
+ @level = Thrifty::Logger::DEBUG
10
+ @context = context || {}
11
+ end
12
+
13
+ def level=(level)
14
+ @level = level.to_i
15
+ end
16
+
17
+ def []=(k,v)
18
+ if v == nil
19
+ @context.delete(k)
20
+ else
21
+ @context[k] = v
22
+ end
23
+ end
24
+
25
+ def measure(message, payload = {})
26
+ tm = Time.now.to_f
27
+ re = nil
28
+ re = yield if block_given?
29
+ tm = Time.now.to_f - tm
30
+ payload[:duration] = tm
31
+ self.info message, payload
32
+ re
33
+ end
34
+
35
+ Thrifty::Logger::LEVELS.each_with_index do |lv, idx|
36
+ level_id = lv.downcase.to_sym
37
+ define_method level_id do |message = nil, payload = nil, &block|
38
+ return if idx < level
39
+ message = block ? block.call : message
40
+ payload = @context.merge(payload || {})
41
+ entry =
42
+ if message.is_a?(Exception)
43
+ ExceptionEntry.new(
44
+ level_id,
45
+ Time.now,
46
+ @scope,
47
+ message,
48
+ payload
49
+ )
50
+ else
51
+ Entry.new(
52
+ level_id,
53
+ Time.now,
54
+ @scope,
55
+ message,
56
+ payload
57
+ )
58
+ end
59
+ @app.append(entry)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,24 @@
1
+ module Thrifty::Logger
2
+ Entry = Struct.new(:level, :time, :scope, :message, :payload) do
3
+ def to_h
4
+ {
5
+ level: level || :info,
6
+ time: time || Time.now,
7
+ message: message,
8
+ scope: scope,
9
+ }.merge!(payload || {})
10
+ end
11
+ end
12
+
13
+ ExceptionEntry = Struct.new(:level, :time, :scope, :exception, :payload) do
14
+ def to_h
15
+ {
16
+ level: level || :error,
17
+ time: time || Time.now,
18
+ message: exception.message,
19
+ scope: scope,
20
+ exception: exception.class.to_s,
21
+ }.merge!(payload || {})
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ module Thrifty::Logger
2
+
3
+ class StderrExceptionHanlder
4
+ def initialize(appender = nil)
5
+ @appender = appender || IoAppender.new(STDERR)
6
+ end
7
+
8
+ def call(ex, scope, context)
9
+ entry = ExceptionEntry.new(nil, nil, scope, ex, context)
10
+ @appender.call(entry)
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,71 @@
1
+ module Thrifty::Logger
2
+ class LogfmtFormatter
3
+
4
+ UNESCAPED_STRING = /\A[\w\.\-\+\%\,\:\;\/]*\z/i
5
+
6
+ IGNORED_FIELDS = [:time]
7
+
8
+ def call(entry)
9
+ case entry
10
+ when ExceptionEntry
11
+ format_exception(entry)
12
+ when Entry
13
+ format_entry(entry)
14
+ else
15
+ [entry_or_ex.to_s]
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def format_entry(entry)
22
+ [format_hash(entry.to_h)]
23
+ end
24
+
25
+ def format_exception(entry)
26
+ values = []
27
+ values << format_hash(entry.to_h)
28
+ if entry.exception && entry.exception.backtrace.is_a?(Array)
29
+ values <<
30
+ "#{entry.exception.class}: #{entry.exception.message}\n" +
31
+ entry.exception.backtrace.map{|l| "\t#{l}" }.join("\n")
32
+ end
33
+ values
34
+ end
35
+
36
+ def format_hash(attrs)
37
+ attrs.inject([]) do |ac, (k,v)|
38
+ if !IGNORED_FIELDS.include?(k) && !(v == nil || v == "")
39
+ new_value = sanitize(v)
40
+ ac << "#{k}=#{new_value}"
41
+ end
42
+ ac
43
+ end.join(" ")
44
+ end
45
+
46
+ def sanitize(v)
47
+ case v
48
+ when ::Array
49
+ may_quote v.join(",")
50
+ when ::Integer, ::Symbol
51
+ v.to_s
52
+ when ::Float
53
+ "%0.4f" % v
54
+ when ::TrueClass, ::FalseClass
55
+ v ? "t" : "f"
56
+ when Time
57
+ quote v.utc.to_s
58
+ else
59
+ may_quote(v.to_s)
60
+ end
61
+ end
62
+
63
+ def quote(s)
64
+ s.inspect
65
+ end
66
+
67
+ def may_quote(s)
68
+ s =~ UNESCAPED_STRING ? s : quote(s)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,21 @@
1
+ module Thrifty::Logger
2
+ class SimpleFormatter < LogfmtFormatter
3
+
4
+ private
5
+
6
+ def format_hash(attrs)
7
+ level = attrs.delete(:level)
8
+ message = attrs.delete(:message)
9
+ scope = attrs.delete(:scope)
10
+
11
+ IGNORED_FIELDS.each do |f|
12
+ attrs.delete(f)
13
+ end
14
+
15
+ payload = super(attrs)
16
+ payload = payload.empty? ? "" : " [#{payload}]"
17
+ message = message.to_s.empty? ? "" : " #{message}"
18
+ "[%5s]: #{scope} -#{message}#{payload}" % level.upcase
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,43 @@
1
+ module Thrifty ; module Logger
2
+ TRACE = 0
3
+ DEBUG = 1
4
+ INFO = 2
5
+ WARN = 3
6
+ ERROR = 4
7
+ FATAL = 5
8
+
9
+ LEVELS = ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'].freeze
10
+
11
+ def self.get_logger(scope, context = nil)
12
+ App.instance.start unless App.instance.started?
13
+ Context.new(App.instance, scope.to_s, context)
14
+ end
15
+
16
+ def self.stop
17
+ App.instance.stop
18
+ end
19
+
20
+ def logfmt_formatter
21
+ LogfmtFormatter.new
22
+ end
23
+
24
+ def simple_formatter
25
+ SimpleFormatter.new
26
+ end
27
+
28
+ def io_appender(*args)
29
+ IoAppender.new(*args)
30
+ end
31
+ end ; end
32
+
33
+ %w{
34
+ entry
35
+ formatters/logfmt_formatter
36
+ formatters/simple_formatter
37
+ appenders/io_appender
38
+ exception_handlers/stderr_exception_handler
39
+ app
40
+ context
41
+ }.each do |f|
42
+ require File.expand_path("../logger/#{f}", __FILE__)
43
+ end
@@ -0,0 +1,44 @@
1
+ require 'singleton'
2
+ require 'thread'
3
+
4
+ module Thrifty ; class Signals
5
+
6
+ include Singleton
7
+
8
+ class << self
9
+ def register(fn)
10
+ instance.register(fn)
11
+ end
12
+ end
13
+
14
+ def initialize
15
+ @handlers = []
16
+ @mutex = Mutex.new
17
+ @resource = ConditionVariable.new
18
+ end
19
+
20
+ def install
21
+ %w{INT TERM}.each do |sig|
22
+ trap sig, &method(:shutdown)
23
+ end
24
+ end
25
+
26
+ def wait
27
+ @mutex.synchronize do
28
+ @resource.wait(@mutex)
29
+ end
30
+ end
31
+
32
+ def register(fn)
33
+ @handlers << fn
34
+ end
35
+
36
+ def shutdown
37
+ @handlers.each {|fn| fn.call }
38
+
39
+ @mutex.synchronize do
40
+ @resource.signal
41
+ end
42
+ end
43
+
44
+ end ; end
@@ -0,0 +1,3 @@
1
+ module Thrifty
2
+ VERSION = '0.1.0'
3
+ end
data/lib/thrifty.rb ADDED
@@ -0,0 +1,25 @@
1
+ module Thrifty
2
+
3
+ extend self
4
+
5
+ def get_logger(*args)
6
+ Thrifty::Logger.get_logger(*args)
7
+ end
8
+
9
+ def http_server
10
+ Thrifty::HTTP::Server::Builder.new
11
+ end
12
+
13
+ def hutch
14
+ Thrifty::Hutch::Builder.new
15
+ end
16
+
17
+ def signals
18
+ Thrifty::Signals.instance
19
+ end
20
+ end
21
+
22
+ require File.expand_path("../thrifty/signals", __FILE__)
23
+ require File.expand_path("../thrifty/logger", __FILE__)
24
+ require File.expand_path("../thrifty/http_server", __FILE__)
25
+ require File.expand_path("../thrifty/hutch", __FILE__)
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dmexe-thrifty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Galinsky
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: puma
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: hutch
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Write a longer description or delete this line.
112
+ email:
113
+ - dima.exe@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - CODE_OF_CONDUCT.md
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - dmexe-thrifty.gemspec
127
+ - lib/dmexe-thrifty.rb
128
+ - lib/thrifty.rb
129
+ - lib/thrifty/http_server.rb
130
+ - lib/thrifty/http_server/builder.rb
131
+ - lib/thrifty/http_server/err_middleware.rb
132
+ - lib/thrifty/http_server/log_middleware.rb
133
+ - lib/thrifty/http_server/puma_server.rb
134
+ - lib/thrifty/hutch.rb
135
+ - lib/thrifty/hutch/builder.rb
136
+ - lib/thrifty/hutch/error_handler.rb
137
+ - lib/thrifty/logger.rb
138
+ - lib/thrifty/logger/app.rb
139
+ - lib/thrifty/logger/appenders/io_appender.rb
140
+ - lib/thrifty/logger/context.rb
141
+ - lib/thrifty/logger/entry.rb
142
+ - lib/thrifty/logger/exception_handlers/stderr_exception_handler.rb
143
+ - lib/thrifty/logger/formatters/logfmt_formatter.rb
144
+ - lib/thrifty/logger/formatters/simple_formatter.rb
145
+ - lib/thrifty/signals.rb
146
+ - lib/thrifty/version.rb
147
+ homepage: https://github.com
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.4.5.1
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Write a short summary, because Rubygems requires one.
171
+ test_files: []