radar-app 0.0.12 → 0.0.13

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: bfcce7a0a67d8975a90faf73b59dc307000b0e55
4
- data.tar.gz: 01328620e14ea086a6bab6d3d13b62de656abefe
3
+ metadata.gz: 147df08f16b84c45254f8accad18b34b9b7d51f7
4
+ data.tar.gz: 6a6a25aa283d0fdd57013266091e494fedc599c4
5
5
  SHA512:
6
- metadata.gz: cb6d33d4d82a1ca525e3b1e49c6a2e6212e5fbebb0dc3e1f6605967a268ba1adef437bfa61dac3e8d0a7debdf2f7b49ae56562aba540a470fd372b11107adb07
7
- data.tar.gz: 3c0557d4fe258a9ad20c3aeece9ce8fef7468fd35ffcb13c0b0bc82379ea3470400e19e7a6e5ca823b4eb50578b4f5aeade1c1e3c81503ae14bb8f4999743ce1
6
+ metadata.gz: 33958d283b8d2a5fa3f6a2d1618920f0425c6b91d8c72ed84dc5dc77e93a2c4c470ed2efe046a89f1978fefb02cc88b137b125f350644429b4b47d774b9a890b
7
+ data.tar.gz: 35233077784098839247952548484f0ee9dc33495018a49fba45c74360d0d63595dd5ab02643a39dc9c8db1cdb0df6a625122404754f6ea7bfe9ce89bbfe880f
data/bin/radar-app CHANGED
@@ -42,9 +42,11 @@ class RadarApp < Thor
42
42
  desc 'server', 'Starts the app server'
43
43
  method_option :aliases => 's'
44
44
  def server
45
+ require 'radar-app'
45
46
  require 'active_support/dependencies'
47
+ require 'active_support/descendants_tracker'
46
48
  ActiveSupport::Dependencies.autoload_paths << './analyzers'
47
- require './analyzers_registry'
49
+ require './config/app.rb'
48
50
  Radar::App::Server.start
49
51
  end
50
52
 
data/lib/radar/app.rb CHANGED
@@ -1,14 +1,30 @@
1
1
  require 'radar/app/version'
2
2
  require 'radar/app/core_ext'
3
+ require 'radar/app/logger'
4
+ require 'radar/app/session'
3
5
  require 'radar/app/controller'
4
6
  require 'radar/app/analyzer_controller'
5
7
  require 'radar/app/server'
6
8
 
7
- require 'connection_pool'
8
9
  require 'radar-api'
10
+ require 'connection_pool'
11
+ require 'thrift_client'
12
+ require 'active_support/string_inquirer'
9
13
 
10
14
  module Radar
11
15
  module App
16
+ def self.logger=(logger)
17
+ @logger = logger
18
+ end
19
+
20
+ def self.logger
21
+ @logger ||= ::Logger.new(STDERR)
22
+ end
23
+
24
+ def self.env
25
+ @env ||= ActiveSupport::StringInquirer.new(ENV['RADAR_ENV'] || 'development')
26
+ end
27
+
12
28
  def self.security_service
13
29
  @security_service ||= connection_pool(Radar::API::SecurityService::Client, 9790)
14
30
  end
@@ -28,11 +44,12 @@ module Radar
28
44
  end
29
45
 
30
46
  def self.connection(client_class, port)
31
- host = ENV['DATA_SERVER_HOST'] || '127.0.0.1'
32
- transport = Thrift::BufferedTransport.new(Thrift::Socket.new(host, port))
33
- protocol = Thrift::BinaryProtocol.new(transport)
34
- transport.open
35
- client_class.new(protocol)
47
+ ThriftClient.new(client_class, "#{host}:#{port}",
48
+ protocol: Thrift::BinaryProtocolAccelerated, retries: 1, server_retry_period: 0)
49
+ end
50
+
51
+ def self.host
52
+ ENV['DATA_SERVER_HOST'] || '127.0.0.1'
36
53
  end
37
54
  end
38
55
  end
@@ -2,68 +2,49 @@ module Radar
2
2
  module App
3
3
  class AnalyzerController
4
4
  include Radar::App::Controller
5
- attr_reader :sessions
6
5
 
7
- forward :on_each_month, :on_each_day, :on_finish, :on_cash_flow
6
+ forward :on_each_month, :on_each_day, :on_finish,
7
+ :on_cash_flow, :dump, :resume, :result
8
8
 
9
9
  def initialize
10
10
  @sessions = {}
11
11
  end
12
12
 
13
13
  def analyzers
14
- handle_error do
15
- @@analyzers.values.map { |analyzer_class| analyzer_class.new.config }
16
- end
14
+ registry.values.map { |analyzer_class| Object.const_get(analyzer_class).new.config }
17
15
  end
18
16
 
19
- def self.register_analyzer(analyzer_id, analyzer_class)
20
- (@@analyzers ||= {})[analyzer_id] = analyzer_class
17
+ def self.<<(analyzer_class)
18
+ registry[analyzer_class.new.config.id] = analyzer_class.name
21
19
  end
22
20
 
23
21
  def create_session(session_id, analyzer_id)
24
- handle_error do
25
- analyzer = create_fresh_analyzer(@@analyzers[analyzer_id].name)
26
- @sessions[session_id] = analyzer
27
- analyzer.config
28
- end
22
+ analyzer = create_fresh_analyzer(analyzer_id)
23
+ @sessions[session_id] = Session.new(analyzer)
24
+ analyzer.config
29
25
  end
30
26
 
31
- def dump(session_id)
32
- handle_error do
33
- @sessions[session_id].dump
34
- end
35
- end
36
-
37
- def resume(session_id, data)
38
- handle_error do
39
- @sessions[session_id].resume(data)
27
+ def example_result(session_id)
28
+ synchronized(session_id) do
29
+ @sessions[session_id].analyzer.example_result
40
30
  end
41
31
  end
42
32
 
43
- def result(session_id)
44
- handle_error do
45
- @sessions[session_id].result
46
- end
47
- end
33
+ protected
48
34
 
49
- def destroy_session(session_id)
50
- handle_error do
51
- @sessions.delete(session_id)
35
+ def create_fresh_analyzer(analyzer_id)
36
+ if Radar::App.env.development?
37
+ $class_reloader.execute_if_updated
52
38
  end
39
+ Object.const_get(registry[analyzer_id]).new
53
40
  end
54
41
 
55
- def example_result(session_id)
56
- handle_error do
57
- @sessions[session_id].example_result
58
- end
42
+ def registry
43
+ self.class.registry
59
44
  end
60
45
 
61
- protected
62
-
63
- def create_fresh_analyzer(class_name)
64
- ActiveSupport::DescendantsTracker.clear
65
- ActiveSupport::Dependencies.clear
66
- Object.const_get(class_name).new
46
+ def self.registry
47
+ (@@registry ||= {})
67
48
  end
68
49
  end
69
50
  end
@@ -5,6 +5,14 @@ module Radar
5
5
  module Controller
6
6
 
7
7
  extend ActiveSupport::Concern
8
+ include Radar::App::Logger
9
+ attr_reader :sessions
10
+
11
+ def destroy_session(session_id)
12
+ synchronized(session_id) do
13
+ @sessions.delete(session_id)
14
+ end
15
+ end
8
16
 
9
17
  module ClassMethods
10
18
 
@@ -13,8 +21,9 @@ module Radar
13
21
  def forward(*methods)
14
22
  methods.each do |method|
15
23
  define_method(method) do |session_id, *args|
16
- handle_error do
17
- @sessions[session_id].send(method, *args)
24
+ synchronized(session_id) do
25
+ logger.debug { "[#{session_id}] #{method}(...)" }
26
+ @sessions[session_id].analyzer.send(method, *args)
18
27
  end
19
28
  end
20
29
  end
@@ -23,15 +32,10 @@ module Radar
23
32
 
24
33
  protected
25
34
 
26
- def handle_error
27
- begin
28
- yield
29
- rescue => e
30
- $stderr.puts "#{e.class}: #{e.message}"
31
- $stderr.puts e.backtrace
32
- $stderr.puts
33
- end
35
+ def synchronized(session_id)
36
+ @sessions[session_id].mutex.synchronize { yield }
34
37
  end
38
+
35
39
  end
36
40
  end
37
41
  end
@@ -0,0 +1,9 @@
1
+ module Radar
2
+ module App
3
+ module Logger
4
+ def logger
5
+ Radar::App.logger
6
+ end
7
+ end
8
+ end
9
+ end
@@ -8,10 +8,9 @@ module Radar
8
8
  handler = Radar::App::AnalyzerController.new
9
9
  processor = Radar::API::AnalyzerController::Processor.new(handler)
10
10
  transport = Thrift::ServerSocket.new(port)
11
- transportFactory = Thrift::BufferedTransportFactory.new
12
- server = Thrift::ThreadedServer.new(processor, transport)
13
- $stderr.puts "Starting server on port #{port}..."
14
- server.serve()
11
+ server = Thrift::NonblockingServer.new(processor, transport, Thrift::FramedTransportFactory.new)
12
+ $stderr.puts "Starting app on port #{port}..."
13
+ server.serve
15
14
  end
16
15
  end
17
16
  end
@@ -0,0 +1,9 @@
1
+ module Radar
2
+ module App
3
+ class Session < Struct.new(:analyzer)
4
+ def mutex
5
+ @mutex ||= Mutex.new
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Radar
2
2
  module App
3
- VERSION = "0.0.12"
3
+ VERSION = "0.0.13"
4
4
  end
5
5
  end
data/radar-app.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency "thin", "1.6.2"
27
27
  spec.add_dependency "connection_pool", "~> 2.0"
28
28
  spec.add_dependency "activesupport", "~> 4.1"
29
+ spec.add_dependency "thrift_client", "~> 0.9"
29
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radar-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonardo Mendonca
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-02 00:00:00.000000000 Z
11
+ date: 2014-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '4.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thrift_client
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.9'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.9'
111
125
  description: generates and run a radar-app.
112
126
  email:
113
127
  - leonardo.mendonca@investtools.com.br
@@ -129,7 +143,9 @@ files:
129
143
  - lib/radar/app/controller.rb
130
144
  - lib/radar/app/core_ext.rb
131
145
  - lib/radar/app/core_ext/date.rb
146
+ - lib/radar/app/logger.rb
132
147
  - lib/radar/app/server.rb
148
+ - lib/radar/app/session.rb
133
149
  - lib/radar/app/version.rb
134
150
  - radar-app.gemspec
135
151
  - templates/Gemfile