bitgirder-platform 0.1.7

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.
Files changed (51) hide show
  1. data/LICENSE.txt +176 -0
  2. data/bin/ensure-test-db +117 -0
  3. data/bin/install-mysql +375 -0
  4. data/bin/tomcat7 +569 -0
  5. data/lib/bitgirder/concurrent.rb +400 -0
  6. data/lib/bitgirder/core.rb +1235 -0
  7. data/lib/bitgirder/etl.rb +58 -0
  8. data/lib/bitgirder/event/file.rb +485 -0
  9. data/lib/bitgirder/event/logger/testing.rb +140 -0
  10. data/lib/bitgirder/event/logger.rb +137 -0
  11. data/lib/bitgirder/event/testing.rb +88 -0
  12. data/lib/bitgirder/http.rb +255 -0
  13. data/lib/bitgirder/io/testing.rb +33 -0
  14. data/lib/bitgirder/io.rb +959 -0
  15. data/lib/bitgirder/irb.rb +35 -0
  16. data/lib/bitgirder/mysql.rb +60 -0
  17. data/lib/bitgirder/ops/java.rb +117 -0
  18. data/lib/bitgirder/ops/ruby.rb +235 -0
  19. data/lib/bitgirder/testing.rb +152 -0
  20. data/lib/doc-gen0.rb +0 -0
  21. data/lib/doc-gen1.rb +0 -0
  22. data/lib/doc-gen10.rb +0 -0
  23. data/lib/doc-gen11.rb +0 -0
  24. data/lib/doc-gen12.rb +0 -0
  25. data/lib/doc-gen13.rb +0 -0
  26. data/lib/doc-gen14.rb +0 -0
  27. data/lib/doc-gen15.rb +0 -0
  28. data/lib/doc-gen16.rb +0 -0
  29. data/lib/doc-gen17.rb +14 -0
  30. data/lib/doc-gen18.rb +0 -0
  31. data/lib/doc-gen19.rb +0 -0
  32. data/lib/doc-gen2.rb +0 -0
  33. data/lib/doc-gen20.rb +182 -0
  34. data/lib/doc-gen21.rb +0 -0
  35. data/lib/doc-gen3.rb +0 -0
  36. data/lib/doc-gen4.rb +0 -0
  37. data/lib/doc-gen5.rb +0 -0
  38. data/lib/doc-gen6.rb +0 -0
  39. data/lib/doc-gen7.rb +0 -0
  40. data/lib/doc-gen8.rb +0 -0
  41. data/lib/doc-gen9.rb +0 -0
  42. data/lib/mingle/bincodec.rb +512 -0
  43. data/lib/mingle/codec.rb +54 -0
  44. data/lib/mingle/http.rb +156 -0
  45. data/lib/mingle/io/stream.rb +142 -0
  46. data/lib/mingle/io.rb +160 -0
  47. data/lib/mingle/json.rb +257 -0
  48. data/lib/mingle/service.rb +110 -0
  49. data/lib/mingle-em.rb +92 -0
  50. data/lib/mingle.rb +2917 -0
  51. metadata +100 -0
@@ -0,0 +1,400 @@
1
+ require 'bitgirder/core'
2
+
3
+ module BitGirder
4
+ module Concurrent
5
+
6
+ # Classes and modules in here which make use of event machine will call this to
7
+ # require it. We don't want to put a bare 'require' statement in this file so as
8
+ # to allow for parts of BitGirder::Concurrent to be used without event machine
9
+ # being present.
10
+ def self.require_em
11
+ require 'eventmachine'
12
+ end
13
+
14
+ class Completion
15
+
16
+ private_class_method :new
17
+
18
+ def initialize( res, ex )
19
+ @res, @ex = res, ex
20
+ end
21
+
22
+ public
23
+ def ok?
24
+ @ex == nil
25
+ end
26
+
27
+ alias ok ok?
28
+ alias is_ok ok?
29
+ alias is_ok? ok?
30
+
31
+ public
32
+ def get
33
+ ok? ? @res : ( raise @ex )
34
+ end
35
+
36
+ private
37
+ def access( ok_expct, meth_name, ret_val )
38
+
39
+ ok_actual = ok?
40
+
41
+ if ( ok_actual == ok_expct )
42
+ ret_val
43
+ else
44
+ raise "Attempt to call #{meth_name} when ok? returns #{ok_actual}"
45
+ end
46
+ end
47
+
48
+ public
49
+ def get_result
50
+ access( true, __method__, @res )
51
+ end
52
+
53
+ alias result get_result
54
+
55
+ public
56
+ def get_exception
57
+ access( false, __method__, @ex )
58
+ end
59
+
60
+ alias exception get_exception
61
+
62
+ public
63
+ def to_s
64
+ self.inspect
65
+ end
66
+
67
+ def self.create_success( res = nil )
68
+ new( res, nil )
69
+ end
70
+
71
+ def self.create_failure( ex )
72
+
73
+ BitGirder::Core::BitGirderMethods.not_nil( ex, :ex )
74
+ new( nil, ex )
75
+ end
76
+ end
77
+
78
+ class Rendezvous < BitGirder::Core::BitGirderClass
79
+
80
+ class RendezvousStateError < StandardError; end
81
+ class ClosedError < RendezvousStateError; end
82
+ class UnderflowError< RendezvousStateError; end
83
+
84
+ attr_reader :remain
85
+
86
+ def initialize( &blk )
87
+
88
+ super( {} )
89
+
90
+ @on_join = blk # could be nil
91
+ @remain = 0
92
+ end
93
+
94
+ public
95
+ def fire
96
+
97
+ raise ClosedError if closed?
98
+ @remain += 1
99
+ end
100
+
101
+ private
102
+ def check_complete
103
+
104
+ if @remain == 0 && closed?
105
+
106
+ if @on_join
107
+ @on_join.call
108
+ end
109
+ end
110
+ end
111
+
112
+ public
113
+ def arrive
114
+
115
+ if @remain == 0
116
+ raise UnderflowError
117
+ else
118
+ check_complete if @remain -= 1
119
+ end
120
+ end
121
+
122
+ public
123
+ def closed?
124
+ @closed
125
+ end
126
+
127
+ public
128
+ def close
129
+
130
+ if closed?
131
+ raise ClosedError
132
+ else
133
+ @closed = true
134
+ check_complete
135
+ end
136
+ end
137
+
138
+ public
139
+ def open?
140
+ ! closed?
141
+ end
142
+
143
+ # Used by self.run() below to build a call sequence
144
+ class Run
145
+
146
+ def initialize
147
+ @fires = []
148
+ end
149
+
150
+ def complete( &blk )
151
+ @on_join = ( blk or raise "Need a block" )
152
+ end
153
+
154
+ def fire( &blk )
155
+ @fires << ( blk or raise "Need a block" )
156
+ end
157
+ end
158
+
159
+ def self.run
160
+
161
+ run = Run.new
162
+ yield( run )
163
+
164
+ blk = run.instance_variable_get( :@on_join )
165
+ raise "Need a complete block" unless blk
166
+
167
+ r = Rendezvous.new( &blk )
168
+
169
+ run.instance_variable_get( :@fires ).each { |f| r.fire; f.call( r ) }
170
+ r.close
171
+ end
172
+ end
173
+
174
+ class Retry < BitGirder::Core::BitGirderClass
175
+
176
+ class Builder
177
+
178
+ attr_accessor :retries, :seed_secs
179
+
180
+ def initialize
181
+
182
+ # Set defaults
183
+ @retries = 3
184
+ @seed_secs = 1.0
185
+ @failed = lambda { |err| raise err }
186
+ @retry_on = [ Exception ]
187
+ end
188
+
189
+ [ :action, :async_action, :complete, :failed ].each do |m|
190
+ class_eval <<-CODE
191
+ # Works as a setter during build and an accessor, used
192
+ # internally by the containing Retry instance
193
+ def #{m}( &blk )
194
+ @#{m} = blk if blk
195
+ @#{m}
196
+ end
197
+ CODE
198
+ end
199
+
200
+ def retry_on( *argv, &blk )
201
+
202
+ if blk
203
+ if argv.empty?
204
+ @retry_on = blk
205
+ else
206
+ raise "Can't combine block and rescue target list"
207
+ end
208
+ else
209
+ if argv.empty?
210
+ raise "Need at least one rescue target"
211
+ else
212
+ @retry_on = Array.new( argv )
213
+ end
214
+ end
215
+ end
216
+ end
217
+
218
+ attr_reader :attempt, :start_time
219
+
220
+ def initialize( *argv )
221
+
222
+ super
223
+ BitGirder::Concurrent.require_em
224
+
225
+ @bldr = Builder.new
226
+ yield @bldr if block_given?
227
+ @bldr.freeze
228
+
229
+ [ :retries, :seed_secs, :retry_on ].each do |m|
230
+ raise ":#{m} not set" unless @bldr.instance_variable_get( :"@#{m}" )
231
+ end
232
+
233
+ if @bldr.action || @bldr.async_action
234
+ if @bldr.action && @bldr.async_action
235
+ raise "Both a synchronous and asynchronous action were set"
236
+ end
237
+ else
238
+ raise "Neither :action nor :async_action was set"
239
+ end
240
+
241
+ @attempt = 0
242
+ end
243
+
244
+ public
245
+ def seed_secs
246
+ @bldr.seed_secs
247
+ end
248
+
249
+ private
250
+ def should_retry?( err )
251
+
252
+ case val = @bldr.instance_variable_get( :@retry_on )
253
+
254
+ when Array then val.find { |err_cls| err.is_a?( err_cls ) }
255
+ else val.call( err )
256
+ end
257
+ end
258
+
259
+ private
260
+ def invoke_call( val, call, name )
261
+
262
+ args =
263
+ case arity = call.arity
264
+ when 0, -1 then []
265
+ when 1 then [ val ]
266
+ when 2 then [ self, val ]
267
+ else raise "Unexpected arity in #{name}: #{arity}"
268
+ end
269
+
270
+ call.call( *args )
271
+ end
272
+
273
+ private
274
+ def fail_final( err )
275
+ invoke_call( err, @bldr.failed, "block :failed" )
276
+ end
277
+
278
+ private
279
+ def complete_final( res )
280
+
281
+ if @bldr.complete
282
+
283
+ begin
284
+ invoke_call( res, @bldr.complete, "block :complete" )
285
+ rescue Exception => e
286
+ fail_final( e )
287
+ end
288
+ end
289
+ end
290
+
291
+ private
292
+ def action_failed( err )
293
+
294
+ if res = should_retry?( err )
295
+
296
+ if @attempt == @bldr.retries
297
+ fail_final( err )
298
+ else
299
+ dur = @bldr.seed_secs * ( 1 << ( @attempt - 1 ) )
300
+ EM.add_timer( dur ) { run_attempt }
301
+ end
302
+ else
303
+ fail_final( err )
304
+ end
305
+ end
306
+
307
+ # Callback for an async action
308
+ public
309
+ def complete( res = nil )
310
+
311
+ if block_given?
312
+
313
+ if res == nil
314
+ begin
315
+ complete_final( yield ) # okay for yield to return nil
316
+ rescue Exception => e
317
+ action_failed( e )
318
+ end
319
+ else
320
+ raise "Block passed to complete with non-nil result: #{res}"
321
+ end
322
+ else
323
+ complete_final( res )
324
+ end
325
+ end
326
+
327
+ public
328
+ def fail_attempt( err )
329
+ complete { raise err }
330
+ end
331
+
332
+ private
333
+ def run_attempt
334
+
335
+ @attempt += 1
336
+
337
+ begin
338
+ if @bldr.action
339
+ complete_final( @bldr.action.call( self ) )
340
+ else
341
+ @bldr.async_action.call( self )
342
+ end
343
+ rescue Exception => e
344
+ action_failed( e )
345
+ end
346
+ end
347
+
348
+ public
349
+ def run
350
+
351
+ if @start_time
352
+ raise "run() already called"
353
+ else
354
+ @start_time = Time.now
355
+ run_attempt
356
+ end
357
+ end
358
+
359
+ def Retry.run( *argv, &blk )
360
+ Retry.new( *argv, &blk ).run
361
+ end
362
+ end
363
+
364
+ module EmUtils
365
+
366
+ extend BitGirder::Core::BitGirderMethods
367
+
368
+ @@did_require = false
369
+
370
+ def self.ensure_em
371
+ unless @@did_require
372
+ Concurrent.require_em
373
+ @@did_require = true
374
+ end
375
+ end
376
+
377
+ def defer( callback, &blk )
378
+
379
+ ensure_em
380
+
381
+ not_nil( callback, :callback )
382
+ raise "Need an operation block" unless blk
383
+
384
+ op =
385
+ lambda do
386
+ begin
387
+ Completion.create_success( blk.call )
388
+ rescue Exception => e
389
+ Completion.create_failure( e )
390
+ end
391
+ end
392
+
393
+ EM.defer( op, callback )
394
+ end
395
+ module_function :defer
396
+ end
397
+
398
+
399
+ end
400
+ end