rubypitaya 1.2.9 → 1.5.0

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
  SHA256:
3
- metadata.gz: 2a34f05283342d1f35366034211370ce5564c8f7c1e033c69bf6efc5aa7cfde6
4
- data.tar.gz: b1ec31931d1306144373921202b10882ea6942c77986dcaafb61800dab879ce3
3
+ metadata.gz: 8ce9fce444231d413f663d1af94356f0a04204af86050e6041a24f3b2bf37d8d
4
+ data.tar.gz: cc4c79730ea4e46891bff56f635c0135c62adc0f5ce06c6a473567bd46a48b78
5
5
  SHA512:
6
- metadata.gz: 65b562f383a9c5d8f21ba37763a5f79a9fbab1f2af016f23e5f008178ff5917894daacc4df6b35819d7db6bbf91d45c53cb020ea41ef0ce382cb6d68606f4849
7
- data.tar.gz: 83cf65e5020902691cd3149220efe2345123ae93a6c3bd057945e7b03a50642100e70f328fe13fcab8994047b9d67df0c9b94dadf593cc14952862f8a4b602e9
6
+ metadata.gz: 46fff9b02c5d40577864c21bd226d7b412b93d70af22612b3dfe39409546d2b14361f2f63d0ba0f4c07db5fe118908d4700b804c5ad134f7ceb06e8c94b5cdc2
7
+ data.tar.gz: 40b3def401682e4f98e6efaabaad9afc384128f6e0a39a3ae495d36643155bab86c1913969fd3ac35f647275836b06c24aafd818dcf7e2657618ffb1754587ee
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem 'rubypitaya', '1.2.9'
3
+ gem 'rubypitaya', '1.5.0'
4
4
 
5
5
  group :development do
6
6
  gem 'pry', '0.12.2'
@@ -62,7 +62,7 @@ GEM
62
62
  diff-lcs (>= 1.2.0, < 2.0)
63
63
  rspec-support (~> 3.8.0)
64
64
  rspec-support (3.8.3)
65
- rubypitaya (1.2.9)
65
+ rubypitaya (1.5.0)
66
66
  activerecord (= 6.0.2)
67
67
  etcdv3 (= 0.10.2)
68
68
  eventmachine (= 1.2.7)
@@ -85,7 +85,7 @@ DEPENDENCIES
85
85
  pry (= 0.12.2)
86
86
  rake (= 10.0)
87
87
  rspec (= 3.8.0)
88
- rubypitaya (= 1.2.9)
88
+ rubypitaya (= 1.5.0)
89
89
 
90
90
  BUNDLED WITH
91
91
  1.17.2
@@ -40,6 +40,7 @@ namespace :db do
40
40
  connection_data = database_config.connection_data_without_database
41
41
 
42
42
  ActiveRecord::Base.establish_connection(connection_data)
43
+ ActiveRecord::Base.connection.select_all "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname='#{database_config.database_name}' AND pid <> pg_backend_pid();"
43
44
  ActiveRecord::Base.connection.drop_database(database_config.database_name)
44
45
  ActiveRecord::Base.connection.close
45
46
 
@@ -1,11 +1,13 @@
1
- class HelloWorldHandler < RubyPitaya::HandlerBase
1
+ module MyApp
2
+ class HelloWorldHandler < RubyPitaya::HandlerBase
2
3
 
3
- non_authenticated_actions :sayHello
4
+ non_authenticated_actions :sayHello
4
5
 
5
- def sayHello
6
- response = {
7
- code: 'RP-200',
8
- msg: 'Hello!'
9
- }
6
+ def sayHello
7
+ response = {
8
+ code: 'RP-200',
9
+ msg: 'Hello!'
10
+ }
11
+ end
10
12
  end
11
- end
13
+ end
@@ -7,16 +7,24 @@ require 'rubypitaya'
7
7
  require 'rubypitaya/core/database_config'
8
8
 
9
9
  environment_name = ENV.fetch("RUBYPITAYA_ENV") { 'development' }
10
- database_config = DatabaseConfig.new(environment_name, Path::DATABASE_CONFIG_PATH)
10
+ database_config = RubyPitaya::DatabaseConfig.new(environment_name, RubyPitaya::Path::DATABASE_CONFIG_PATH)
11
11
  ActiveRecord::Base.establish_connection(database_config.connection_data)
12
12
  ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
13
13
  ActiveSupport::LogSubscriber.colorize_logging = true
14
14
 
15
15
  Gem.find_files('rubypitaya/**/*.rb').each do |path|
16
- require path unless path.end_with?('spec.rb') || path.include?('db/migrate')
16
+ require path unless path.end_with?('spec.rb') ||
17
+ path.include?('db/migrate') ||
18
+ path.include?('app-template')
19
+ end
20
+
21
+ app_files_path = File.join(RubyPitaya::Path::APP_FOLDER_PATH, '**/*.rb')
22
+ Dir[app_files_path].each do |path|
23
+ require path unless path.end_with?('spec.rb') ||
24
+ path.include?('db/migrate')
17
25
  end
18
26
 
19
27
  require 'irb'
20
28
  IRB.start(__FILE__)
21
29
 
22
- ActiveRecord::Base.connection.close
30
+ ActiveRecord::Base.connection.close
@@ -0,0 +1,11 @@
1
+ class Routes < RubyPitaya::RoutesBase
2
+
3
+ # class: RoutesBase
4
+ # methods:
5
+ # - match('new_handler_name', to: 'ClassHandler')
6
+ # - Defines a new name to handler
7
+
8
+ def setup
9
+ # match('helloHandler', to: 'MyApp::HelloWorldHandler')
10
+ end
11
+ end
@@ -63,6 +63,7 @@ services:
63
63
  NATS_URL: 'nats://nats:4222'
64
64
  ETCD_URL: 'http://etcd:2379'
65
65
  ETCD_PREFIX: 'rubypitaya/'
66
+ ETCD_LEASE_SECONDS: '60'
66
67
  REDIS_URL: 'redis://redis:6379'
67
68
  DATABASE_HOST: 'db'
68
69
  DATABASE_USER: 'postgres'
@@ -6,35 +6,71 @@ module RubyPitaya
6
6
  class EtcdConnector
7
7
 
8
8
  def initialize(server_uuid, desktop_name, server_name, etcd_prefix,
9
- etcd_address, allow_reconnect)
9
+ etcd_address, allow_reconnect, lease_seconds)
10
10
  @server_uuid = server_uuid
11
11
  @server_name = server_name
12
12
  @desktop_name = desktop_name
13
13
  @etcd_prefix = etcd_prefix
14
14
  @etcd_address = etcd_address
15
15
  @allow_reconnect = allow_reconnect
16
+ @lease_seconds = lease_seconds
17
+
18
+ @renew_connection_seconds = lease_seconds / 2.0
19
+
20
+ @renew_connection_key = nil
21
+ @renew_connection_value = nil
22
+ @renew_connection_thread = nil
16
23
  end
17
24
 
18
25
  def connect
26
+ connection_key = get_connection_key
27
+ connection_value = get_connection_value
28
+
19
29
  @connection = Etcdv3.new(endpoints: @etcd_address,
20
30
  allow_reconnect: @allow_reconnect)
21
- @connection.put(connection_key, connection_value)
31
+
32
+ @lease = @connection.lease_grant(@lease_seconds)
33
+
34
+ @connection.put(connection_key, connection_value, lease: @lease.ID)
35
+
36
+ @renew_connection_key = connection_key
37
+ @renew_connection_value = connection_value
38
+
39
+ renew_connection
22
40
  end
23
41
 
24
42
  def disconnect
25
- @connection.del(connection_key)
43
+ @connection.del(get_connection_key)
26
44
  end
27
45
 
28
46
  private
29
47
 
30
- def connection_key
48
+ def get_connection_key
31
49
  "#{@etcd_prefix}servers/#{@server_name}/#{@server_uuid}"
32
50
  end
33
51
 
34
- def connection_value
52
+ def get_connection_value
35
53
  JSON.generate(get_server_data)
36
54
  end
37
55
 
56
+ def renew_connection
57
+ stop_renew_connection
58
+
59
+ @renew_connection_thread = Thread.new do
60
+ loop do
61
+ sleep(@renew_connection_seconds)
62
+
63
+ @lease = @connection.lease_grant(@lease_seconds)
64
+ @connection.put(@renew_connection_key, @renew_connection_value,
65
+ lease: @lease.ID)
66
+ end
67
+ end
68
+ end
69
+
70
+ def stop_renew_connection
71
+ @renew_connection_thread.exit unless @renew_connection_thread.nil?
72
+ end
73
+
38
74
  def get_server_data
39
75
  {
40
76
  id: @server_uuid,
@@ -4,11 +4,26 @@ module RubyPitaya
4
4
 
5
5
  class HandlerRouter
6
6
 
7
- def initialize(handler_folder_path)
7
+ def initialize(handler_folder_path, routes_path)
8
+ import_routes_file(routes_path)
8
9
  import_handler_files(handler_folder_path)
10
+
11
+ import_routes_class
9
12
  import_handler_classes
10
13
  end
11
14
 
15
+ def import_routes_file(routes_path)
16
+ require routes_path
17
+ end
18
+
19
+ def import_routes_class
20
+ routes_classes = ObjectSpace.each_object(RoutesBase.singleton_class).select do |klass|
21
+ klass != RoutesBase
22
+ end
23
+
24
+ @routes = routes_classes.first.new
25
+ end
26
+
12
27
  def import_handler_files(handler_folder_path)
13
28
  Gem.find_files("#{handler_folder_path}/*.rb").each { |path| require path }
14
29
  end
@@ -22,6 +37,8 @@ module RubyPitaya
22
37
 
23
38
  @handler_name_map = @handlers.map do |handler|
24
39
  handler_name = handler.class.to_s
40
+ handler_name = @routes.get_new_handler_name(handler_name)
41
+ handler_name = handler_name.split('::').last
25
42
  handler_name = handler_name[0].downcase + handler_name[1..-1]
26
43
 
27
44
  [handler_name, handler]
@@ -7,6 +7,7 @@ require 'rubypitaya/core/config'
7
7
  require 'rubypitaya/core/session'
8
8
  require 'rubypitaya/core/postman'
9
9
  require 'rubypitaya/core/parameters'
10
+ require 'rubypitaya/core/routes_base'
10
11
  require 'rubypitaya/core/handler_router'
11
12
  require 'rubypitaya/core/etcd_connector'
12
13
  require 'rubypitaya/core/nats_connector'
@@ -24,7 +25,6 @@ module RubyPitaya
24
25
 
25
26
  attr_reader :redis_connector, :config, :bll
26
27
 
27
- private
28
28
  def initialize
29
29
  @environment_name = ENV.fetch("RUBYPITAYA_ENV") { 'development' }
30
30
  @is_development_environment = @environment_name == 'development'
@@ -39,10 +39,12 @@ module RubyPitaya
39
39
 
40
40
  @etcd_prefix = ENV['ETCD_PREFIX']
41
41
  @etcd_address = ENV['ETCD_URL']
42
+ @etcd_lease_seconds = ENV['ETCD_LEASE_SECONDS'].to_i
42
43
  @allow_reconnect = false
43
44
  @etcd_connector = EtcdConnector.new(@service_uuid, @desktop_name,
44
45
  @server_name, @etcd_prefix,
45
- @etcd_address, @allow_reconnect)
46
+ @etcd_address, @allow_reconnect,
47
+ @etcd_lease_seconds)
46
48
  @etcd_connector.connect
47
49
 
48
50
  @nats_address = ENV['NATS_URL']
@@ -68,13 +70,43 @@ module RubyPitaya
68
70
  @initializer_broadcast = InitializerBroadcast.new
69
71
  @initializer_broadcast.run(@initializer_content)
70
72
 
71
- @handler_router = HandlerRouter.new(Path::HANDLERS_FOLDER_PATH)
73
+ @handler_router = HandlerRouter.new(Path::HANDLERS_FOLDER_PATH,
74
+ Path::ROUTES_FILE_PATH)
75
+
76
+ run_server
77
+ end
78
+
79
+ private
80
+
81
+ def run_server
82
+ @is_shutting_down = false
72
83
 
73
- puts "Server started!"
74
- run_nats_connection
84
+ catch :sig_shutdown do
85
+ Signal.trap("SIGINT") { throw :sig_shutdown }
86
+ Signal.trap("SIGQUIT") { throw :sig_shutdown }
87
+ Signal.trap("SIGTERM") { throw :sig_shutdown }
88
+
89
+ puts "Server started!"
90
+ run_nats_connection
91
+ end
92
+
93
+ Signal.trap("SIGINT", nil)
94
+ Signal.trap("SIGQUIT", nil)
95
+ Signal.trap("SIGTERM", nil)
96
+
97
+ shutdown_server
98
+ end
99
+
100
+ def shutdown_server
101
+ return if @is_shutting_down
102
+ @is_shutting_down = true
103
+
104
+ puts "Server shutting down..."
75
105
 
76
106
  @etcd_connector.disconnect
77
107
  @database_connector.disconnect
108
+
109
+ exit(0)
78
110
  end
79
111
 
80
112
  def run_nats_connection
@@ -113,9 +145,6 @@ module RubyPitaya
113
145
 
114
146
  response
115
147
  end
116
- rescue SystemExit, Interrupt
117
- puts "Quit!"
118
- return 0
119
148
  rescue Exception => error
120
149
  puts "ERROR: #{error}"
121
150
  puts error.backtrace
@@ -9,5 +9,7 @@ module RubyPitaya
9
9
  HANDLERS_FOLDER_PATH = File.join(Dir.pwd, 'app/handlers/')
10
10
  APP_CONFIG_FOLDER_PATH = File.join(Dir.pwd, 'app/config/')
11
11
  MIGRATIONS_FOLDER_PATH = File.join(Dir.pwd, 'db/migrate/')
12
+
13
+ ROUTES_FILE_PATH = File.join(Dir.pwd, 'config/routes.rb')
12
14
  end
13
15
  end
@@ -0,0 +1,36 @@
1
+
2
+ module RubyPitaya
3
+
4
+ class RoutesBase
5
+
6
+ def initialize
7
+ @handler_name_map = {}
8
+
9
+ setup
10
+ end
11
+
12
+ def setup
13
+ end
14
+
15
+ def match(route_name, to:)
16
+ handler_name, action_name = to.split('#')
17
+ new_handler_name, new_action_name = route_name.split('.')
18
+
19
+ if new_action_name.nil?
20
+ set_handler_name(handler_name, new_handler_name)
21
+ end
22
+ end
23
+
24
+ def get_new_handler_name(handler_name)
25
+ return handler_name unless @handler_name_map.include?(handler_name)
26
+
27
+ @handler_name_map[handler_name]
28
+ end
29
+
30
+ private
31
+
32
+ def set_handler_name(handler_name, new_handler_name)
33
+ @handler_name_map[handler_name] = new_handler_name
34
+ end
35
+ end
36
+ end
@@ -1,4 +1,3 @@
1
1
  module RubyPitaya
2
- VERSION = "1.2.9"
2
+ VERSION = "1.5.0"
3
3
  end
4
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubypitaya
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.9
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luciano Prestes Cavalcanti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-27 00:00:00.000000000 Z
11
+ date: 2020-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -203,6 +203,7 @@ files:
203
203
  - "./lib/rubypitaya/app-template/app/models/user.rb"
204
204
  - "./lib/rubypitaya/app-template/bin/console"
205
205
  - "./lib/rubypitaya/app-template/config/database.yml"
206
+ - "./lib/rubypitaya/app-template/config/routes.rb"
206
207
  - "./lib/rubypitaya/app-template/db/migrate/001_create_user_migration.rb"
207
208
  - "./lib/rubypitaya/app-template/db/migrate/002_create_player_migration.rb"
208
209
  - "./lib/rubypitaya/app-template/docker-compose.yml"
@@ -227,6 +228,7 @@ files:
227
228
  - "./lib/rubypitaya/core/path.rb"
228
229
  - "./lib/rubypitaya/core/postman.rb"
229
230
  - "./lib/rubypitaya/core/redis_connector.rb"
231
+ - "./lib/rubypitaya/core/routes_base.rb"
230
232
  - "./lib/rubypitaya/core/session.rb"
231
233
  - "./lib/rubypitaya/version.rb"
232
234
  - bin/rubypitaya