rubypitaya 1.3.0 → 1.6.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: 308569ffd1a21f885317d944646e19c7f95159c4c64b900c3fc89bc5bca2321c
4
- data.tar.gz: 4f89dc81fbc999187538019916121cef7d982dafd88edca1fe665518c4c9225b
3
+ metadata.gz: d3c2d65cb66c8a89ffc58b797ccef1f404d81056f912d98f7e656ea63271e5c7
4
+ data.tar.gz: b8d089767fa6fff1c092fa570fcf730fdf77e83a656669ad71204a67585345e5
5
5
  SHA512:
6
- metadata.gz: f6a6ce18e3c2ff2afb15b1408edd05c2c9ac6920807360d21f1a2e24fc4f68912b5eabf9cf023ec873962150f7547948dab9c039b88febd0493d151fd0a27c04
7
- data.tar.gz: 1d0272741a51cea4cd1253185ede2d3c6cbef88a328d4562eacfdc9a9269f96d0af817d51898f9ee154a3428c18aec69768e0fdbb0f27c01ea7372076ebed0a9
6
+ metadata.gz: aa66e24ec0a0027136ebcd4d329f2b4fa3c7ee1347343d002b5e9d875040e7cfaf894bdd595060dfbacc43e0f102a760bcbf9177871cb4814e1c98bc5be1865c
7
+ data.tar.gz: 37d7f64a4417a6544a8f9f3faca2f700dfdf7489de03c4f3321de1e359edc903281402b1ada1f8c31fc349756a900f0a796760b339193d46aac34da48a148f85
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem 'rubypitaya', '1.3.0'
3
+ gem 'rubypitaya', '1.6.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.3.0)
65
+ rubypitaya (1.6.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.3.0)
88
+ rubypitaya (= 1.6.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'
@@ -38,10 +39,12 @@ module RubyPitaya
38
39
 
39
40
  @etcd_prefix = ENV['ETCD_PREFIX']
40
41
  @etcd_address = ENV['ETCD_URL']
42
+ @etcd_lease_seconds = ENV['ETCD_LEASE_SECONDS'].to_i
41
43
  @allow_reconnect = false
42
44
  @etcd_connector = EtcdConnector.new(@service_uuid, @desktop_name,
43
45
  @server_name, @etcd_prefix,
44
- @etcd_address, @allow_reconnect)
46
+ @etcd_address, @allow_reconnect,
47
+ @etcd_lease_seconds)
45
48
  @etcd_connector.connect
46
49
 
47
50
  @nats_address = ENV['NATS_URL']
@@ -67,7 +70,8 @@ module RubyPitaya
67
70
  @initializer_broadcast = InitializerBroadcast.new
68
71
  @initializer_broadcast.run(@initializer_content)
69
72
 
70
- @handler_router = HandlerRouter.new(Path::HANDLERS_FOLDER_PATH)
73
+ @handler_router = HandlerRouter.new(Path::HANDLERS_FOLDER_PATH,
74
+ Path::ROUTES_FILE_PATH)
71
75
 
72
76
  run_server
73
77
  end
@@ -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.3.0"
2
+ VERSION = "1.6.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.3.0
4
+ version: 1.6.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-05-01 00:00:00.000000000 Z
11
+ date: 2020-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -201,8 +201,10 @@ files:
201
201
  - "./lib/rubypitaya/app-template/app/handlers/player_handler.rb"
202
202
  - "./lib/rubypitaya/app-template/app/models/player.rb"
203
203
  - "./lib/rubypitaya/app-template/app/models/user.rb"
204
+ - "./lib/rubypitaya/app-template/apptemplate-0.1.0.gem"
204
205
  - "./lib/rubypitaya/app-template/bin/console"
205
206
  - "./lib/rubypitaya/app-template/config/database.yml"
207
+ - "./lib/rubypitaya/app-template/config/routes.rb"
206
208
  - "./lib/rubypitaya/app-template/db/migrate/001_create_user_migration.rb"
207
209
  - "./lib/rubypitaya/app-template/db/migrate/002_create_player_migration.rb"
208
210
  - "./lib/rubypitaya/app-template/docker-compose.yml"
@@ -227,6 +229,7 @@ files:
227
229
  - "./lib/rubypitaya/core/path.rb"
228
230
  - "./lib/rubypitaya/core/postman.rb"
229
231
  - "./lib/rubypitaya/core/redis_connector.rb"
232
+ - "./lib/rubypitaya/core/routes_base.rb"
230
233
  - "./lib/rubypitaya/core/session.rb"
231
234
  - "./lib/rubypitaya/version.rb"
232
235
  - bin/rubypitaya