ambethia-bj 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/HISTORY +80 -0
  2. data/LICENSE +1 -0
  3. data/README +318 -0
  4. data/Rakefile +20 -0
  5. data/TODO +53 -0
  6. data/VERSION +1 -0
  7. data/bin/bj +692 -0
  8. data/bj.gemspec +150 -0
  9. data/init.rb +1 -0
  10. data/install.rb +214 -0
  11. data/lib/bj.rb +85 -0
  12. data/lib/bj/api.rb +164 -0
  13. data/lib/bj/bj.rb +72 -0
  14. data/lib/bj/errors.rb +4 -0
  15. data/lib/bj/joblist.rb +112 -0
  16. data/lib/bj/logger.rb +50 -0
  17. data/lib/bj/runner.rb +330 -0
  18. data/lib/bj/stdext.rb +86 -0
  19. data/lib/bj/table.rb +426 -0
  20. data/lib/bj/util.rb +133 -0
  21. data/plugin/HISTORY +3 -0
  22. data/plugin/README +142 -0
  23. data/plugin/Rakefile +22 -0
  24. data/plugin/init.rb +33 -0
  25. data/plugin/install.rb +95 -0
  26. data/plugin/script/bj +55 -0
  27. data/plugin/tasks/bj_tasks.rake +4 -0
  28. data/plugin/test/bj_test.rb +8 -0
  29. data/plugin/uninstall.rb +1 -0
  30. data/spec/bj.rb +80 -0
  31. data/spec/helper.rb +85 -0
  32. data/spec/rails_root/README +256 -0
  33. data/spec/rails_root/Rakefile +10 -0
  34. data/spec/rails_root/app/controllers/application.rb +15 -0
  35. data/spec/rails_root/app/helpers/application_helper.rb +3 -0
  36. data/spec/rails_root/config/boot.rb +109 -0
  37. data/spec/rails_root/config/database.yml +23 -0
  38. data/spec/rails_root/config/environment.rb +67 -0
  39. data/spec/rails_root/config/environments/development.rb +17 -0
  40. data/spec/rails_root/config/environments/production.rb +22 -0
  41. data/spec/rails_root/config/environments/test.rb +22 -0
  42. data/spec/rails_root/config/initializers/inflections.rb +10 -0
  43. data/spec/rails_root/config/initializers/mime_types.rb +5 -0
  44. data/spec/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  45. data/spec/rails_root/config/routes.rb +41 -0
  46. data/spec/rails_root/db/development.sqlite3 +0 -0
  47. data/spec/rails_root/db/migrate/20080909151517_bj_migration0.rb +8 -0
  48. data/spec/rails_root/db/schema.rb +62 -0
  49. data/spec/rails_root/doc/README_FOR_APP +2 -0
  50. data/spec/rails_root/log/development.log +141 -0
  51. data/spec/rails_root/log/production.log +0 -0
  52. data/spec/rails_root/log/server.log +0 -0
  53. data/spec/rails_root/log/test.log +0 -0
  54. data/spec/rails_root/public/404.html +30 -0
  55. data/spec/rails_root/public/422.html +30 -0
  56. data/spec/rails_root/public/500.html +30 -0
  57. data/spec/rails_root/public/dispatch.cgi +10 -0
  58. data/spec/rails_root/public/dispatch.fcgi +24 -0
  59. data/spec/rails_root/public/dispatch.rb +10 -0
  60. data/spec/rails_root/public/favicon.ico +0 -0
  61. data/spec/rails_root/public/images/rails.png +0 -0
  62. data/spec/rails_root/public/index.html +274 -0
  63. data/spec/rails_root/public/javascripts/application.js +2 -0
  64. data/spec/rails_root/public/javascripts/controls.js +963 -0
  65. data/spec/rails_root/public/javascripts/dragdrop.js +972 -0
  66. data/spec/rails_root/public/javascripts/effects.js +1120 -0
  67. data/spec/rails_root/public/javascripts/prototype.js +4225 -0
  68. data/spec/rails_root/public/robots.txt +5 -0
  69. data/spec/rails_root/script/about +3 -0
  70. data/spec/rails_root/script/bj +679 -0
  71. data/spec/rails_root/script/console +3 -0
  72. data/spec/rails_root/script/dbconsole +3 -0
  73. data/spec/rails_root/script/destroy +3 -0
  74. data/spec/rails_root/script/generate +3 -0
  75. data/spec/rails_root/script/performance/benchmarker +3 -0
  76. data/spec/rails_root/script/performance/profiler +3 -0
  77. data/spec/rails_root/script/performance/request +3 -0
  78. data/spec/rails_root/script/plugin +3 -0
  79. data/spec/rails_root/script/process/inspector +3 -0
  80. data/spec/rails_root/script/process/reaper +3 -0
  81. data/spec/rails_root/script/process/spawner +3 -0
  82. data/spec/rails_root/script/runner +3 -0
  83. data/spec/rails_root/script/server +3 -0
  84. data/spec/rails_root/test/test_helper.rb +38 -0
  85. data/todo.yml +23 -0
  86. metadata +184 -0
@@ -0,0 +1,86 @@
1
+ class Hash
2
+ begin
3
+ method "to_options"
4
+ rescue
5
+ def to_options
6
+ inject(Hash.new){|h, kv| h.update kv.first.to_s.to_sym => kv.last}
7
+ end
8
+ def to_options!
9
+ replace to_options
10
+ end
11
+ end
12
+
13
+ begin
14
+ method "to_string_options"
15
+ rescue
16
+ def to_string_options
17
+ inject(Hash.new){|h, kv| h.update kv.first.to_s => kv.last}
18
+ end
19
+ def to_string_options!
20
+ replace to_string_options
21
+ end
22
+ end
23
+
24
+ begin
25
+ method "reverse_merge"
26
+ rescue
27
+ def reverse_merge other
28
+ other.merge self
29
+ end
30
+ def reverse_merge! other
31
+ replace reverse_merge(other)
32
+ end
33
+ end
34
+
35
+ begin
36
+ method "slice"
37
+ rescue
38
+ def slice(*keys)
39
+ allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
40
+ reject { |key,| !allowed.include?(key) }
41
+ end
42
+ end
43
+
44
+ begin
45
+ method "slice!"
46
+ rescue
47
+ def slice!(*keys)
48
+ replace(slice(*keys))
49
+ end
50
+ end
51
+ end
52
+
53
+ class Object
54
+ begin
55
+ method "returning"
56
+ rescue
57
+ def returning value, &block
58
+ block.call value
59
+ value
60
+ end
61
+ end
62
+ end
63
+
64
+ class Object
65
+ def singleton_class &block
66
+ @singleton_class ||=
67
+ class << self
68
+ self
69
+ end
70
+ block ? @singleton_class.module_eval(&block) : @singleton_class
71
+ end
72
+ end
73
+
74
+ class String
75
+ begin
76
+ method 'underscore'
77
+ rescue
78
+ def underscore
79
+ gsub(/::/, '/').
80
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
81
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
82
+ tr("-", "_").
83
+ downcase
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,426 @@
1
+ class Bj
2
+ #
3
+ # table base class
4
+ #
5
+ class Table < ActiveRecord::Base
6
+ module ClassMethods
7
+ fattr("list"){ Array.new }
8
+ fattr("migration"){}
9
+
10
+ def migration_code classname = "BjMigration"
11
+ <<-code
12
+ class #{ classname } < ActiveRecord::Migration
13
+ def self.up
14
+ Bj::Table.each{|table| table.up}
15
+ end
16
+ def self.down
17
+ Bj::Table.reverse_each{|table| table.down}
18
+ end
19
+ end
20
+ code
21
+ end
22
+
23
+ def up
24
+ migration_class.up
25
+ end
26
+
27
+ def down
28
+ migration_class.down
29
+ end
30
+
31
+ def migration_class
32
+ table = self
33
+ @migration_class ||=
34
+ Class.new(ActiveRecord::Migration) do
35
+ sc =
36
+ class << self
37
+ self
38
+ end
39
+ sc.module_eval{ fattr :table => table }
40
+ sc.module_eval &table.migration
41
+ end
42
+ end
43
+
44
+ def content_column_names
45
+ @content_column_names = content_columns.map{|column| column.name}
46
+ end
47
+
48
+ def create_hash_for options
49
+ options.to_options!
50
+ hash = {}
51
+ content_column_names.each do |key|
52
+ key = key.to_s.to_sym
53
+ hash[key] = options[key]
54
+ end
55
+ hash
56
+ end
57
+
58
+ def each *a, &b
59
+ list.each *a, &b
60
+ end
61
+
62
+ def reverse_each *a, &b
63
+ list.reverse.each *a, &b
64
+ end
65
+ end
66
+ send :extend, ClassMethods
67
+
68
+ module InstanceMethods
69
+ def to_hash
70
+ oh = OrderedHash.new
71
+ self.class.content_column_names.each{|c| oh[c] = self[c]}
72
+ oh
73
+ end
74
+ end
75
+ send :include, InstanceMethods
76
+
77
+ module RecursivelyInherited
78
+ def inherited other
79
+ super
80
+ ensure
81
+ (Table.list << other).uniq!
82
+ basename = other.name.split(%r/::/).last.underscore
83
+ Table.singleton_class{ fattr basename => other }
84
+ other.send :extend, RecursivelyInherited
85
+ end
86
+ end
87
+ send :extend, RecursivelyInherited
88
+
89
+ #
90
+ # table classes
91
+ #
92
+ class Job < Table
93
+ set_table_name "bj_job"
94
+ set_primary_key "#{ table_name }_id"
95
+
96
+ migration {
97
+ define_method :up do
98
+ create_table table.table_name, :primary_key => table.primary_key, :force => true do |t|
99
+ t.column "command" , :text
100
+
101
+ t.column "state" , :text
102
+ t.column "priority" , :integer
103
+ t.column "tag" , :text
104
+ t.column "is_restartable" , :integer
105
+
106
+ t.column "submitter" , :text
107
+ t.column "runner" , :text
108
+ t.column "pid" , :integer
109
+
110
+ t.column "submitted_at" , :datetime
111
+ t.column "started_at" , :datetime
112
+ t.column "finished_at" , :datetime
113
+
114
+ t.column "env" , :text
115
+ t.column "stdin" , :text
116
+ t.column "stdout" , :text
117
+ t.column "stderr" , :text
118
+ t.column "exit_status" , :integer
119
+ end
120
+ end
121
+
122
+ define_method :down do
123
+ drop_table table.table_name
124
+ end
125
+ }
126
+
127
+ module ClassMethods
128
+ def submit jobs, options = {}, &block
129
+ jobs = Joblist.for jobs, options
130
+ returned = []
131
+ transaction do
132
+ jobs.each do |job|
133
+ job = create_hash_for(job.reverse_merge(submit_defaults))
134
+ job = create! job
135
+ returned << (block ? block.call(job) : job)
136
+ end
137
+ end
138
+ returned
139
+ end
140
+
141
+ def submit_defaults
142
+ {
143
+ :state => "pending",
144
+ :priority => 0,
145
+ :tag => "",
146
+ :is_restartable => true,
147
+ :submitter => Bj.hostname,
148
+ :submitted_at => Time.now.utc,
149
+ }
150
+ end
151
+ end
152
+ send :extend, ClassMethods
153
+
154
+ module InstanceMethods
155
+ def title
156
+ "job[#{ id }](#{ command })"
157
+ end
158
+
159
+ def finished
160
+ reload
161
+ exit_status
162
+ end
163
+ alias_method "finished?", "finished"
164
+
165
+ def run! options = {}
166
+ job = self
167
+ thread = nil
168
+
169
+ command = job.command
170
+ env = job.env ? YAML.load(job.env) : {}
171
+ stdin = job.stdin || ''
172
+ stdout = job.stdout || ''
173
+ stderr = job.stderr || ''
174
+ started_at = Time.now.utc
175
+
176
+ job.state = "starting"
177
+ job.save!
178
+
179
+ thread = Util.start command, :cwd=>Bj.rails_root, :env=>env, :stdin=>stdin, :stdout=>stdout, :stderr=>stderr
180
+
181
+ job.state = "running"
182
+ job.runner = Bj.hostname
183
+ job.pid = thread.pid
184
+ job.started_at = started_at
185
+
186
+ Bj.transaction(options) do
187
+ job.save!
188
+ job.reload
189
+ end
190
+
191
+ exit_status = thread.value
192
+
193
+ job.state = "finished"
194
+ job.finished_at = Time.now.utc
195
+ job.stdout = stdout
196
+ job.stderr = stderr
197
+ job.exit_status = exit_status
198
+
199
+ Bj.transaction(options) do
200
+ job.save!
201
+ job.reload
202
+ end
203
+
204
+ job
205
+ end
206
+ end
207
+ send :include, InstanceMethods
208
+ end
209
+
210
+ class JobArchive < Job
211
+ set_table_name "bj_job_archive"
212
+ set_primary_key "#{ table_name }_id"
213
+
214
+ migration {
215
+ define_method(:up) do
216
+ create_table table.table_name, :primary_key => table.primary_key, :force => true do |t|
217
+ t.column "command" , :text
218
+
219
+ t.column "state" , :text
220
+ t.column "priority" , :integer
221
+ t.column "tag" , :text
222
+ t.column "is_restartable" , :integer
223
+
224
+ t.column "submitter" , :text
225
+ t.column "runner" , :text
226
+ t.column "pid" , :integer
227
+
228
+ t.column "submitted_at" , :datetime
229
+ t.column "started_at" , :datetime
230
+ t.column "finished_at" , :datetime
231
+ t.column "archived_at" , :datetime
232
+
233
+ t.column "env" , :text
234
+ t.column "stdin" , :text
235
+ t.column "stdout" , :text
236
+ t.column "stderr" , :text
237
+ t.column "exit_status" , :integer
238
+ end
239
+ end
240
+
241
+ define_method(:down) do
242
+ drop_table table.table_name
243
+ end
244
+ }
245
+ end
246
+
247
+ class Config < Table
248
+ set_table_name "bj_config"
249
+ set_primary_key "#{ table_name }_id"
250
+
251
+ migration {
252
+ define_method(:up) do
253
+ create_table table.table_name, :primary_key => table.primary_key, :force => true do |t|
254
+ t.column "hostname" , :text
255
+ t.column "key" , :text
256
+ t.column "value" , :text
257
+ t.column "cast" , :text
258
+ end
259
+
260
+ begin
261
+ add_index table.table_name, %w[ hostname key ], :unique => true
262
+ rescue Exception
263
+ STDERR.puts "WARNING: your database is sucking and does not support unique indexes on text fields!?"
264
+ end
265
+ end
266
+
267
+ define_method(:down) do
268
+ begin
269
+ remove_index table.table_name, :column => %w[ hostname key ]
270
+ rescue Exception
271
+ end
272
+ drop_table table.table_name
273
+ end
274
+ }
275
+
276
+ module ClassMethods
277
+ def [] key
278
+ get key
279
+ end
280
+
281
+ def get key, options = {}
282
+ transaction do
283
+ options.to_options!
284
+ hostname = options[:hostname] || Bj.hostname
285
+ record = find :first, :conditions => conditions(:key => key, :hostname => hostname)
286
+ record ? record.value : default_for(key)
287
+ end
288
+ end
289
+
290
+ def conditions options = {}
291
+ options.to_options!
292
+ options.reverse_merge!(
293
+ :hostname => Bj.hostname
294
+ )
295
+ options
296
+ end
297
+
298
+ def default_for key
299
+ record = find :first, :conditions => conditions(:key => key, :hostname => '*')
300
+ record ? record.value : nil
301
+ end
302
+
303
+ def []= key, value
304
+ set key, value
305
+ end
306
+
307
+ def set key, value, options = {}
308
+ transaction do
309
+ options.to_options!
310
+ hostname = options[:hostname] || Bj.hostname
311
+ record = find :first, :conditions => conditions(:key => key, :hostname => hostname), :lock => true
312
+ cast = options[:cast] || cast_for(value)
313
+ key = key.to_s
314
+ value = value.to_s
315
+ if record
316
+ record["value"] = value
317
+ record["cast"] = cast
318
+ record.save!
319
+ else
320
+ create! :hostname => hostname, :key => key, :value => value, :cast => cast
321
+ end
322
+ value
323
+ end
324
+ end
325
+
326
+ def delete key
327
+ transaction do
328
+ record = find :first, :conditions => conditions(:key => key), :lock => true
329
+ if record
330
+ record.destroy
331
+ record
332
+ else
333
+ nil
334
+ end
335
+ end
336
+ end
337
+
338
+ def has_key? key
339
+ record = find :first, :conditions => conditions(:key => key)
340
+ record ? record : false
341
+ end
342
+ alias_method "has_key", "has_key?"
343
+
344
+ def keys
345
+ find(:all, :conditions => conditions).map(&:key)
346
+ end
347
+
348
+ def values
349
+ find(:all, :conditions => conditions).map(&:value)
350
+ end
351
+
352
+ def for options = {}
353
+ oh = OrderedHash.new
354
+ find(:all, :conditions => conditions(options)).each do |record|
355
+ oh[record.key] = record.value
356
+ end
357
+ oh
358
+ end
359
+
360
+ def cast_for value
361
+ case value
362
+ when TrueClass, FalseClass
363
+ 'to_bool'
364
+ when NilClass
365
+ 'to_nil'
366
+ when Fixnum, Bignum
367
+ 'to_i'
368
+ when Float
369
+ 'to_f'
370
+ when Time
371
+ 'to_time'
372
+ when Symbol
373
+ 'to_sym'
374
+ else
375
+ case value.to_s
376
+ when %r/^\d+$/
377
+ 'to_i'
378
+ when %r/^\d+\.\d+$/
379
+ 'to_f'
380
+ when %r/^nil$|^$/
381
+ 'to_nil'
382
+ when %r/^true|false$/
383
+ 'to_bool'
384
+ else
385
+ 'to_s'
386
+ end
387
+ end
388
+ end
389
+
390
+ def casts
391
+ @casts ||= {
392
+ 'to_bool' => lambda do |value|
393
+ value.to_s =~ %r/^true$/i ? true : false
394
+ end,
395
+ 'to_i' => lambda do |value|
396
+ Integer value.to_s.gsub(%r/^(-)?0*/,'\1')
397
+ end,
398
+ 'to_f' => lambda do |value|
399
+ Float value.to_s.gsub(%r/^0*/,'')
400
+ end,
401
+ 'to_time' => lambda do |value|
402
+ Time.parse(value.to_s)
403
+ end,
404
+ 'to_sym' => lambda do |value|
405
+ value.to_s.to_sym
406
+ end,
407
+ 'to_nil' => lambda do |value|
408
+ value.to_s =~ %r/^nil$|^$/i ? nil : value.to_s
409
+ end,
410
+ 'to_s' => lambda do |value|
411
+ value.to_s
412
+ end,
413
+ }
414
+ end
415
+ end
416
+ send :extend, ClassMethods
417
+
418
+ module InstanceMethods
419
+ def value
420
+ self.class.casts[cast][self["value"]]
421
+ end
422
+ end
423
+ send :include, InstanceMethods
424
+ end
425
+ end
426
+ end