consistent-cluster 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b9a381e3ba8ba30496deec02f82acb44f03c608
4
- data.tar.gz: d91a5fe213f3fc911c8e8dae829b8489b020d632
3
+ metadata.gz: 1d7447f969567786aef54f72b5c03abfa42bb860
4
+ data.tar.gz: a6b96f1db23abe63dc2b1411875c0fcb98a2bd4d
5
5
  SHA512:
6
- metadata.gz: 24a73b4697edc754ca7ca59ca039534540f16da968050ada4f5a80b01fd28cc650e2a8e08dd445732c57c34f5923293d8312190e5cccb92edd2c2c2a9099ee90
7
- data.tar.gz: a8b4369cf7f64d8b597ad75004f93c1f6b96e4d578f596de8ceb43d3a7ea58387a22cbe7cbcb17e9a1794af46b44d324d81439f4d4a90c9d2a5b9280d5c5cd86
6
+ metadata.gz: ad96b31d7dd661ca27464b94a8d84d1f7a332cb46d4f423825fbdf0884f1845984537131f02e6839b3a2ded5959acfa1403b85620f7f09babc1b5b4f33c0fb72
7
+ data.tar.gz: 03abd91418a5a9ddc432fd1448b1dbf3ca71cf93dc93a95fac6a1bdae41bf328753bd0fc17aa925249c8d019eeaa0273848e941f3f1022633f494f022ee5329f
@@ -11,6 +11,8 @@ module ConsistentCluster
11
11
 
12
12
  def initialize(options)
13
13
 
14
+ @syncingMutex = Mutex.new
15
+
14
16
  @data = {}
15
17
  @cluster = {}
16
18
  @node_register = {}
@@ -20,10 +22,19 @@ module ConsistentCluster
20
22
 
21
23
  @create_proc = options[:create_proc]
22
24
  @destroy_proc = options[:destroy_proc]
23
- @after_sync_proc = options[:after_sync_proc]
25
+ @after_sync_proc = options[:after_sync_proc]
26
+ @log_proc = options[:log_proc]
27
+
28
+ @logger = if options[:log_file]
29
+ Logger.new(options[:log_file])
30
+ elsif STDERR || STDOUT
31
+ Logger.new(STDERR || STDOUT)
32
+ else
33
+ Logger.new(options[:nil])
34
+ end
35
+
36
+ @last_sync_at = 0
24
37
 
25
- @to_sync,@syncing = false,false
26
-
27
38
  @path = options[:zookeeper_path]
28
39
 
29
40
  @zk = ZK.new(options[:zookeeper_service],reconnect: true)
@@ -65,47 +76,81 @@ module ConsistentCluster
65
76
  server
66
77
  end
67
78
 
79
+ def invoke_sync
80
+ sync_services
81
+ end
82
+
83
+ def local_version
84
+ @data
85
+ end
86
+
87
+ def remote_version
88
+ app_names = get_app_names(watch: false)
89
+
90
+ data = {}
91
+ app_names.each do |app_name|
92
+ app_content = get_app_content(app_name, watch: false)
93
+ data[app_name] = app_content
94
+ end
95
+ data
96
+ end
97
+
98
+ def zookeeper_path
99
+ @path
100
+ end
101
+
102
+ def last_sync_at
103
+ @last_sync_at
104
+ end
105
+
68
106
  protected
69
107
 
70
108
  def sync_services
71
- @to_sync = true
72
- if !@syncing
109
+ @syncingMutex.synchronize do
73
110
  syncing_process
74
111
  end
75
112
  end
76
113
 
77
114
  def syncing_process
78
- @syncing = true
79
- while @to_sync
80
- @to_sync = false
81
- app_names = get_app_names
82
- current_app_names = @cluster.keys
83
-
84
- to_update = current_app_names&app_names
85
- to_create = app_names - current_app_names
86
- to_destroy = current_app_names - app_names
87
-
88
- to_update.each do |app_name|
89
- update_service(app_name)
90
- end
115
+ app_names = get_app_names
116
+ current_app_names = @cluster.keys
91
117
 
92
- to_create.each do |app_name|
93
- create_service(app_name)
94
- end
118
+ to_update = current_app_names&app_names
119
+ to_create = app_names - current_app_names
120
+ to_destroy = current_app_names - app_names
95
121
 
96
- to_destroy.each do |app_name|
97
- destroy_service(app_name)
98
- end
122
+ to_update.each do |app_name|
123
+ update_service(app_name)
124
+ end
125
+
126
+ to_create.each do |app_name|
127
+ create_service(app_name)
128
+ end
129
+
130
+ to_destroy.each do |app_name|
131
+ destroy_service(app_name)
99
132
  end
100
- @syncing = false
101
- if @after_sync_proc
133
+
134
+ @last_sync_at = Time.now.to_i
135
+
136
+ if @after_sync_proc || @log_proc
102
137
  clone_data = Marshal.load(Marshal.dump(@data)) #avoid change outside
103
- @after_sync_proc.call(clone_data)
138
+
139
+ if @after_sync_proc
140
+ @after_sync_proc.call(clone_data)
141
+ end
142
+
143
+ if @log_proc
144
+ debugInfo = @log_proc.call(@data).to_s
145
+ if debugInfo != ""
146
+ @logger.info debugInfo
147
+ end
148
+ end
104
149
  end
105
150
  end
106
151
 
107
- def get_app_names
108
- @zk.children(@path, watch: true)
152
+ def get_app_names(options = {watch: true})
153
+ @zk.children(@path, watch: options[:watch])
109
154
  end
110
155
 
111
156
  def create_service(app_name)
@@ -130,7 +175,7 @@ module ConsistentCluster
130
175
  end
131
176
 
132
177
  rescue Exception => boom
133
- puts "sync create :#{app_name} raise #{boom.class} - #{boom.message}"
178
+ @logger.error "syncClientError when create :#{app_name} raise #{boom.class} - #{boom.message}"
134
179
  end
135
180
 
136
181
  def destroy_service(app_name)
@@ -151,7 +196,7 @@ module ConsistentCluster
151
196
  end
152
197
 
153
198
  rescue Exception => boom
154
- puts "sync destroy :#{app_name} raise #{boom.class} - #{boom.message}"
199
+ @logger.error "syncClientError when destroy :#{app_name} raise #{boom.class} - #{boom.message}"
155
200
  end
156
201
 
157
202
  def update_service(app_name)
@@ -166,15 +211,15 @@ module ConsistentCluster
166
211
  create_service(app_name)
167
212
  end
168
213
  rescue Exception => boom
169
- puts "sync update :#{app_name} raise #{boom.class} - #{boom.message}"
214
+ @logger.error "syncClientError when update :#{app_name} raise #{boom.class} - #{boom.message}"
170
215
  end
171
216
 
172
- def get_app_content(app_name)
217
+ def get_app_content(app_name, options={watch: true})
173
218
  app_path = "#{@path}/#{app_name}"
174
- content = @zk.get(app_path, watch: true).first
219
+ content = @zk.get(app_path, watch: options[:watch]).first
175
220
  content
176
221
  end
177
222
 
178
223
  end
179
224
 
180
- end
225
+ end
@@ -1,3 +1,3 @@
1
1
  module ConsistentCluster
2
- Version = "1.0.3"
3
- end
2
+ Version = "1.0.4"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consistent-cluster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - jeffrey6052
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-14 00:00:00.000000000 Z
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: atomic
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  version: '0'
63
63
  requirements: []
64
64
  rubyforge_project:
65
- rubygems_version: 2.2.2
65
+ rubygems_version: 2.5.1
66
66
  signing_key:
67
67
  specification_version: 4
68
68
  summary: 一致性哈希集群