sidekiq-mem-warden 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 575fff9fdaf94990af42942e3fe1ab7bb314277e583e8e21a2eeae31ec2d970e
4
+ data.tar.gz: 93ae73d198036cf34286d49ac31a666b38f8fe51fec8b6cb68f596b562b39ce8
5
+ SHA512:
6
+ metadata.gz: 32ea98d03d13bf6749c138d971a48662d4ef240f8ef28c044552f2187d9b2411d332670ea91bf795b2bacc568130adc955bb280a9d004024d5c0d55f3a3372dd
7
+ data.tar.gz: 492bfb015c2512f75d3f7cdbfe03ac1c2069f59893b5a9d99ef00d141a85e7f68050cac3f138bbff1139379ebe2825244d34351fcffcdaef0c1c9c56a694caa8
@@ -0,0 +1,22 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: ["master"]
6
+ pull_request:
7
+ branches: ["master"]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+
15
+ - name: Set up Ruby
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: "3.2"
19
+ bundler-cache: true
20
+
21
+ - name: Run specs
22
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.10
7
+ before_install: gem install bundler -v 1.17.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+ - Initial release with RSS-based watchdog, quiet, drain, and restart behavior.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sidekiq-mem-warden.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sidekiq-mem-warden (0.1.0)
5
+ sidekiq (>= 6.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ connection_pool (2.5.3)
11
+ diff-lcs (1.6.2)
12
+ rack (2.2.17)
13
+ rake (13.3.0)
14
+ redis (4.8.1)
15
+ rspec (3.13.1)
16
+ rspec-core (~> 3.13.0)
17
+ rspec-expectations (~> 3.13.0)
18
+ rspec-mocks (~> 3.13.0)
19
+ rspec-core (3.13.5)
20
+ rspec-support (~> 3.13.0)
21
+ rspec-expectations (3.13.5)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.13.0)
24
+ rspec-mocks (3.13.5)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.13.0)
27
+ rspec-support (3.13.6)
28
+ sidekiq (6.5.12)
29
+ connection_pool (>= 2.2.5, < 3)
30
+ rack (~> 2.0)
31
+ redis (>= 4.5.0, < 5)
32
+
33
+ PLATFORMS
34
+ arm64-darwin-24
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (>= 1.17)
39
+ rake (>= 10.0)
40
+ rspec (~> 3.0)
41
+ sidekiq-mem-warden!
42
+
43
+ BUNDLED WITH
44
+ 2.6.9
data/LICENSE.txt ADDED
@@ -0,0 +1,308 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Library General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but does
111
+ not normally print such an announcement, your work based on the
112
+ Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three years,
143
+ to give any third party, for a charge no more than your cost of
144
+ physically performing source distribution, a complete machine-readable
145
+ copy of the corresponding source code, to be distributed under the
146
+ terms of Sections 1 and 2 above on a medium customarily used for
147
+ software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer to
150
+ distribute corresponding source code. (This alternative is allowed
151
+ only for noncommercial distribution and only if you received the
152
+ program in object code or executable form with such an offer, in accord
153
+ with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt otherwise
174
+ to copy, modify, sublicense or distribute the Program is void, and will
175
+ automatically terminate your rights under this License. However, parties
176
+ who have received copies, or rights, from you under this License will
177
+ not have their licenses terminated so long as such parties remain in
178
+ full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further restrictions
193
+ on the recipients' exercise of the rights granted herein. You are not
194
+ responsible for enforcing compliance by third parties to this License.
195
+
196
+ 7. If, as a consequence of a court judgment or allegation of patent
197
+ infringement or for any other reason (not limited to patent issues),
198
+ conditions are imposed on you (whether by court order, agreement or
199
+ otherwise) that contradict the conditions of this License, they do not
200
+ excuse you from the conditions of this License. If you cannot distribute
201
+ so as to satisfy simultaneously your obligations under this License and
202
+ any other pertinent obligations, then as a consequence you may not
203
+ distribute the Program at all. For example, if a patent license would
204
+ not permit royalty-free redistribution of the Program by all those who
205
+ receive copies directly or indirectly through you, then the only way you
206
+ could satisfy both it and this License would be to refrain entirely from
207
+ distribution of the Program.
208
+
209
+ If any portion of this section is held invalid or unenforceable under
210
+ any particular circumstance, the balance of the section is intended to
211
+ apply and the section as a whole is intended to apply in other
212
+ circumstances.
213
+
214
+ It is not the purpose of this section to induce you to infringe any
215
+ patents or other property right claims or to contest validity of any
216
+ such claims; this section has the sole purpose of protecting the
217
+ integrity of the free software distribution system, which is
218
+ implemented by public license practices. Many people have made
219
+ generous contributions to the wide range of software distributed
220
+ through that system in reliance on consistent application of that
221
+ system; it is up to the author/donor to decide if he or she is willing
222
+ to distribute software through any other system and a licensee cannot
223
+ impose that choice.
224
+
225
+ This section is intended to make thoroughly clear that you have no
226
+ rights to use, modify or distribute the Program except as expressly
227
+ provided under this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License may
232
+ add an explicit geographical distribution limitation excluding those
233
+ countries, so that distribution is permitted only in or among countries
234
+ not thus excluded. In such case, this License incorporates the
235
+ limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ state the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ <one line to give the program's name and a brief idea of what it does.>
294
+ Copyright (C) <year> <name of author>
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License
307
+ along with this program; if not, write to the Free Software
308
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Sidekiq::Mem::Warden
2
+
3
+ Sidekiq::Mem::Warden is a tiny, in-process watchdog that quiets a Sidekiq process when RSS exceeds a limit, waits for busy jobs to drain, then exits so your process manager can restart it.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sidekiq-mem-warden'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install sidekiq-mem-warden
20
+
21
+ ## Usage
22
+
23
+ Configure it in your Sidekiq server process:
24
+
25
+ ```ruby
26
+ require "sidekiq/mem/warden"
27
+
28
+ Sidekiq.configure_server do |config|
29
+ Sidekiq::Mem::Warden.configure do |c|
30
+ c.memory_limit_mb = 1024
31
+ c.check_interval = 15
32
+ c.quiet_timeout = 30
33
+ c.shutdown_timeout = 300
34
+ end
35
+
36
+ Sidekiq::Mem::Warden.install!(config)
37
+ end
38
+ ```
39
+
40
+ Operational notes:
41
+
42
+ - The warden runs inside each Sidekiq process.
43
+ - When memory exceeds the limit, it sends `TSTP` to quiet the process, sleeps for `quiet_timeout`, waits for `Sidekiq::Workers` to drain, then sends `TERM` to exit.
44
+ - Ensure your supervisor (systemd, Kubernetes, etc.) is set to restart the process.
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tedaford/sidekiq-mem-warden.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the GNU General Public License v2.0.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sidekiq/mem/warden"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ module Sidekiq
2
+ module Mem
3
+ module Warden
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,132 @@
1
+ require "sidekiq"
2
+ require "sidekiq/mem/warden/version"
3
+
4
+ module Sidekiq
5
+ module Mem
6
+ module Warden
7
+ class Error < StandardError; end
8
+
9
+ class Config
10
+ attr_accessor :memory_limit_mb, :check_interval, :quiet_timeout, :shutdown_timeout, :logger
11
+
12
+ def initialize
13
+ @memory_limit_mb = 1024
14
+ @check_interval = 15
15
+ @quiet_timeout = 30
16
+ @shutdown_timeout = 300
17
+ @logger = nil
18
+ end
19
+ end
20
+
21
+ def self.configure
22
+ yield(config)
23
+ end
24
+
25
+ def self.install!(sidekiq_config = nil)
26
+ sidekiq_config ||= Sidekiq
27
+ sidekiq_config.on(:startup) do
28
+ start_monitor(Warden::Monitor.new(config))
29
+ end
30
+ end
31
+
32
+ def self.config
33
+ @config ||= Config.new
34
+ end
35
+
36
+ def self.start_monitor(monitor)
37
+ @start_mutex ||= Mutex.new
38
+ @start_mutex.synchronize do
39
+ return if @started
40
+ @started = true
41
+ end
42
+ monitor.start
43
+ end
44
+
45
+ class Monitor
46
+ def initialize(config)
47
+ @config = config
48
+ @logger = config.logger || Sidekiq.logger
49
+ @triggered = false
50
+ @lock = Mutex.new
51
+ @start_lock = Mutex.new
52
+ @started = false
53
+ end
54
+
55
+ def start
56
+ @start_lock.synchronize do
57
+ return if @started
58
+ @started = true
59
+ end
60
+
61
+ @thread = Thread.new do
62
+ Thread.current.name = "sidekiq-mem-warden" if Thread.current.respond_to?(:name=)
63
+ loop do
64
+ sleep @config.check_interval
65
+ next unless over_limit?
66
+ trigger!
67
+ break
68
+ end
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def trigger!
75
+ @lock.synchronize do
76
+ return if @triggered
77
+ @triggered = true
78
+ end
79
+
80
+ @logger.warn("[sidekiq-mem-warden] RSS over limit (#{rss_mb}MB >= #{@config.memory_limit_mb}MB), quieting")
81
+ begin
82
+ Process.kill("TSTP", Process.pid)
83
+ rescue StandardError => e
84
+ @logger.warn("[sidekiq-mem-warden] failed to send TSTP: #{e.class}: #{e.message}")
85
+ end
86
+
87
+ sleep @config.quiet_timeout
88
+ wait_for_idle(@config.shutdown_timeout)
89
+
90
+ @logger.warn("[sidekiq-mem-warden] shutting down for restart")
91
+ begin
92
+ Process.kill("TERM", Process.pid)
93
+ rescue StandardError => e
94
+ @logger.warn("[sidekiq-mem-warden] failed to send TERM: #{e.class}: #{e.message}")
95
+ end
96
+ end
97
+
98
+ def wait_for_idle(timeout_seconds)
99
+ deadline = Time.now + timeout_seconds
100
+ while Time.now < deadline
101
+ busy = Sidekiq::Workers.new.size
102
+ return if busy == 0
103
+ sleep 1
104
+ end
105
+ @logger.warn("[sidekiq-mem-warden] timeout waiting for busy to drain; proceeding")
106
+ end
107
+
108
+ def over_limit?
109
+ rss_mb >= @config.memory_limit_mb
110
+ end
111
+
112
+ def rss_mb
113
+ (rss_kb.to_f / 1024).round(2)
114
+ end
115
+
116
+ def rss_kb
117
+ status_path = "/proc/#{Process.pid}/status"
118
+ if File.exist?(status_path)
119
+ status = File.read(status_path)
120
+ match = status.match(/^VmRSS:\s+(\d+)\s+kB$/)
121
+ return match[1].to_i if match
122
+ end
123
+
124
+ output = `ps -o rss= -p #{Process.pid}`.to_s.strip
125
+ output.to_i
126
+ rescue StandardError
127
+ 0
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sidekiq/mem/warden/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-mem-warden"
8
+ spec.version = Sidekiq::Mem::Warden::VERSION
9
+ spec.authors = ["tedaford"]
10
+ spec.email = ["daturafarms@gmail.com"]
11
+
12
+ spec.summary = %q{Sidekiq watchdog that quiets and restarts on high RSS.}
13
+ spec.description = %q{A small in-process watchdog that quiets Sidekiq with TSTP, waits for busy jobs to drain, and exits for a clean restart when RSS exceeds a configured limit.}
14
+ spec.homepage = "https://github.com/tedford/sidekiq-mem-warden"
15
+ spec.license = "GPL-2.0-only"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = spec.homepage
24
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency "sidekiq", ">= 6.0"
40
+ spec.add_development_dependency "bundler", ">= 1.17"
41
+ spec.add_development_dependency "rake", ">= 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-mem-warden
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tedaford
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: sidekiq
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '1.17'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '1.17'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rake
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '10.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ description: A small in-process watchdog that quiets Sidekiq with TSTP, waits for
69
+ busy jobs to drain, and exits for a clean restart when RSS exceeds a configured
70
+ limit.
71
+ email:
72
+ - daturafarms@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/ci.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CHANGELOG.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - lib/sidekiq/mem/warden.rb
90
+ - lib/sidekiq/mem/warden/version.rb
91
+ - sidekiq-mem-warden.gemspec
92
+ homepage: https://github.com/tedford/sidekiq-mem-warden
93
+ licenses:
94
+ - GPL-2.0-only
95
+ metadata:
96
+ allowed_push_host: https://rubygems.org
97
+ homepage_uri: https://github.com/tedford/sidekiq-mem-warden
98
+ source_code_uri: https://github.com/tedford/sidekiq-mem-warden
99
+ changelog_uri: https://github.com/tedford/sidekiq-mem-warden/blob/main/CHANGELOG.md
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.6.9
115
+ specification_version: 4
116
+ summary: Sidekiq watchdog that quiets and restarts on high RSS.
117
+ test_files: []