ksconnect 0.1.3 → 0.1.4

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: 9033ab93825f6763a35f296a758ea63777e7c3db
4
- data.tar.gz: f6470fcf3b96720d6dd1e400999afbc96b9866aa
3
+ metadata.gz: 01bb45ec16d68199879715b17d8926ba15a23f5d
4
+ data.tar.gz: 0b62b79b1bf8d5b9deaa7aeb3aac0b85844486df
5
5
  SHA512:
6
- metadata.gz: d1c5b726757accd1e09b23c93803316e049cafc7f8dfce9b91966f65a4de2adc26bd322aca158ca42a1463ae183c3f0f6ac4b2e820f24bae7ddfd11a38cb5da4
7
- data.tar.gz: 8903eaae449f8fdbdcc7025aef062bfaace099bdf508881c4f7048c6416acf55a1a55ff2d14145bdfae1fe421309e6a6e01dbe0dab1d3956cf749dd62c745371
6
+ metadata.gz: ec46f07360b655b7fb433d56f73b6d7c51adcfaad60c2db91577449c38de641aa4ed4eeb0db37708508ab78757a06211882197e72d872a7dc2ada76212e75ad2
7
+ data.tar.gz: 55743f59b294f8f6d2336bca125019867d29efbfb7d80edfa982a16c5ce470c53b9aa08593912c93edd8e62c64d1479eb4e99c75ca4b59ddf7344057769c1f3a
@@ -1,10 +1,12 @@
1
1
  require 'redis'
2
2
  require 'active_support/hash_with_indifferent_access'
3
+ require 'ksconnect/logs'
3
4
 
4
5
  class KSConnect
5
6
  class API
6
7
  class Plugin
7
8
  class Data
9
+ include Logs
8
10
  attr_reader :type
9
11
 
10
12
  def initialize(plugin_name, domain_name, type = :data, use_cache = true)
@@ -13,26 +15,26 @@ class KSConnect
13
15
  @type = type
14
16
  @use_cache = use_cache
15
17
 
16
- @data = ActiveSupport::HashWithIndifferentAccess.new if @use_cache
17
- @data_uuid = nil
18
+ @cache = ActiveSupport::HashWithIndifferentAccess.new if @use_cache
19
+ @cache_uuid = nil
18
20
  end
19
21
 
20
22
  def []=(field, value)
21
- @data[field] = value if @use_cache
23
+ @cache[field] = value if @use_cache
22
24
  redis.hset(key, field, value)
23
- redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' })
25
+ redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json)
24
26
  end
25
27
 
26
28
  def setall(hash)
27
- @data = @data.merge(hash) if @use_cache
29
+ @cache = @cache.merge(hash) if @use_cache
28
30
  redis.mapped_hmset(key, hash)
29
- redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' })
31
+ redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json)
30
32
  end
31
33
 
32
34
  def [](field)
33
35
  if @use_cache
34
- @data ||= redis.hgetall(key)
35
- @data[field]
36
+ @cache = redis.hgetall(key) if @cache.empty?
37
+ @cache[field]
36
38
  else
37
39
  redis.hget(key, field)
38
40
  end
@@ -40,24 +42,25 @@ class KSConnect
40
42
 
41
43
  def getall
42
44
  if @use_cache
43
- @data ||= redis.hgetall(key)
45
+ @cache = redis.hgetall(key) if @cache.empty?; @cache
44
46
  else
45
47
  redis.hgetall(key)
46
48
  end
47
49
  end
48
50
 
49
51
  def reload
50
- @data = redis.hgetall(key) if @use_cache
52
+ @cache = redis.hgetall(key) if @use_cache
53
+ @cache
51
54
  end
52
55
 
53
56
  def delete(field)
54
- @data.delete(field) if @use_cache
57
+ @cache.delete(field) if @use_cache
55
58
  redis.hdel(key, field)
56
59
  end
57
60
 
58
61
  def key
59
- set_data_uuid unless @data_uuid
60
- "kloudsec_data:#{@data_uuid}"
62
+ set_data_uuid unless @cache_uuid
63
+ "kloudsec_data:#{@cache_uuid}"
61
64
  end
62
65
 
63
66
  private
@@ -67,10 +70,10 @@ class KSConnect
67
70
  tries ||= 3
68
71
  id = redis.hget("#{@plugin_name}:#{@type}", @domain_name)
69
72
  if id
70
- @data_uuid = id
73
+ @cache_uuid = id
71
74
  else
72
- @data_uuid = SecureRandom.uuid
73
- raise "Race on setting data key failed." unless redis.hsetnx("#{@plugin_name}:#{@type}", @domain_name, @data_uuid)
75
+ @cache_uuid = SecureRandom.uuid
76
+ raise "Race on setting data key failed." unless redis.hsetnx("#{@plugin_name}:#{@type}", @domain_name, @cache_uuid)
74
77
  end
75
78
  rescue Exception => e
76
79
  logger.error e.message
@@ -1,4 +1,5 @@
1
1
  require 'redis'
2
+ require 'json'
2
3
 
3
4
  class KSConnect
4
5
  class API
@@ -18,10 +19,14 @@ class KSConnect
18
19
  @private_data = Data.new(plugin_name, name, :private_data)
19
20
  end
20
21
 
22
+ def update_ip_address
23
+ @ip_address = redis.hget(domains_key, name)
24
+ end
25
+
21
26
  def ip_address=(new_ip)
22
27
  @ip_address = new_ip
23
28
  redis.hset(domains_key, name, new_ip)
24
- redis.publish("core:push", { domain_name: name, plugin_name: "core", request_type: "update"}) if plugin_name == 'core'
29
+ redis.publish("core:push", { domain_name: name, plugin_name: "core", request_type: "update"}.to_json) if plugin_name == 'core'
25
30
  end
26
31
 
27
32
  def notify(data)
@@ -1,15 +1,18 @@
1
1
  require 'redis'
2
2
  require 'json'
3
+ require 'ksconnect/logs'
3
4
 
4
5
  class KSConnect
5
6
  class API
6
7
  class Plugin
8
+ include Logs
7
9
  attr_accessor :domains
8
10
  attr_reader :name
9
11
  attr_writer :config
10
12
 
11
- def initialize(name)
13
+ def initialize(name, main = false)
12
14
  @name = name
15
+ @main_plugin = main
13
16
  @domains = {}
14
17
 
15
18
  load_domains
@@ -39,11 +42,12 @@ class KSConnect
39
42
  end
40
43
 
41
44
  def subscribe_to_events
42
- KSConnect.channel("#{name}:push") do |message|
45
+ KSConnect.channel("#{@name}:push") do |message|
43
46
  begin
44
47
  msg = JSON.parse(message)
45
48
  rescue Exception => e
46
- logger.error "Error parsing message as JSON: #{msg}"
49
+ logger.error e
50
+ logger.error "Error parsing message as JSON: #{message}"
47
51
  next
48
52
  end
49
53
 
@@ -67,7 +71,7 @@ class KSConnect
67
71
  @domains[domain_name] = Domain.new(domain_name, get_ip_for(domain_name), @name)
68
72
  config.on_initialize.call(request)
69
73
  when 'update'
70
- @domains[domain_name].ip_address = get_ip_for(domain_name)
74
+ @domains[domain_name].update_ip_address
71
75
  config.on_update.call(request)
72
76
  when 'teardown'
73
77
  @domains.delete(domain_name)
@@ -76,7 +80,7 @@ class KSConnect
76
80
  raise "Invalid request type"
77
81
  end
78
82
 
79
- redis.publish("core:push", { domain_name: domain_name, plugin_name: @name, request_type: request_type }.to_json) unless plugin_is_core?
83
+ redis.publish("core:push", { domain_name: domain_name, plugin_name: @name, request_type: request_type }.to_json) if should_repush?
80
84
  end
81
85
 
82
86
  def domains_key
@@ -86,8 +90,8 @@ class KSConnect
86
90
 
87
91
  private
88
92
 
89
- def plugin_is_core?
90
- @name == 'core'
93
+ def should_repush?
94
+ @main_plugin && @name != 'core'
91
95
  end
92
96
 
93
97
  def get_ip_for(domain_name)
data/lib/ksconnect/api.rb CHANGED
@@ -5,9 +5,24 @@ class KSConnect
5
5
 
6
6
  def initialize(opts)
7
7
  enabled_plugins = *opts[:enabled_plugins] || []
8
- plugins = enabled_plugins.unshift(:core).uniq # always load core first
9
- @plugins = plugins.reduce({}) { |hash, plugin_name| hash.tap { |h| h[plugin_name] = KSConnect::API::Plugin.new(plugin_name.to_s) } }
10
- @plugin = @plugins[*opts[:enabled_plugins].first || :core]
8
+
9
+ # init the plugins that will be used
10
+ @plugins = {}
11
+
12
+ # always init the core plugin
13
+ enabled_plugins.delete(:core)
14
+ @plugins[:core] = KSConnect::API::Plugin.new('core', enabled_plugins.empty?)
15
+
16
+ # set the main plugin
17
+ unless enabled_plugins.empty?
18
+ plugin_name = enabled_plugins.shift
19
+ @plugin = @plugins[plugin_name] = KSConnect::API::Plugin.new(plugin_name.to_s, true)
20
+ end
21
+
22
+ # init the other plugins
23
+ enabled_plugins.each do |plugin_name|
24
+ @plugins[plugin_name] = KSConnect::API::Plugin.new(plugin_name.to_s)
25
+ end
11
26
 
12
27
  if opts[:use_helpers]
13
28
  extend KSConnect::Helpers
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ksconnect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Poon