stagnum 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +24 -0
- data/README.md +51 -0
- data/lib/stagnum.rb +113 -0
- data/stagnum.gemspec +46 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8d935b2dcef0e06bf324569727b0e0416444d733d20da884426bd28d5d76ea18
|
4
|
+
data.tar.gz: f00a9639d70ba8c6fbd3da0e0877e4c8bbff5ae8968a1691d5312644f230e4b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '08c53834df95715abd1b77ab151ff49d29b9913db15cb81903c18174e67e0a2d4b33fecd45697efdfb4574bb732b0cb840b2d438ac9def2bae70e448b41eeea9'
|
7
|
+
data.tar.gz: ce0f53b3c4650e5aa3d2e1880a4fe31a49162949881f1fa755c70e7b9aed8979669e3044027aff6297515c19f33139a6c7b2cc7ebaa055898226b80e983c12ce
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
Copyright (c) 2025-2025, John Mettraux, jmettraux+flor@gmail.com
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
6
|
+
in the Software without restriction, including without limitation the rights
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
9
|
+
furnished to do so, subject to the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
12
|
+
all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
20
|
+
THE SOFTWARE.
|
21
|
+
|
22
|
+
|
23
|
+
Made in Japan
|
24
|
+
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
|
2
|
+
# stagnum
|
3
|
+
|
4
|
+
A stupid Ruby worker pool based on `Thread` and `Thread::Queue`.
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
pool = Stagnum::Pool.new('pool-zero', 4)
|
8
|
+
#
|
9
|
+
# 4 worker threads to service the enqueued jobs
|
10
|
+
|
11
|
+
q = Stagnum::DoneQueue.new
|
12
|
+
#
|
13
|
+
# a queue shared by a group of jobs to enqueue
|
14
|
+
|
15
|
+
30.times do |i|
|
16
|
+
|
17
|
+
pool.enqueue(q, { i: i }) do |d|
|
18
|
+
|
19
|
+
sleep rand * 1
|
20
|
+
|
21
|
+
d[:tname] = Thread.current.name
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
successes, failures = q.pop_all
|
26
|
+
#
|
27
|
+
# Stagnum::DoneQueue extends Thread::Queue
|
28
|
+
# but has a #pop_all convenience method
|
29
|
+
|
30
|
+
pp successes
|
31
|
+
#
|
32
|
+
# ==>
|
33
|
+
#
|
34
|
+
# [[:success, {i: 0, tname: "pool-zero__0"}],
|
35
|
+
# [:success, {i: 3, tname: "pool-zero__3"}],
|
36
|
+
# [:success, {i: 1, tname: "pool-zero__1"}],
|
37
|
+
# [:success, {i: 2, tname: "pool-zero__2"}],
|
38
|
+
# [:success, {i: 7, tname: "pool-zero__2"}],
|
39
|
+
# [:success, {i: 5, tname: "pool-zero__3"}],
|
40
|
+
# [:success, {i: 9, tname: "pool-zero__3"}],
|
41
|
+
# [:success, {i: 6, tname: "pool-zero__1"}],
|
42
|
+
# [:success, {i: 4, tname: "pool-zero__0"}],
|
43
|
+
# [:success, {i: 8, tname: "pool-zero__2"}]]
|
44
|
+
```
|
45
|
+
|
46
|
+
One can use `Thread::Queue` instead of `Stagnum::DoneQueue`
|
47
|
+
|
48
|
+
## LICENSE
|
49
|
+
|
50
|
+
MIT, see [LICENSE.txt](LICENSE.txt)
|
51
|
+
|
data/lib/stagnum.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'thread'
|
4
|
+
|
5
|
+
|
6
|
+
module Stagnum
|
7
|
+
|
8
|
+
VERSION = '0.9.0'
|
9
|
+
|
10
|
+
|
11
|
+
class Pool
|
12
|
+
|
13
|
+
attr_reader :name
|
14
|
+
|
15
|
+
def initialize(name, worker_count)
|
16
|
+
|
17
|
+
@name = name
|
18
|
+
@work_queue = ::Thread::Queue.new
|
19
|
+
@worker_count = worker_count
|
20
|
+
|
21
|
+
@maintenance_mutex = ::Thread::Mutex.new
|
22
|
+
@next_worker_thread_id = -1
|
23
|
+
@worker_threads = []
|
24
|
+
end
|
25
|
+
|
26
|
+
def enqueue(done_queue=Stagnum::DoneQueue.new, data, &block)
|
27
|
+
|
28
|
+
maintain
|
29
|
+
|
30
|
+
done_queue.increment if done_queue.respond_to?(:increment)
|
31
|
+
|
32
|
+
@work_queue << [ done_queue, data, block ]
|
33
|
+
|
34
|
+
done_queue
|
35
|
+
end
|
36
|
+
|
37
|
+
def next_worker_thread_id
|
38
|
+
|
39
|
+
@next_worker_thread_id += 1
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
def maintain
|
45
|
+
|
46
|
+
@maintenance_mutex.synchronize do
|
47
|
+
|
48
|
+
@worker_threads = @worker_threads.select { |t| t.alive? }
|
49
|
+
|
50
|
+
while @worker_threads.size < @worker_count
|
51
|
+
|
52
|
+
@worker_threads << Stagnum::WorkerThread.new(self, @work_queue)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class DoneQueue < ::Thread::Queue
|
59
|
+
|
60
|
+
attr_reader :count
|
61
|
+
|
62
|
+
def initialize(items=[])
|
63
|
+
|
64
|
+
super
|
65
|
+
|
66
|
+
@count = 0
|
67
|
+
end
|
68
|
+
|
69
|
+
def increment
|
70
|
+
|
71
|
+
@count += 1
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns [ successes, failures ]
|
75
|
+
#
|
76
|
+
def pop_all
|
77
|
+
|
78
|
+
@count.times
|
79
|
+
.inject([ [], [] ]) { |a, i|
|
80
|
+
r = self.pop
|
81
|
+
a[r[0] == :success ? 0 : 1] << r
|
82
|
+
a }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class WorkerThread < ::Thread
|
87
|
+
|
88
|
+
def initialize(stagnum, work_queue)
|
89
|
+
|
90
|
+
@stagnum = stagnum
|
91
|
+
@work_queue = work_queue
|
92
|
+
|
93
|
+
self.name = "#{stagnum.name}__#{stagnum.next_worker_thread_id}"
|
94
|
+
|
95
|
+
super do
|
96
|
+
|
97
|
+
loop do
|
98
|
+
|
99
|
+
done_queue, data, block = @work_queue.pop
|
100
|
+
|
101
|
+
block.call(data)
|
102
|
+
|
103
|
+
done_queue << [ :success, data ]
|
104
|
+
|
105
|
+
rescue => err
|
106
|
+
|
107
|
+
done_queue << [ :failure, data, err ]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
data/stagnum.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
|
4
|
+
s.name = 'stagnum'
|
5
|
+
|
6
|
+
s.version = File.read(
|
7
|
+
File.expand_path('../lib/stagnum.rb', __FILE__)
|
8
|
+
).match(/ VERSION *= *['"]([^'"]+)/)[1]
|
9
|
+
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = [ 'John Mettraux' ]
|
12
|
+
s.email = [ 'jmettraux+flor@gmail.com' ]
|
13
|
+
s.homepage = 'https://github.com/floraison/stagnum'
|
14
|
+
s.license = 'MIT'
|
15
|
+
s.summary = 'a worker pool'
|
16
|
+
|
17
|
+
s.description = %{
|
18
|
+
A stupid worker pool
|
19
|
+
}.strip
|
20
|
+
|
21
|
+
s.metadata = {
|
22
|
+
'changelog_uri' => s.homepage + '/blob/master/CHANGELOG.md',
|
23
|
+
'bug_tracker_uri' => s.homepage + '/issues',
|
24
|
+
'homepage_uri' => s.homepage,
|
25
|
+
'documentation_uri' => s.homepage,
|
26
|
+
'source_code_uri' => s.homepage,
|
27
|
+
#'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/floraison',
|
28
|
+
#'wiki_uri' => s.homepage + '/wiki',
|
29
|
+
'rubygems_mfa_required' => 'true',
|
30
|
+
}
|
31
|
+
|
32
|
+
#s.files = `git ls-files`.split("\n")
|
33
|
+
s.files = Dir[
|
34
|
+
'{README,CHANGELOG,CREDITS,LICENSE}.{md,txt}',
|
35
|
+
#'Makefile',
|
36
|
+
'lib/**/*.rb', #'spec/**/*.rb', 'test/**/*.rb',
|
37
|
+
"#{s.name}.gemspec",
|
38
|
+
]
|
39
|
+
|
40
|
+
#s.add_runtime_dependency 'raabro', '~> 1.4'
|
41
|
+
|
42
|
+
s.add_development_dependency 'probatio', '~> 1.0'
|
43
|
+
|
44
|
+
s.require_path = 'lib'
|
45
|
+
end
|
46
|
+
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stagnum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Mettraux
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-03-04 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: probatio
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1.0'
|
19
|
+
type: :development
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '1.0'
|
26
|
+
description: A stupid worker pool
|
27
|
+
email:
|
28
|
+
- jmettraux+flor@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- LICENSE.txt
|
34
|
+
- README.md
|
35
|
+
- lib/stagnum.rb
|
36
|
+
- stagnum.gemspec
|
37
|
+
homepage: https://github.com/floraison/stagnum
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata:
|
41
|
+
changelog_uri: https://github.com/floraison/stagnum/blob/master/CHANGELOG.md
|
42
|
+
bug_tracker_uri: https://github.com/floraison/stagnum/issues
|
43
|
+
homepage_uri: https://github.com/floraison/stagnum
|
44
|
+
documentation_uri: https://github.com/floraison/stagnum
|
45
|
+
source_code_uri: https://github.com/floraison/stagnum
|
46
|
+
rubygems_mfa_required: 'true'
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.6.2
|
62
|
+
specification_version: 4
|
63
|
+
summary: a worker pool
|
64
|
+
test_files: []
|