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.
- checksums.yaml +4 -4
- data/lib/tiny_work_service.rb +53 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64ea8638eba6ebc22945d5c80631513c30d8866519b973e04d66a6a247e408c0
|
4
|
+
data.tar.gz: '099b5c7719d958f0674f1449d8da62d9de10fe66720b28aa0078c78f0c72bbf0'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a68228cd855eeeb8943a20b87a12cb0b62f1bb6b9737251376a388eff7e08e5fa783fd2d0f5150eebb8d5c7237349d8abfe82568d59664c0a7140030b86cce15
|
7
|
+
data.tar.gz: b0a7f962ccac5be3a45c0869bae4432336547a411669b8cf3950751d31a67ac841c47adf13e86def1b046686b188f108e4cb79c3a9c8f7296d5dd8aeeb05bab0
|
data/lib/tiny_work_service.rb
CHANGED
@@ -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}
|
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.
|
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-
|
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
|