coworker 0.1.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/CHANGELOG.md +3 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +8 -0
- data/TODO.md +9 -0
- data/examples/simple.rb +18 -0
- data/lib/coworker/supervisor.rb +212 -0
- data/lib/coworker/version.rb +5 -0
- data/lib/coworker/worker.rb +93 -0
- data/lib/coworker.rb +10 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8acd4eb9e15823c97f2485b46031de4e3ab49bb7b7fa558cbb3c735fad28d200
|
4
|
+
data.tar.gz: 70f71ef2a2c95278aadfa8ba33d6844a0374d21aa327cbcbaf027086d062c3b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e5fdc6981d7dd2690d0150cbee71e4f2ca6895219d66dce6c4ead4ec2929a35792402ae9a49370f8fea2db1fb52748a2cda26100dbaffc1fe3e62b4d263918d5
|
7
|
+
data.tar.gz: e788c80ec2090e67a964e55695a914bbe8506e97ad66740cf4ab75219d783042e0ac0b44b28b9529245754b1f17340b827537216ef6326ea5aa1fbfea6c44b25
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Sharon Rosner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Sharon Rosner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Coworker
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/coworker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
15
|
+
```
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/coworker.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/TODO.md
ADDED
data/examples/simple.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal
|
2
|
+
|
3
|
+
require 'bundler/inline'
|
4
|
+
|
5
|
+
gemfile do
|
6
|
+
source 'https://rubygems.org'
|
7
|
+
gem 'coworker', path: '..'
|
8
|
+
end
|
9
|
+
|
10
|
+
app = ->(w) do
|
11
|
+
loop do
|
12
|
+
puts "#{Time.now} pid: #{Process.pid} generation: #{w.generation}"
|
13
|
+
sleep 5
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
supervisor = Coworker::Supervisor.new(&app)
|
18
|
+
supervisor.run
|
@@ -0,0 +1,212 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './worker'
|
4
|
+
require 'child_subreaper'
|
5
|
+
|
6
|
+
module Coworker
|
7
|
+
CLUSTER_SIZE = 2
|
8
|
+
|
9
|
+
class Supervisor
|
10
|
+
def initialize(**opts, &app)
|
11
|
+
@opts = opts
|
12
|
+
@app = app
|
13
|
+
|
14
|
+
@leader_pid = nil
|
15
|
+
@workers = {}
|
16
|
+
@messages = Queue.new
|
17
|
+
@pipe_r, @pipe_w = IO.pipe
|
18
|
+
# @socket_supervisor, @socket_worker = UNIXSocket.socketpair(:SOCK_SEQPACKET)
|
19
|
+
end
|
20
|
+
|
21
|
+
def run
|
22
|
+
ChildSubreaper.enable
|
23
|
+
|
24
|
+
trap('SIGUSR1') { @messages.push 'respawn' }
|
25
|
+
start_message_parser
|
26
|
+
while true
|
27
|
+
sleep 1
|
28
|
+
check_worker_state
|
29
|
+
spawn_worker if need_spawn?
|
30
|
+
select_leader if !@leader_pid
|
31
|
+
end
|
32
|
+
rescue Interrupt
|
33
|
+
return
|
34
|
+
ensure
|
35
|
+
stop_message_parser
|
36
|
+
kill_all_workers
|
37
|
+
end
|
38
|
+
|
39
|
+
def stop
|
40
|
+
kill_all_workers
|
41
|
+
end
|
42
|
+
|
43
|
+
def check_worker_state
|
44
|
+
process_received_messages
|
45
|
+
reap_unresponsive_workers
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def start_message_parser
|
51
|
+
@message_parser_thread = Thread.new { recv_msg_loop }
|
52
|
+
end
|
53
|
+
|
54
|
+
def stop_message_parser
|
55
|
+
@message_parser_thread&.kill&.join
|
56
|
+
end
|
57
|
+
|
58
|
+
def recv_msg_loop
|
59
|
+
# @pipe_w.close
|
60
|
+
loop do
|
61
|
+
msg = recv_msg
|
62
|
+
@messages.push msg
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def recv_msg
|
67
|
+
@pipe_r.gets(chomp: true)
|
68
|
+
end
|
69
|
+
|
70
|
+
WORKER_PING_TTL = 15
|
71
|
+
|
72
|
+
def process_received_messages
|
73
|
+
while !@messages.empty?
|
74
|
+
msg = @messages.shift
|
75
|
+
parts = msg.split(':')
|
76
|
+
case parts[0]
|
77
|
+
when 'ping'
|
78
|
+
handle_ping(*parts[1..2].map(&:to_i))
|
79
|
+
when 'reap'
|
80
|
+
handle_reap(parts[1].to_i)
|
81
|
+
when 'respawn'
|
82
|
+
handle_respawn
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def handle_ping(pid, generation)
|
88
|
+
@workers[pid] = { stamp: Time.now.to_i, generation: generation }
|
89
|
+
end
|
90
|
+
|
91
|
+
def handle_reap(pid)
|
92
|
+
reap_process(pid)
|
93
|
+
end
|
94
|
+
|
95
|
+
def handle_respawn
|
96
|
+
respawn
|
97
|
+
end
|
98
|
+
|
99
|
+
def reap_unresponsive_workers
|
100
|
+
deadline = Time.now.to_i - WORKER_PING_TTL
|
101
|
+
unresponsive_pids = @workers.each_with_object([]) { |(k, v), o| o << k if v[:stamp] < deadline }
|
102
|
+
unresponsive_pids.each do |pid|
|
103
|
+
kill_process(pid)
|
104
|
+
@workers.delete(pid)
|
105
|
+
select_leader if @leader_pid == pid
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def kill_process(pid, timeout = 10)
|
110
|
+
t0 = Time.now
|
111
|
+
Process.kill('SIGTERM', pid)
|
112
|
+
while Time.now - t0 < timeout
|
113
|
+
wpid, _status = Process.wait2(pid, Process::WNOHANG)
|
114
|
+
return if wpid == pid
|
115
|
+
|
116
|
+
sleep 0.25
|
117
|
+
end
|
118
|
+
|
119
|
+
Process.kill('SIGKILL', pid)
|
120
|
+
Process.wait(pid)
|
121
|
+
rescue Errno::ESRCH, Errno::ECHILD
|
122
|
+
# ignore
|
123
|
+
end
|
124
|
+
|
125
|
+
def reap_process(pid, timeout = 10)
|
126
|
+
t0 = Time.now
|
127
|
+
while Time.now - t0 < timeout
|
128
|
+
wpid, _status = Process.wait2(pid, Process::WNOHANG)
|
129
|
+
return if wpid == pid
|
130
|
+
|
131
|
+
sleep 0.25
|
132
|
+
end
|
133
|
+
|
134
|
+
Process.kill('SIGKILL', pid)
|
135
|
+
Process.wait(pid)
|
136
|
+
rescue Errno::ESRCH, Errno::ECHILD
|
137
|
+
# ignore
|
138
|
+
end
|
139
|
+
|
140
|
+
def kill_all_workers(timeout = 10)
|
141
|
+
t0 = Time.now
|
142
|
+
pids = @workers.keys
|
143
|
+
pids.each { Process.kill('SIGTERM', it) rescue nil }
|
144
|
+
|
145
|
+
while Time.now - t0 < timeout
|
146
|
+
wpid, _status = Process.wait2(-1, Process::WNOHANG)
|
147
|
+
if wpid
|
148
|
+
pids.delete(wpid)
|
149
|
+
@workers.delete(wpid)
|
150
|
+
return if pids.empty?
|
151
|
+
else
|
152
|
+
sleep 0.25
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
pids.each do
|
157
|
+
Process.kill('SIGKILL', it) rescue nil
|
158
|
+
Process.wait(it)
|
159
|
+
@workers.delete(it)
|
160
|
+
end
|
161
|
+
rescue Errno::ECHILD
|
162
|
+
# ignore
|
163
|
+
end
|
164
|
+
|
165
|
+
def need_spawn?
|
166
|
+
@workers.size < CLUSTER_SIZE
|
167
|
+
end
|
168
|
+
|
169
|
+
def select_leader
|
170
|
+
@leader_pid = @workers.keys.sort_by { @workers[it][:generation] }.last
|
171
|
+
|
172
|
+
on_leader_pid_changed
|
173
|
+
end
|
174
|
+
|
175
|
+
# returns [void]
|
176
|
+
def spawn_worker
|
177
|
+
return send_signal('SIGUSR1', @leader_pid) if @leader_pid
|
178
|
+
|
179
|
+
pid = worker_class.new(@pipe_w, &@app).start
|
180
|
+
@workers[pid] = { stamp: Time.now.to_i, generation: 1 } if pid
|
181
|
+
pid
|
182
|
+
end
|
183
|
+
|
184
|
+
def worker_class
|
185
|
+
@opts[:worker_class] ||= Worker
|
186
|
+
end
|
187
|
+
|
188
|
+
def respawn
|
189
|
+
return if !@leader_pid
|
190
|
+
|
191
|
+
pids_to_replace = @workers.keys.select { it != @leader_pid }
|
192
|
+
|
193
|
+
while (pid = pids_to_replace.shift)
|
194
|
+
size_before = @workers.size
|
195
|
+
send_signal('SIGUSR1', @leader_pid)
|
196
|
+
sleep 0.1 while @workers.size > size_before
|
197
|
+
kill_process(pid)
|
198
|
+
end
|
199
|
+
|
200
|
+
select_leader
|
201
|
+
end
|
202
|
+
|
203
|
+
def send_signal(sig, pid)
|
204
|
+
Process.kill(sig, pid)
|
205
|
+
end
|
206
|
+
|
207
|
+
# event hooks (to be ovcerriden)
|
208
|
+
def on_leader_pid_changed(pid); end
|
209
|
+
def on_spawn_next_generation(pid); end
|
210
|
+
def on_reap(pid); end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Coworker
|
4
|
+
class Worker
|
5
|
+
attr_reader :fd, :generation
|
6
|
+
|
7
|
+
def initialize(conn, generation = 1, &app)
|
8
|
+
@fd = conn.fileno
|
9
|
+
@app = app
|
10
|
+
@generation = generation
|
11
|
+
end
|
12
|
+
|
13
|
+
def next_generation
|
14
|
+
self.class.new(IO.new(@fd), @generation + 1, &@app)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Starts a forked orphaned worker process,
|
18
|
+
def start
|
19
|
+
r, w = IO.pipe
|
20
|
+
w.sync = true
|
21
|
+
|
22
|
+
# we count on ChildSubreaper to make it so the
|
23
|
+
# grandparent process reaps the workers
|
24
|
+
parent_pid = fork do
|
25
|
+
fork do
|
26
|
+
send_ping
|
27
|
+
w << "#{Process.pid}"
|
28
|
+
w.close
|
29
|
+
run
|
30
|
+
end
|
31
|
+
|
32
|
+
w.close
|
33
|
+
send_msg("reap:#{Process.pid}")
|
34
|
+
end
|
35
|
+
# get grand-child PID
|
36
|
+
w.close
|
37
|
+
Process.wait(parent_pid)
|
38
|
+
r.read(16).to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def send_msg(msg)
|
44
|
+
if !@conn
|
45
|
+
@conn = IO.new(@fd)
|
46
|
+
@conn.sync = true
|
47
|
+
end
|
48
|
+
# tell supervisor to reap child
|
49
|
+
@conn.write "#{msg}\n"
|
50
|
+
end
|
51
|
+
|
52
|
+
def on_start
|
53
|
+
end
|
54
|
+
|
55
|
+
def on_stop
|
56
|
+
end
|
57
|
+
|
58
|
+
def run
|
59
|
+
on_start
|
60
|
+
|
61
|
+
@spawn_requests = Queue.new
|
62
|
+
trap(:SIGUSR1) { @spawn_requests << true }
|
63
|
+
|
64
|
+
spawn_thread = Thread.new { spawn_loop}
|
65
|
+
pinger_thread = Thread.new { ping_loop }
|
66
|
+
|
67
|
+
@app.call(self)
|
68
|
+
ensure
|
69
|
+
on_stop
|
70
|
+
spawn_thread.kill.join
|
71
|
+
pinger_thread.kill.join
|
72
|
+
@conn&.close rescue nil
|
73
|
+
end
|
74
|
+
|
75
|
+
def spawn_loop
|
76
|
+
loop do
|
77
|
+
@spawn_requests.shift && next_generation.start
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def send_ping
|
82
|
+
@pid ||= Process.pid
|
83
|
+
send_msg("ping:#{@pid}:#{@generation}")
|
84
|
+
end
|
85
|
+
|
86
|
+
def ping_loop
|
87
|
+
loop do
|
88
|
+
sleep 5
|
89
|
+
send_ping
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/coworker.rb
ADDED
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coworker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sharon Rosner
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: child_subreaper
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - '='
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 0.1.0
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - '='
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.1.0
|
26
|
+
description: for optimizing memory usage
|
27
|
+
email:
|
28
|
+
- sharon@noteflakes.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- CHANGELOG.md
|
34
|
+
- LICENSE
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- TODO.md
|
39
|
+
- examples/simple.rb
|
40
|
+
- lib/coworker.rb
|
41
|
+
- lib/coworker/supervisor.rb
|
42
|
+
- lib/coworker/version.rb
|
43
|
+
- lib/coworker/worker.rb
|
44
|
+
homepage: https://github.com/noteflakes/coworker
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata:
|
48
|
+
homepage_uri: https://github.com/noteflakes/coworker
|
49
|
+
source_code_uri: https://github.com/noteflakes/coworker
|
50
|
+
changelog_uri: https://github.com/noteflakes/coworker/CHANGELOG.md
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 3.4.0
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubygems_version: 3.6.8
|
66
|
+
specification_version: 4
|
67
|
+
summary: Worker process supervision
|
68
|
+
test_files: []
|