hydroponic_bean 1.0.0 → 1.1.0

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: 9be5fc754d60f8829cbbd0ba05227eba6870223f
4
- data.tar.gz: 84c0e63df7d54790e15fe9c740a916d6abd09486
3
+ metadata.gz: 2f4b6d8a2f04c35eb3f1633af6644cd013a72ed8
4
+ data.tar.gz: 30490d78a6e123d415091462c65d8eee5f663d67
5
5
  SHA512:
6
- metadata.gz: 1fecd8d584eb0d85cf2e0398d5330aeff2fd418dabe8a1c218815b1d894b4d9e56748c1d716022cf61041b0f96e2b5ac4e1940e77cb16a6ccad4ca628c6723a5
7
- data.tar.gz: 4a11702f8cfe3255080f2597814693a132b6dc007b1bf93951a5de28b844faf2093f777ff896f4a935af2ab3b3fc8325b17cc3968f1a54b5f77f8d6e9188ea4c
6
+ metadata.gz: 374d7a1c86c90bb0c4e11300910ef30cae7ce3b9b1867ea61df180a086049b9813b1628ffe25645cf72f2ecf7fe4a695bb0d16e4fcc6c1508bd38987f7523fde
7
+ data.tar.gz: 65fbc11688e4fd1770890e6542c9d9f8d7c5b52ba8488b918518750ca6355fa57be1d2e2771d340dd3dfa174651e74bdb76554839555e0893dcb946b135855c9
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # 1.1.0
2
+
3
+ - Added support for a lot more commands, see lib/hydroponic_bean/commands/
4
+ - Changed internal code structure
5
+
6
+ # 1.0.0
7
+
8
+ - Initial release
9
+ - Support for only some very basic commands
@@ -1,15 +1,69 @@
1
1
  module HydroponicBean
2
2
  module Commands
3
3
  module Other
4
- def peek(stream, id)
5
- peek_output(HydroponicBean.jobs[id.to_i - 1])
4
+ def peek(stream, id = nil)
5
+ id = id.to_i
6
+ job = (id == 0) ? nil : HydroponicBean.jobs[id - 1]
7
+ peek_output(job)
8
+ end
9
+
10
+ def peek_ready(stream)
11
+ peek_output current_tube.ready_jobs.first
12
+ end
13
+
14
+ def peek_buried(stream)
15
+ peek_output current_tube.buried_jobs.first
16
+ end
17
+
18
+ def peek_delayed(stream)
19
+ peek_output current_tube.delayed_jobs.first
20
+ end
21
+
22
+ def stats_job(stream, id)
23
+ id = id.to_i
24
+ job = (id == 0) ? nil : HydroponicBean.jobs[id - 1]
25
+ if !job
26
+ output(Protocol::NOT_FOUND)
27
+ return false
28
+ end
29
+
30
+ stats = job.serialize_stats.to_yaml
31
+ output("OK #{stats.length}\r\n")
32
+ output("#{stats}\r\n")
33
+ end
34
+
35
+ def kick_job(stream, id)
36
+ id = id.to_i
37
+ job = (id == 0) ? nil : HydroponicBean.jobs[id - 1]
38
+ if !job || !job.kick
39
+ output(Protocol::NOT_FOUND)
40
+ return false
41
+ end
42
+ job.kick
43
+ output("KICKED\r\n")
44
+ end
45
+
46
+ def watch(stream, tube_name)
47
+ watched_tube_names << tube_name
48
+ watched_tube_names.uniq!
49
+ output("WATCHING #{watched_tube_names.count}\r\n")
50
+ end
51
+
52
+ def ignore(stream, tube_name)
53
+ watched_tube_names.delete(tube_name)
54
+ if watched_tube_names.empty?
55
+ watched_tube_names << tube_name
56
+ output("NOT_IGNORED\r\n")
57
+ else
58
+ output("WATCHING #{watched_tube_names.count}\r\n")
59
+ end
6
60
  end
7
61
 
8
62
  protected
9
63
  def peek_output(job)
10
64
  if job
11
- output("FOUND #{job[:id]} #{job[:data].length}\r\n")
12
- output("#{job[:data]}\r\n")
65
+ output("FOUND #{job.id} #{job.data.length}\r\n")
66
+ output("#{job.data}\r\n")
13
67
  else
14
68
  output(Protocol::NOT_FOUND)
15
69
  return false
@@ -0,0 +1,57 @@
1
+ module HydroponicBean
2
+ module Commands
3
+ module Tube
4
+ def delete(stream, id)
5
+ job = HydroponicBean.jobs[id.to_i - 1]
6
+ if job
7
+ job.delete
8
+ output("DELETED\r\n")
9
+ else
10
+ output(Protocol::NOT_FOUND)
11
+ return false
12
+ end
13
+ end
14
+
15
+ def list_tubes(stream)
16
+ tubes = HydroponicBean.tubes.keys.to_yaml
17
+ output("OK #{tubes.length}\r\n")
18
+ output("#{tubes}\r\n")
19
+ end
20
+
21
+ def list_tubes_watched(stream)
22
+ tubes = watched_tube_names.to_yaml
23
+ output("OK #{tubes.length}\r\n")
24
+ output("#{tubes}\r\n")
25
+ end
26
+
27
+ def stats_tube(stream, tube_name)
28
+ if HydroponicBean.tubes.has_key?(tube_name)
29
+ tube = HydroponicBean.tubes[tube_name]
30
+ data = tube.serialize_stats.to_yaml
31
+ output("OK #{data.length}\r\n")
32
+ output("#{data}\r\n")
33
+ else
34
+ output(Protocol::NOT_FOUND)
35
+ return false
36
+ end
37
+ end
38
+
39
+ def pause_tube(stream, tube_name, delay)
40
+ if HydroponicBean.tubes.has_key?(tube_name)
41
+ tube = HydroponicBean.tubes[tube_name]
42
+ tube.pause(delay)
43
+ output("PAUSED\r\n")
44
+ else
45
+ output(Protocol::NOT_FOUND)
46
+ return false
47
+ end
48
+ end
49
+
50
+ def kick(stream, bound)
51
+ bound = bound.to_i
52
+ tube = current_tube
53
+ output("KICKED #{tube.kick(bound)}\r\n")
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,11 +1,13 @@
1
1
  require 'hydroponic_bean/commands/producer'
2
2
  require 'hydroponic_bean/commands/worker'
3
+ require 'hydroponic_bean/commands/tube'
3
4
  require 'hydroponic_bean/commands/other'
4
5
 
5
6
  module HydroponicBean
6
7
  module Commands
7
8
  include HydroponicBean::Commands::Producer
8
9
  include HydroponicBean::Commands::Worker
10
+ include HydroponicBean::Commands::Tube
9
11
  include HydroponicBean::Commands::Other
10
12
  end
11
13
  end
@@ -1,6 +1,9 @@
1
+ require 'hydroponic_bean/tube'
2
+ require 'hydroponic_bean/job'
3
+
1
4
  module HydroponicBean
2
5
  def self.tubes
3
- @tubes ||= Hash.new{|h, k| h[k] = []}
6
+ @tubes ||= Hash.new{|h, k| h[k] = Tube.new(k)}
4
7
  end
5
8
 
6
9
  def self.jobs
@@ -16,25 +19,16 @@ module HydroponicBean
16
19
  HydroponicBean.tubes[current_tube_name]
17
20
  end
18
21
 
19
- def next_id
20
- HydroponicBean.jobs.length + 1
22
+ def watched_tube_names
23
+ @watched_tube_names ||= ['default']
21
24
  end
22
25
 
23
26
  def create_job(pri, delay, ttr, data)
24
- job = {
25
- id: next_id,
26
- tube: current_tube_name,
27
- pri: pri.to_i,
28
- delay: delay.to_i,
29
- ttr: ttr.to_i,
30
- data: data,
31
- }
32
- job[:state] = (job[:delay] == 0) ? :ready : :delayed
27
+ job = Job.new(current_tube, pri, delay, ttr, data)
33
28
 
34
29
  HydroponicBean.jobs.push(job)
35
- current_tube.push(job)
36
30
 
37
- return job[:id]
31
+ return job.id
38
32
  end
39
33
  end
40
34
  end
@@ -0,0 +1,87 @@
1
+ module HydroponicBean
2
+ class Job
3
+ def self.next_id
4
+ HydroponicBean.jobs.length + 1
5
+ end
6
+
7
+ attr_accessor :id, :pri, :delay, :ttr, :data, :created_at,
8
+ :state, :tube, :stats
9
+
10
+ attr_reader :deleted
11
+
12
+ def initialize(tube, pri, delay, ttr, data)
13
+ @id = self.class.next_id
14
+ @created_at = Time.now.utc
15
+ @tube = tube
16
+ @pri = pri.to_i
17
+ @delay = delay.to_i
18
+ @ttr = ttr.to_i
19
+ @state = @delay > 0 ? State.delayed : State.ready
20
+ @data = data
21
+ @deleted = false
22
+ @stats = {
23
+ 'reserves' => 0,
24
+ 'timeouts' => 0,
25
+ 'releases' => 0,
26
+ 'buries' => 0,
27
+ 'kicks' => 0,
28
+ }
29
+
30
+ @tube.push(self)
31
+ end
32
+
33
+ def age
34
+ (Time.now.utc - created_at).to_i
35
+ end
36
+
37
+ def tube_name
38
+ @tube.name
39
+ end
40
+
41
+ def urgent?; exists? && pri <= 1024; end
42
+ def ready?; exists? && state == State.ready; end
43
+ def reserved?; exists? && state == State.reserved; end
44
+ def delayed?; exists? && state == State.delayed; end
45
+ def buried?; exists? && state == State.buried; end
46
+
47
+ def delete
48
+ if @deleted == false
49
+ @tube.job_deleted
50
+ @deleted = true
51
+ end
52
+ end
53
+ def exists?; !deleted; end
54
+
55
+ def kick
56
+ if buried? || delayed?
57
+ stats['kicks'] += 1
58
+ @state = State.ready
59
+ end
60
+ end
61
+
62
+ def time_left
63
+ reserved? ? ttr : [delay - age, 0].max
64
+ end
65
+
66
+ def serialize_stats
67
+ {
68
+ 'id' => id,
69
+ 'tube' => tube.name,
70
+ 'state' => state.to_s,
71
+ 'pri' => pri,
72
+ 'age' => age,
73
+ 'delay' => delay,
74
+ 'ttr' => ttr,
75
+ 'time-left' => time_left,
76
+ 'file' => 0,
77
+ }.merge(stats)
78
+ end
79
+
80
+ module State
81
+ def self.ready; :ready; end
82
+ def self.reserved; :reserved; end
83
+ def self.delayed; :delayed; end
84
+ def self.buried; :buried; end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,73 @@
1
+ module HydroponicBean
2
+ class Tube
3
+ attr_reader :name, :jobs, :stats
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @jobs = []
8
+ @stats = {
9
+ 'cmd-delete' => 0,
10
+ 'cmd-pause-tube' => 0,
11
+ 'pause' => 0,
12
+ 'pause-time-left' => 0,
13
+ }
14
+ end
15
+
16
+ def push(job)
17
+ @jobs.push(job)
18
+ self
19
+ end
20
+
21
+ def current_jobs_urgent; jobs.select(&:urgent?).count; end
22
+ def current_jobs_ready; jobs.select(&:ready?).count; end
23
+ def current_jobs_reserved; jobs.select(&:reserved?).count; end
24
+ def current_jobs_delayed; jobs.select(&:delayed?).count; end
25
+ def current_jobs_buried; jobs.select(&:buried?).count; end
26
+
27
+ def serialize_stats
28
+ {
29
+ 'name' => name,
30
+ 'current-jobs-urgent' => current_jobs_urgent,
31
+ 'current-jobs-ready' => current_jobs_ready,
32
+ 'current-jobs-reserved' => current_jobs_reserved,
33
+ 'current-jobs-delayed' => current_jobs_delayed,
34
+ 'current-jobs-buried' => current_jobs_buried,
35
+ 'total-jobs' => jobs.count,
36
+ 'current-using' => 0,
37
+ 'current-waiting' => 0,
38
+ 'current-watching' => 0,
39
+ }.merge(stats)
40
+ end
41
+
42
+ def pause(delay)
43
+ delay = delay.to_i
44
+ stats['pause'] = delay
45
+ stats['pause-time-left'] = delay
46
+ stats['cmd-pause-tube'] += 1
47
+ end
48
+
49
+ def ready_jobs; jobs.select(&:ready?); end
50
+ def buried_jobs; jobs.select(&:buried?); end
51
+ def delayed_jobs; jobs.select(&:delayed?).sort_by(&:delay); end
52
+
53
+ def kick(bound)
54
+ initial_bound = bound
55
+ while bound > 0
56
+ if buried_jobs.count > 0
57
+ buried_jobs.first.kick
58
+ bound -= 1
59
+ elsif delayed_jobs.count > 0
60
+ delayed_jobs.first.kick
61
+ bound -= 1
62
+ else
63
+ return initial_bound - bound
64
+ end
65
+ end
66
+ return initial_bound
67
+ end
68
+
69
+ def job_deleted
70
+ stats['cmd-delete'] += 1
71
+ end
72
+ end
73
+ end
@@ -1,3 +1,3 @@
1
1
  module HydroponicBean
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hydroponic_bean
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoffroy Planquart
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-14 00:00:00.000000000 Z
11
+ date: 2017-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
77
  - ".travis.yml"
78
+ - CHANGELOG.md
78
79
  - CODE_OF_CONDUCT.md
79
80
  - Gemfile
80
81
  - LICENSE.txt
@@ -87,11 +88,14 @@ files:
87
88
  - lib/hydroponic_bean/commands.rb
88
89
  - lib/hydroponic_bean/commands/other.rb
89
90
  - lib/hydroponic_bean/commands/producer.rb
91
+ - lib/hydroponic_bean/commands/tube.rb
90
92
  - lib/hydroponic_bean/commands/worker.rb
91
93
  - lib/hydroponic_bean/connection.rb
92
94
  - lib/hydroponic_bean/data.rb
95
+ - lib/hydroponic_bean/job.rb
93
96
  - lib/hydroponic_bean/protocol.rb
94
97
  - lib/hydroponic_bean/test_helper.rb
98
+ - lib/hydroponic_bean/tube.rb
95
99
  - lib/hydroponic_bean/version.rb
96
100
  homepage: https://github.com/Aethelflaed/hydroponic_bean
97
101
  licenses: