buildbox 0.0.3 → 0.0.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: eb5d520609a7709bb39f3101371b189843a1529f
4
- data.tar.gz: 17c6f844295770f5db6e0ed00b8d96d9780ebbe6
3
+ metadata.gz: 5ae40618215e45c8217f4089029f72106f0b3201
4
+ data.tar.gz: cba478aafb75089f7796eb58b36ebbe7058533b9
5
5
  SHA512:
6
- metadata.gz: 71082a07bf307f1a53d1e294f2a84ebcbb0882a9194a289a35ee13b9ca1ebc69efd04f38428e63f5498008008346725e634b124a228b10f9602beae602814772
7
- data.tar.gz: 0e2ad6bca3ac9228d49d3e09d5e012644839281bef5d4c18980e484156ce42aa73a8194f437e088cccffa8542d5dd71e15b853b7b489ce81610dd468cdbb91b9
6
+ metadata.gz: 2f59b811b325337a6a3b0686358915e643d3dc2fe5d6ef5808ef946bfbfdb0f054d8145a0cfbf9b09ba4ea5d7de071c613c84ed5c2a99379f948814c8d87cf09
7
+ data.tar.gz: 7553cc4351956d502101f57a0827f02ab33bc346b6c89614fc003f4d7223a5bf3deaf5cd78f0e268526acc159e013e9adfeccb3df36a9a251384ce532cd056e8
@@ -5,11 +5,12 @@ require "buildbox/build"
5
5
  require "buildbox/version"
6
6
  require "buildbox/client"
7
7
  require "buildbox/api"
8
- require "buildbox/queue"
8
+ require "buildbox/worker"
9
9
  require "buildbox/pid_file"
10
10
  require "buildbox/configuration"
11
11
  require "buildbox/auth"
12
12
  require "buildbox/response"
13
+ require "buildbox/observer"
13
14
 
14
15
  module Buildbox
15
16
  require 'fileutils'
@@ -9,8 +9,8 @@ module Buildbox
9
9
  @config = options[:config]
10
10
  end
11
11
 
12
- def start(&block)
13
- @block = block
12
+ def start(observer = nil)
13
+ @observer = observer
14
14
 
15
15
  unless build_path.exist?
16
16
  setup_build_path
@@ -52,13 +52,19 @@ module Buildbox
52
52
  end
53
53
 
54
54
  def run(command)
55
- path = build_path if build_path.exist?
55
+ path = build_path if build_path.exist?
56
+ started = false
56
57
 
57
58
  result = Buildbox::Command.new(path).run(command) do |result, chunk|
58
- @block.call(result)
59
+ if started
60
+ @observer.chunk(result)
61
+ else
62
+ @observer.started(result)
63
+ started = true
64
+ end
59
65
  end
60
66
 
61
- @block.call(result)
67
+ @observer.finished(result)
62
68
 
63
69
  result
64
70
  end
@@ -18,7 +18,7 @@ module Buildbox
18
18
 
19
19
  loop do
20
20
  reload_configuration
21
- Buildbox::Queue.new.process
21
+ Buildbox::Worker.new.process
22
22
  wait_for_interval
23
23
  end
24
24
  rescue => e
@@ -15,6 +15,11 @@ module Buildbox
15
15
  read_io, write_io, pid = nil
16
16
  result = Buildbox::Result.new(command)
17
17
 
18
+ # hack: this is so the observer class can raise a started event.
19
+ # instead of having a block passed to this command, we should implement
20
+ # a proper command observer
21
+ yield result
22
+
18
23
  begin
19
24
  dir = File.expand_path(@path)
20
25
 
@@ -0,0 +1,59 @@
1
+ module Buildbox
2
+ class Observer
3
+ INTERVAL = 3 # feels like a good number
4
+
5
+ def initialize(api, build_uuid)
6
+ @api = api
7
+ @build_uuid = build_uuid
8
+
9
+ @queue = Queue.new
10
+ @results = {}
11
+ @threads = {}
12
+ end
13
+
14
+ def started(result)
15
+ @results[result.uuid] = result
16
+
17
+ update(result.uuid)
18
+ end
19
+
20
+ def chunk(result)
21
+ update_on_interval(result.uuid)
22
+ end
23
+
24
+ def finished(result)
25
+ # kill off the interval updater
26
+ thread = @threads[result.uuid]
27
+ thread.kill if thread && thread.alive?
28
+
29
+ update(result.uuid)
30
+ end
31
+
32
+ private
33
+
34
+ # every INTERVAL seconds, read from the result and update the server with the output.
35
+ # if we don't do updates every INTERVAL like this, then we end up spamming the server
36
+ # with every chunk we get from the command runner (which ends up being every line), and in the case
37
+ # of a bundle install for example, it may have 100 lines, we end up doing 100 updates to the server.
38
+ def update_on_interval(uuid)
39
+ return if @threads[uuid]
40
+
41
+ @threads[uuid] = Thread.new do
42
+ loop do
43
+ update(uuid)
44
+ sleep INTERVAL
45
+ end
46
+ end
47
+
48
+ @threads[uuid].abort_on_exception = true
49
+ end
50
+
51
+ def update(uuid)
52
+ result = @results[uuid]
53
+ json = result.as_json
54
+ json[:state] = json.delete(:finished) ? 'finished' : 'started'
55
+
56
+ @api.update_build_result_async(@build_uuid, json.delete(:uuid), json)
57
+ end
58
+ end
59
+ end
@@ -1,3 +1,3 @@
1
1
  module Buildbox
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  module Buildbox
2
- class Queue
2
+ class Worker
3
3
  def process
4
4
  if scheduled = api.builds.payload.first
5
5
  start Build.new(scheduled)
@@ -10,14 +10,7 @@ module Buildbox
10
10
 
11
11
  def start(build)
12
12
  api.update_build_state(build.uuid, 'started')
13
-
14
- build.start do |result|
15
- json = result.as_json
16
- json[:state] = json.delete(:finished) ? 'finished' : 'started'
17
-
18
- api.update_build_result_async(build.uuid, json.delete(:uuid), json)
19
- end
20
-
13
+ build.start Buildbox::Observer.new(api, build.uuid)
21
14
  api.update_build_state_async(build.uuid, 'finished')
22
15
  end
23
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buildbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Pitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-14 00:00:00.000000000 Z
11
+ date: 2013-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,12 +90,13 @@ files:
90
90
  - lib/buildbox/client.rb
91
91
  - lib/buildbox/command.rb
92
92
  - lib/buildbox/configuration.rb
93
+ - lib/buildbox/observer.rb
93
94
  - lib/buildbox/pid_file.rb
94
- - lib/buildbox/queue.rb
95
95
  - lib/buildbox/response.rb
96
96
  - lib/buildbox/result.rb
97
97
  - lib/buildbox/utf8.rb
98
98
  - lib/buildbox/version.rb
99
+ - lib/buildbox/worker.rb
99
100
  - spec/buildbox/buildbox/build_spec.rb
100
101
  - spec/buildbox/buildbox/command_spec.rb
101
102
  - spec/buildbox/buildbox/configuration_spec.rb