tiny_work_service 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_work_service.rb +53 -1
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 327188d9aeb076b114aa5915bda2a4a50516b070152df17f8242f9fe09548d67
4
- data.tar.gz: fe76da5717bea63e4ab09ff69663ba8209984024fbf50af412b20d96d99f61bf
3
+ metadata.gz: 64ea8638eba6ebc22945d5c80631513c30d8866519b973e04d66a6a247e408c0
4
+ data.tar.gz: '099b5c7719d958f0674f1449d8da62d9de10fe66720b28aa0078c78f0c72bbf0'
5
5
  SHA512:
6
- metadata.gz: 7ca1d978cb9aa4a88f61970e3ea675cd58da4a55c20e0fb3121b73d40e15eef84badd27d3ba13b33f06d81e09ab5780e78448c83a58dcfcbfdfe65db9e618774
7
- data.tar.gz: ca2f7f1300eb9093ace4657fefa6beba346f4fb9f9284398f4f84ccb7f26324f25b8e6ccd032097d018887511d598b73eaae8dab2015c5441c6c1ec48a5ba6d2
6
+ metadata.gz: a68228cd855eeeb8943a20b87a12cb0b62f1bb6b9737251376a388eff7e08e5fa783fd2d0f5150eebb8d5c7237349d8abfe82568d59664c0a7140030b86cce15
7
+ data.tar.gz: b0a7f962ccac5be3a45c0869bae4432336547a411669b8cf3950751d31a67ac841c47adf13e86def1b046686b188f108e4cb79c3a9c8f7296d5dd8aeeb05bab0
@@ -4,20 +4,66 @@ require 'tiny_tcp_service'
4
4
  # s = TinyWorkService.new(1234)
5
5
  # s.stop!
6
6
  class TinyWorkService
7
+ attr_reader :jobs_enqueued,
8
+ :jobs_dequeued,
9
+ :jobs_per_minute,
10
+ :jobs_per_hour
11
+
7
12
  def initialize(port, label='TinyWorkService')
8
13
  @service = TinyTCPService.new(port)
9
14
  @service.msg_handler = self
10
15
  @jobs = Queue.new
11
16
  @label = label
12
17
 
18
+ @jobs_enqueued = 0
19
+ @jobs_dequeued = 0
20
+ @jobs_to_track = Queue.new
21
+ @jobs_dequeued_tracker = []
22
+
23
+ @jobs_per_minute = 0.0
24
+ @jobs_per_hour = 0.0
25
+
26
+ # status printing thread
13
27
  Thread.new do
14
28
  loop do
15
29
  break unless @service.running?
16
30
 
17
- print "\r#{@label}:#{port} #{@jobs.length.to_s.rjust(6)} jobs #{@service.num_clients.to_s.rjust(4)} workers\e[K"
31
+ print "\r#{@label}:#{port} jobs:#{@jobs.length.to_s} workers:#{@service.num_clients.to_s} jobs/m:#{@jobs_per_minute} jobs/h:#{@jobs_per_hour}\e[K"
18
32
  sleep 0.5
19
33
  end
20
34
  end
35
+
36
+ # update stats thread
37
+ Thread.new do
38
+ loop do
39
+ one_minute_ago = Time.now.to_i - 60
40
+ one_hour_ago = Time.now.to_i - 3600
41
+
42
+ # move jobs_to_track into jobs_dequeued_tracker, threadsafe
43
+ loop do
44
+ break if @jobs_to_track.length == 0
45
+ @jobs_dequeued_tracker << @jobs_to_track.shift
46
+ end
47
+
48
+ # remove job tracking times from older than one_hour_ago
49
+ loop do
50
+ break if @jobs_dequeued_tracker.empty? || @jobs_dequeued_tracker.first >= one_hour_ago
51
+ @jobs_dequeued_tracker.shift
52
+ end
53
+
54
+ counter = 0
55
+ i = -1
56
+ loop do
57
+ break if i.abs > @jobs_dequeued_tracker.length || @jobs_dequeued_tracker[i] < one_minute_ago
58
+ i -= 1
59
+ counter += 1
60
+ end
61
+ @jobs_per_minute = counter
62
+ @jobs_per_hour = @jobs_dequeued_tracker.count
63
+
64
+ sleep 2
65
+ end
66
+ end
21
67
  end
22
68
 
23
69
  # interface for TinyTCPService
@@ -42,13 +88,19 @@ class TinyWorkService
42
88
 
43
89
  # enqueue a job
44
90
  def <<(j)
91
+ @jobs_enqueued += 1
45
92
  @jobs << j
93
+
94
+ nil
46
95
  end
47
96
 
48
97
  # return the first job in the work queue, if there is one present
49
98
  # otherwise, return nil
50
99
  def shift
51
100
  return nil if @jobs.empty?
101
+ @jobs_dequeued += 1
102
+ @jobs_to_track << Time.now.to_i
103
+
52
104
  @jobs.shift
53
105
  end
54
106
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_work_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-05 00:00:00.000000000 Z
11
+ date: 2023-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiny_tcp_service