slotz 0.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1829fa39dcca433a7c74867bde89d53f70447c4c72c7abd54fb7ef4e68a73c87
4
- data.tar.gz: acc87fb694a334b2fbaabf99bb4c608ee6fd9fa168f4f143c2087fd4e86e4f2d
3
+ metadata.gz: 647d08d88f1e16c20c9aa18c9105aad86b3f1b5136761ad9a192ae0ff2405f90
4
+ data.tar.gz: bb7622e79a6744750e352ef3cc010d94f8ec48a1c373283d10291442dbe53a4f
5
5
  SHA512:
6
- metadata.gz: cae56d7e4293d7b3152dd353127ef550b089fefb49aea0e3b83cb48e41a67fee0f9a3e7d2f2166872791ceebcabe5066039ecb5969f64063e4a8358fd0ef2a68
7
- data.tar.gz: 05e01352f292781b59cd23cc4fc0ae665ff718d4c1451c8afefdbcd1c9c3c9149e1bc9ccc6cab42ee97d3a89fbd9a3b62d9f61b67c9de671760a2a9438c62ea1
6
+ metadata.gz: 50d8da5b167c1fabc9e82be9ec8ebbe9d82cbff501961a6c8d3d17ff11193467c5956ca762a1af7e75f4214816ecf49f464eb4e862224a3bd8bde6613d2f198c
7
+ data.tar.gz: 751e119f5b07eca377631646c137c21114e603f083fd557e5b8e18ac9789fc375df8b450e8a40e76c44e1e94198fd2b3f76613424f6c596907d4ea1a42d50410
data/Gemfile CHANGED
@@ -16,4 +16,7 @@ group :prof do
16
16
  gem 'memory_profiler'
17
17
  end
18
18
 
19
+ gem 'msgpack'
20
+ gem 'toq'
21
+
19
22
  gemspec
data/LICENSE.md CHANGED
@@ -1,29 +1,375 @@
1
- # License
2
-
3
- Copyright (C) 2025, Anastasios Laskos <mailto:anastasios.laskos@gmail.com>
4
- All rights reserved.
5
-
6
- Redistribution and use in source and binary forms, with or without modification,
7
- are permitted provided that the following conditions are met:
8
-
9
- * Redistributions of source code must retain the above copyright notice,
10
- this list of conditions and the following disclaimer.
11
-
12
- * Redistributions in binary form must reproduce the above copyright notice,
13
- this list of conditions and the following disclaimer in the documentation
14
- and/or other materials provided with the distribution.
15
-
16
- * Neither the name of the copyright holder nor the names of its contributors
17
- may be used to endorse or promote products derived from this software
18
- without specific prior written permission.
19
-
20
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24
- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27
- ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ ```
2
+ Mozilla Public License Version 2.0
3
+ ==================================
4
+
5
+ 1. Definitions
6
+ --------------
7
+
8
+ 1.1. "Contributor"
9
+ means each individual or legal entity that creates, contributes to
10
+ the creation of, or owns Covered Software.
11
+
12
+ 1.2. "Contributor Version"
13
+ means the combination of the Contributions of others (if any) used
14
+ by a Contributor and that particular Contributor's Contribution.
15
+
16
+ 1.3. "Contribution"
17
+ means Covered Software of a particular Contributor.
18
+
19
+ 1.4. "Covered Software"
20
+ means Source Code Form to which the initial Contributor has attached
21
+ the notice in Exhibit A, the Executable Form of such Source Code
22
+ Form, and Modifications of such Source Code Form, in each case
23
+ including portions thereof.
24
+
25
+ 1.5. "Incompatible With Secondary Licenses"
26
+ means
27
+
28
+ (a) that the initial Contributor has attached the notice described
29
+ in Exhibit B to the Covered Software; or
30
+
31
+ (b) that the Covered Software was made available under the terms of
32
+ version 1.1 or earlier of the License, but not also under the
33
+ terms of a Secondary License.
34
+
35
+ 1.6. "Executable Form"
36
+ means any form of the work other than Source Code Form.
37
+
38
+ 1.7. "Larger Work"
39
+ means a work that combines Covered Software with other material, in
40
+ a separate file or files, that is not Covered Software.
41
+
42
+ 1.8. "License"
43
+ means this document.
44
+
45
+ 1.9. "Licensable"
46
+ means having the right to grant, to the maximum extent possible,
47
+ whether at the time of the initial grant or subsequently, any and
48
+ all of the rights conveyed by this License.
49
+
50
+ 1.10. "Modifications"
51
+ means any of the following:
52
+
53
+ (a) any file in Source Code Form that results from an addition to,
54
+ deletion from, or modification of the contents of Covered
55
+ Software; or
56
+
57
+ (b) any new file in Source Code Form that contains any Covered
58
+ Software.
59
+
60
+ 1.11. "Patent Claims" of a Contributor
61
+ means any patent claim(s), including without limitation, method,
62
+ process, and apparatus claims, in any patent Licensable by such
63
+ Contributor that would be infringed, but for the grant of the
64
+ License, by the making, using, selling, offering for sale, having
65
+ made, import, or transfer of either its Contributions or its
66
+ Contributor Version.
67
+
68
+ 1.12. "Secondary License"
69
+ means either the GNU General Public License, Version 2.0, the GNU
70
+ Lesser General Public License, Version 2.1, the GNU Affero General
71
+ Public License, Version 3.0, or any later versions of those
72
+ licenses.
73
+
74
+ 1.13. "Source Code Form"
75
+ means the form of the work preferred for making modifications.
76
+
77
+ 1.14. "You" (or "Your")
78
+ means an individual or a legal entity exercising rights under this
79
+ License. For legal entities, "You" includes any entity that
80
+ controls, is controlled by, or is under common control with You. For
81
+ purposes of this definition, "control" means (a) the power, direct
82
+ or indirect, to cause the direction or management of such entity,
83
+ whether by contract or otherwise, or (b) ownership of more than
84
+ fifty percent (50%) of the outstanding shares or beneficial
85
+ ownership of such entity.
86
+
87
+ 2. License Grants and Conditions
88
+ --------------------------------
89
+
90
+ 2.1. Grants
91
+
92
+ Each Contributor hereby grants You a world-wide, royalty-free,
93
+ non-exclusive license:
94
+
95
+ (a) under intellectual property rights (other than patent or trademark)
96
+ Licensable by such Contributor to use, reproduce, make available,
97
+ modify, display, perform, distribute, and otherwise exploit its
98
+ Contributions, either on an unmodified basis, with Modifications, or
99
+ as part of a Larger Work; and
100
+
101
+ (b) under Patent Claims of such Contributor to make, use, sell, offer
102
+ for sale, have made, import, and otherwise transfer either its
103
+ Contributions or its Contributor Version.
104
+
105
+ 2.2. Effective Date
106
+
107
+ The licenses granted in Section 2.1 with respect to any Contribution
108
+ become effective for each Contribution on the date the Contributor first
109
+ distributes such Contribution.
110
+
111
+ 2.3. Limitations on Grant Scope
112
+
113
+ The licenses granted in this Section 2 are the only rights granted under
114
+ this License. No additional rights or licenses will be implied from the
115
+ distribution or licensing of Covered Software under this License.
116
+ Notwithstanding Section 2.1(b) above, no patent license is granted by a
117
+ Contributor:
118
+
119
+ (a) for any code that a Contributor has removed from Covered Software;
120
+ or
121
+
122
+ (b) for infringements caused by: (i) Your and any other third party's
123
+ modifications of Covered Software, or (ii) the combination of its
124
+ Contributions with other software (except as part of its Contributor
125
+ Version); or
126
+
127
+ (c) under Patent Claims infringed by Covered Software in the absence of
128
+ its Contributions.
129
+
130
+ This License does not grant any rights in the trademarks, service marks,
131
+ or logos of any Contributor (except as may be necessary to comply with
132
+ the notice requirements in Section 3.4).
133
+
134
+ 2.4. Subsequent Licenses
135
+
136
+ No Contributor makes additional grants as a result of Your choice to
137
+ distribute the Covered Software under a subsequent version of this
138
+ License (see Section 10.2) or under the terms of a Secondary License (if
139
+ permitted under the terms of Section 3.3).
140
+
141
+ 2.5. Representation
142
+
143
+ Each Contributor represents that the Contributor believes its
144
+ Contributions are its original creation(s) or it has sufficient rights
145
+ to grant the rights to its Contributions conveyed by this License.
146
+
147
+ 2.6. Fair Use
148
+
149
+ This License is not intended to limit any rights You have under
150
+ applicable copyright doctrines of fair use, fair dealing, or other
151
+ equivalents.
152
+
153
+ 2.7. Conditions
154
+
155
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
156
+ in Section 2.1.
157
+
158
+ 3. Responsibilities
159
+ -------------------
160
+
161
+ 3.1. Distribution of Source Form
162
+
163
+ All distribution of Covered Software in Source Code Form, including any
164
+ Modifications that You create or to which You contribute, must be under
165
+ the terms of this License. You must inform recipients that the Source
166
+ Code Form of the Covered Software is governed by the terms of this
167
+ License, and how they can obtain a copy of this License. You may not
168
+ attempt to alter or restrict the recipients' rights in the Source Code
169
+ Form.
170
+
171
+ 3.2. Distribution of Executable Form
172
+
173
+ If You distribute Covered Software in Executable Form then:
174
+
175
+ (a) such Covered Software must also be made available in Source Code
176
+ Form, as described in Section 3.1, and You must inform recipients of
177
+ the Executable Form how they can obtain a copy of such Source Code
178
+ Form by reasonable means in a timely manner, at a charge no more
179
+ than the cost of distribution to the recipient; and
180
+
181
+ (b) You may distribute such Executable Form under the terms of this
182
+ License, or sublicense it under different terms, provided that the
183
+ license for the Executable Form does not attempt to limit or alter
184
+ the recipients' rights in the Source Code Form under this License.
185
+
186
+ 3.3. Distribution of a Larger Work
187
+
188
+ You may create and distribute a Larger Work under terms of Your choice,
189
+ provided that You also comply with the requirements of this License for
190
+ the Covered Software. If the Larger Work is a combination of Covered
191
+ Software with a work governed by one or more Secondary Licenses, and the
192
+ Covered Software is not Incompatible With Secondary Licenses, this
193
+ License permits You to additionally distribute such Covered Software
194
+ under the terms of such Secondary License(s), so that the recipient of
195
+ the Larger Work may, at their option, further distribute the Covered
196
+ Software under the terms of either this License or such Secondary
197
+ License(s).
198
+
199
+ 3.4. Notices
200
+
201
+ You may not remove or alter the substance of any license notices
202
+ (including copyright notices, patent notices, disclaimers of warranty,
203
+ or limitations of liability) contained within the Source Code Form of
204
+ the Covered Software, except that You may alter any license notices to
205
+ the extent required to remedy known factual inaccuracies.
206
+
207
+ 3.5. Application of Additional Terms
208
+
209
+ You may choose to offer, and to charge a fee for, warranty, support,
210
+ indemnity or liability obligations to one or more recipients of Covered
211
+ Software. However, You may do so only on Your own behalf, and not on
212
+ behalf of any Contributor. You must make it absolutely clear that any
213
+ such warranty, support, indemnity, or liability obligation is offered by
214
+ You alone, and You hereby agree to indemnify every Contributor for any
215
+ liability incurred by such Contributor as a result of warranty, support,
216
+ indemnity or liability terms You offer. You may include additional
217
+ disclaimers of warranty and limitations of liability specific to any
218
+ jurisdiction.
219
+
220
+ 4. Inability to Comply Due to Statute or Regulation
221
+ ---------------------------------------------------
222
+
223
+ If it is impossible for You to comply with any of the terms of this
224
+ License with respect to some or all of the Covered Software due to
225
+ statute, judicial order, or regulation then You must: (a) comply with
226
+ the terms of this License to the maximum extent possible; and (b)
227
+ describe the limitations and the code they affect. Such description must
228
+ be placed in a text file included with all distributions of the Covered
229
+ Software under this License. Except to the extent prohibited by statute
230
+ or regulation, such description must be sufficiently detailed for a
231
+ recipient of ordinary skill to be able to understand it.
232
+
233
+ 5. Termination
234
+ --------------
235
+
236
+ 5.1. The rights granted under this License will terminate automatically
237
+ if You fail to comply with any of its terms. However, if You become
238
+ compliant, then the rights granted under this License from a particular
239
+ Contributor are reinstated (a) provisionally, unless and until such
240
+ Contributor explicitly and finally terminates Your grants, and (b) on an
241
+ ongoing basis, if such Contributor fails to notify You of the
242
+ non-compliance by some reasonable means prior to 60 days after You have
243
+ come back into compliance. Moreover, Your grants from a particular
244
+ Contributor are reinstated on an ongoing basis if such Contributor
245
+ notifies You of the non-compliance by some reasonable means, this is the
246
+ first time You have received notice of non-compliance with this License
247
+ from such Contributor, and You become compliant prior to 30 days after
248
+ Your receipt of the notice.
249
+
250
+ 5.2. If You initiate litigation against any entity by asserting a patent
251
+ infringement claim (excluding declaratory judgment actions,
252
+ counter-claims, and cross-claims) alleging that a Contributor Version
253
+ directly or indirectly infringes any patent, then the rights granted to
254
+ You by any and all Contributors for the Covered Software under Section
255
+ 2.1 of this License shall terminate.
256
+
257
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all
258
+ end user license agreements (excluding distributors and resellers) which
259
+ have been validly granted by You or Your distributors under this License
260
+ prior to termination shall survive termination.
261
+
262
+ ************************************************************************
263
+ * *
264
+ * 6. Disclaimer of Warranty *
265
+ * ------------------------- *
266
+ * *
267
+ * Covered Software is provided under this License on an "as is" *
268
+ * basis, without warranty of any kind, either expressed, implied, or *
269
+ * statutory, including, without limitation, warranties that the *
270
+ * Covered Software is free of defects, merchantable, fit for a *
271
+ * particular purpose or non-infringing. The entire risk as to the *
272
+ * quality and performance of the Covered Software is with You. *
273
+ * Should any Covered Software prove defective in any respect, You *
274
+ * (not any Contributor) assume the cost of any necessary servicing, *
275
+ * repair, or correction. This disclaimer of warranty constitutes an *
276
+ * essential part of this License. No use of any Covered Software is *
277
+ * authorized under this License except under this disclaimer. *
278
+ * *
279
+ ************************************************************************
280
+
281
+ ************************************************************************
282
+ * *
283
+ * 7. Limitation of Liability *
284
+ * -------------------------- *
285
+ * *
286
+ * Under no circumstances and under no legal theory, whether tort *
287
+ * (including negligence), contract, or otherwise, shall any *
288
+ * Contributor, or anyone who distributes Covered Software as *
289
+ * permitted above, be liable to You for any direct, indirect, *
290
+ * special, incidental, or consequential damages of any character *
291
+ * including, without limitation, damages for lost profits, loss of *
292
+ * goodwill, work stoppage, computer failure or malfunction, or any *
293
+ * and all other commercial damages or losses, even if such party *
294
+ * shall have been informed of the possibility of such damages. This *
295
+ * limitation of liability shall not apply to liability for death or *
296
+ * personal injury resulting from such party's negligence to the *
297
+ * extent applicable law prohibits such limitation. Some *
298
+ * jurisdictions do not allow the exclusion or limitation of *
299
+ * incidental or consequential damages, so this exclusion and *
300
+ * limitation may not apply to You. *
301
+ * *
302
+ ************************************************************************
303
+
304
+ 8. Litigation
305
+ -------------
306
+
307
+ Any litigation relating to this License may be brought only in the
308
+ courts of a jurisdiction where the defendant maintains its principal
309
+ place of business and such litigation shall be governed by laws of that
310
+ jurisdiction, without reference to its conflict-of-law provisions.
311
+ Nothing in this Section shall prevent a party's ability to bring
312
+ cross-claims or counter-claims.
313
+
314
+ 9. Miscellaneous
315
+ ----------------
316
+
317
+ This License represents the complete agreement concerning the subject
318
+ matter hereof. If any provision of this License is held to be
319
+ unenforceable, such provision shall be reformed only to the extent
320
+ necessary to make it enforceable. Any law or regulation which provides
321
+ that the language of a contract shall be construed against the drafter
322
+ shall not be used to construe this License against a Contributor.
323
+
324
+ 10. Versions of the License
325
+ ---------------------------
326
+
327
+ 10.1. New Versions
328
+
329
+ Mozilla Foundation is the license steward. Except as provided in Section
330
+ 10.3, no one other than the license steward has the right to modify or
331
+ publish new versions of this License. Each version will be given a
332
+ distinguishing version number.
333
+
334
+ 10.2. Effect of New Versions
335
+
336
+ You may distribute the Covered Software under the terms of the version
337
+ of the License under which You originally received the Covered Software,
338
+ or under the terms of any subsequent version published by the license
339
+ steward.
340
+
341
+ 10.3. Modified Versions
342
+
343
+ If you create software not governed by this License, and you want to
344
+ create a new license for such software, you may create and use a
345
+ modified version of this License if you rename the license and remove
346
+ any references to the name of the license steward (except to note that
347
+ such modified license differs from this License).
348
+
349
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary
350
+ Licenses
351
+
352
+ If You choose to distribute Source Code Form that is Incompatible With
353
+ Secondary Licenses under the terms of this version of the License, the
354
+ notice described in Exhibit B of this License must be attached.
355
+
356
+ Exhibit A - Source Code Form License Notice
357
+ -------------------------------------------
358
+
359
+ This Source Code Form is subject to the terms of the Mozilla Public
360
+ License, v. 2.0. If a copy of the MPL was not distributed with this
361
+ file, You can obtain one at https://mozilla.org/MPL/2.0/.
362
+
363
+ If it is not possible or desirable to put the notice in a particular
364
+ file, then You may include the notice in a location (such as a LICENSE
365
+ file in a relevant directory) where a recipient would be likely to look
366
+ for such a notice.
367
+
368
+ You may add additional accurate notices of copyright ownership.
369
+
370
+ Exhibit B - "Incompatible With Secondary Licenses" Notice
371
+ ---------------------------------------------------------
372
+
373
+ This Source Code Form is "Incompatible With Secondary Licenses", as
374
+ defined by the Mozilla Public License, v. 2.0.
375
+ ```
data/README.md CHANGED
@@ -1,3 +1,105 @@
1
1
  # Slotz
2
2
 
3
- TBD/WiP
3
+ A resource management library for Ruby, an assistance to help you keep your resource (disk, memory)
4
+ requirements healthy.
5
+
6
+ ## Reservations
7
+
8
+ Reserve the amount of resources your application needs, and move forward accordingly.
9
+
10
+ You can stop allocating resources upon getting an exception indicating insufficient resources
11
+ of any type.
12
+
13
+ ```ruby
14
+ require 'slotz'
15
+
16
+ class MyApp
17
+
18
+ Slotz::Reservation.provision(
19
+ self,
20
+ disk: 1 * 1_000_000_000, # bytes
21
+ memory: 5 * 1_000_000_000 #
22
+ )
23
+
24
+ end
25
+
26
+ my_app = MyApp.new
27
+
28
+ p my_app.available_slots
29
+ #=> 11
30
+
31
+ p my_app.available_slots_on_disk
32
+ #=> 1425
33
+
34
+ p my_app.available_slots_in_memory
35
+ #=> 11
36
+
37
+ p Slotz.utilization
38
+ #=> 0.08933773478082488 %
39
+ ```
40
+
41
+ versus:
42
+
43
+ ```ruby
44
+ require 'slotz'
45
+
46
+ class MyApp
47
+ Slotz::Reservation.provision(
48
+ self,
49
+ disk: 99 * 1_000_000_000, # bytes
50
+ memory: 50 * 1_000_000_000 #
51
+ )
52
+ end
53
+
54
+ my_app = MyApp.new
55
+
56
+ p my_app.available_slots
57
+ #=> 1
58
+
59
+ p my_app.available_slots_on_disk
60
+ #=> 13
61
+
62
+ p my_app.available_slots_in_memory
63
+ #=> 1
64
+
65
+ p Slotz.utilization
66
+ #=> 0.911408461062344
67
+ ```
68
+
69
+ ## Process loading/spawning
70
+
71
+ In order to still allow for `spawn`/`loader` functionality to be accommodated, you can spawn or load
72
+ your file of interest accordingly, and still maintain the desired resource management.
73
+
74
+ This can be the file you wish to load:
75
+
76
+ ### `child.rb:`
77
+
78
+ ```ruby
79
+ p $options # Pre-set options.
80
+ # => {:execute=>false}
81
+ # => {:my=>:option, :ppid=>3158249, :tmpdir=>"/tmp", :execute=>true}
82
+
83
+ class Child
84
+ Slotz::Reservation.provision(
85
+ self,
86
+ disk: 1 * 1_000_000_000,
87
+ memory: 5 * 1_000_000_000
88
+ )
89
+ end
90
+
91
+ # Just load for the inside view. don't run anything, unless you've been instructed to.
92
+ return unless $execute
93
+
94
+ # Load everything to run.
95
+ # [...]
96
+ ```
97
+
98
+ ### `loader.rb:`
99
+ ```ruby
100
+ require 'slotz'
101
+
102
+ loader = Slotz::Loader.new
103
+ loader.load( 'Child', "tmp/test2/child.rb", { my: :option } )
104
+
105
+ ```
@@ -0,0 +1,8 @@
1
+ require 'base64'
2
+ require 'slotz'
3
+
4
+ $options = Marshal.load( Base64.strict_decode64( ARGV.pop ) )
5
+ $executable = ARGV.pop
6
+ $execute = !!$options[:execute]
7
+
8
+ load $executable
@@ -0,0 +1,113 @@
1
+ require 'base64'
2
+
3
+ module Slotz
4
+ class Loader
5
+
6
+ RUNNER = "#{File.dirname( __FILE__ )}/loader/base.rb"
7
+
8
+ def initialize
9
+ @pids = {}
10
+ @t = Thread.new do
11
+ loop do
12
+ sleep 1
13
+ @pids.each do |pid, info|
14
+ begin
15
+ result = Process.waitpid( pid, Process::WNOHANG )
16
+ if result
17
+ @pids.delete pid
18
+ resources = info[:resources]
19
+ next if !resources
20
+
21
+ Slotz::RESERVED[:disk] -= resources[:disk]
22
+ Slotz::RESERVED[:memory] -= resources[:klass.memory]
23
+ end
24
+ rescue Errno::ECHILD
25
+ @pids.delete( pid )
26
+ resources = info[:resources]
27
+ next if !resources
28
+
29
+ Slotz::RESERVED[:disk] -= resources[:disk]
30
+ Slotz::RESERVED[:memory] -= resources[:memory]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ # @param [String] executable
38
+ # Name of the executable Ruby script found in {OptionGroups::Paths#executables}
39
+ # without the '.rb' extension.
40
+ # @param [Hash] options
41
+ # Options to pass to the script -- can be retrieved from `$options`.
42
+ #
43
+ # @return [Integer]
44
+ # PID of the process.
45
+ def load( klass, executable, options = {} )
46
+ if !File.exist?( executable )
47
+ fail "File does not exist: #{executable}"
48
+ end
49
+ executable = File.absolute_path( executable )
50
+
51
+ $options = { execute: false }
52
+ require_relative executable
53
+ klass = Object.const_get( klass )
54
+ resources = Slotz.filter( klass )
55
+
56
+ stdin = options.delete(:stdin)
57
+ stdout = options.delete(:stdout)
58
+ stderr = options.delete(:stderr)
59
+ new_pgroup = options.delete(:new_pgroup)
60
+ daemonize = options.delete(:daemonize)
61
+
62
+ spawn_options = {}
63
+
64
+ if new_pgroup
65
+ spawn_options[:pgroup] = new_pgroup
66
+ end
67
+
68
+ spawn_options[:in] = stdin if stdin
69
+ spawn_options[:out] = stdout if stdout
70
+ spawn_options[:err] = stderr if stderr
71
+
72
+ options[:ppid] = Process.pid
73
+ options[:tmpdir] = Dir.tmpdir
74
+
75
+ encoded_options = Base64.strict_encode64( Marshal.dump( options.merge( execute: true ) ) )
76
+ argv = [executable, encoded_options]
77
+
78
+ # It's very, **VERY** important that we use this argument format as
79
+ # it bypasses the OS shell and we can thus count on a 1-to-1 process
80
+ # creation and that the PID we get will be for the actual process.
81
+ pid = Process.spawn(
82
+ {
83
+ 'SLOTZ_SPAWN_OPTIONS' => Base64.strict_encode64( Marshal.dump( {} ) )
84
+ },
85
+ RbConfig.ruby,
86
+ RUNNER,
87
+ *(argv + [spawn_options])
88
+ )
89
+
90
+ @pids[pid] = {
91
+ klass: klass,
92
+ resources: resources
93
+ }
94
+
95
+ if !daemonize
96
+ begin
97
+ Process.waitpid( pid )
98
+ rescue Interrupt
99
+ exit 0
100
+ end
101
+ end
102
+
103
+ pid
104
+ rescue => e
105
+ p e
106
+ e.backtrace.each do |l|
107
+ p l
108
+ end
109
+ end
110
+
111
+
112
+ end
113
+ end
@@ -1,42 +1,35 @@
1
1
  module Slotz
2
2
 
3
- class Reservation < Module
3
+ module Reservation
4
4
 
5
- attr_reader :disk
6
- attr_reader :memory
7
- attr_reader :cores
8
-
9
- def initialize( provision )
10
- @provision = provision
5
+ def provision( base, resources )
6
+ base.class_eval do
7
+ self.class.attr_accessor :_slotz_disk
8
+ self.class.attr_accessor :_slotz_memory
9
+ self.class.attr_accessor :_slotz_cores
11
10
 
12
- ObjectSpace.define_finalizer(self, proc {
13
- Slotz::RESERVED[:disk] -= self.class.disk
14
- Slotz::RESERVED[:memory] -= self.class.memory
15
- Slotz::RESERVED[:cores] -= self.class.cores
16
- })
17
- end
11
+ def initialize(*)
12
+ super
13
+ Slotz.filter self.class
14
+ end
18
15
 
19
- def included( base )
20
- provisions = @provision
21
- base.class_eval do
22
- @disk = provisions[:disk]
23
- def self.disk
24
- @disk
16
+ def available_slots
17
+ System.available_auto self
25
18
  end
26
19
 
27
- @memory = provisions[:memory]
28
- def self.memory
29
- @memory
20
+ def available_slots_on_disk
21
+ System.disk_space_free / self.class._slotz_disk
30
22
  end
31
23
 
32
- @cores = provisions[:cores]
33
- def self.cores
34
- @cores
24
+ def available_slots_in_memory
25
+ System.memory_free / self.class._slotz_memory
35
26
  end
36
27
  end
37
28
 
38
- Slotz.filter base
29
+ base._slotz_disk = resources[:disk]
30
+ base._slotz_memory = resources[:memory]
39
31
  end
32
+ extend self
40
33
 
41
34
  end
42
35
 
@@ -1,4 +1,5 @@
1
1
  require 'vmstat'
2
+ require_relative '../base'
2
3
 
3
4
  module Slotz
4
5
  class System
@@ -7,6 +7,6 @@ end
7
7
  end
8
8
  end
9
9
 
10
- Dir.glob( "#{File.dirname(__FILE__)}/**/*.rb" ).each do |platform|
10
+ Dir.glob( "#{File.dirname(__FILE__)}/platforms/**/*.rb" ).each do |platform|
11
11
  require platform
12
12
  end
data/lib/slotz/system.rb CHANGED
@@ -1,4 +1,3 @@
1
- require_relative 'system/platforms/base'
2
1
  require 'singleton'
3
2
  require 'set'
4
3
 
@@ -32,35 +31,35 @@ class System
32
31
 
33
32
  def initialize
34
33
  @platforms = []
35
- @pids = Set.new
36
- end
37
-
38
- def use( pid )
39
- @pids << pid
40
- pid
41
34
  end
42
35
 
43
36
  # @return [Integer]
44
37
  # Amount of new applications that can be safely run in parallel, currently.
45
38
  # User option will override decision based on system resources.
46
- def available
39
+ def available( application )
47
40
  # Manual mode, user gave us a value.
48
41
  if (max_slots = System.max_slots)
49
42
  max_slots - used
50
43
 
51
44
  # Auto-mode, pick the safest restriction, RAM vs CPU.
52
45
  else
53
- available_auto
46
+ available_auto( application )
54
47
  end
55
48
  end
56
49
 
57
50
  # @return [Integer]
58
51
  # Amount of new applications that can be safely run in parallel, currently.
59
52
  # The decision is based on the available resources alone.
60
- def available_auto( requirements )
61
- [ available_in_memory( requirements[:memory] ),
62
- available_in_cpu( requirements[:cores] ),
63
- available_in_disk( requirements[:disk] ) ].min
53
+ def available_auto( application )
54
+ memory_slots = available_in_memory( application.class._slotz_memory )
55
+ # cpu_slots = available_in_cpu( application.cpu_requirement )
56
+ disk_slots = unallocated_disk_space / application.class._slotz_disk
57
+
58
+ [
59
+ memory_slots,
60
+ # cpu_slots,
61
+ disk_slots
62
+ ].min
64
63
  end
65
64
 
66
65
  # @return [Integer]
@@ -95,30 +94,13 @@ class System
95
94
  # Well, they may not be really available, other stuff on the machine could
96
95
  # be using them to a considerable extent, but we can only do so much.
97
96
  def available_in_cpu( cores )
98
- @system.cpu_count - used
99
- end
100
-
101
- # @param [Integer] pid
102
- #
103
- # @return [Integer]
104
- # Remaining memory for the scan, in bytes.
105
- def remaining_memory_for( pid )
106
- [memory_size - @system.memory_for_process_group( pid ), 0].max
97
+ cpu_count - used
107
98
  end
108
99
 
109
100
  # @return [Integer]
110
101
  # Amount of memory (in bytes) available for future scans.
111
102
  def unallocated_memory
112
- # Available memory right now.
113
- available_mem = @system.memory_free
114
-
115
- # Remove allocated memory to figure out how much we can really spare.
116
- @pids.each do |pid|
117
- # Mark the remaining allocated memory as unavailable.
118
- available_mem -= remaining_memory_for( pid )
119
- end
120
-
121
- available_mem
103
+ memory_free
122
104
  end
123
105
 
124
106
  # @param [Integer] pid
@@ -133,25 +115,7 @@ class System
133
115
  # Amount of disk space (in bytes) available for future scans.
134
116
  def unallocated_disk_space
135
117
  # Available space right now.
136
- available_space = @system.disk_space_free
137
-
138
- # # Remove allocated space to figure out how much we can really spare.
139
- # @pids.each do |pid|
140
- # # Mark the remaining allocated space as unavailable.
141
- # available_space -= remaining_disk_space_for( pid )
142
- # end
143
-
144
- available_space
145
- end
146
-
147
- def disk_space_requirement( application )
148
- application.disk.to_i
149
- end
150
-
151
- # @return [Fixnum]
152
- # Amount of memory (in bytes) to allocate.
153
- def memory_requirement( application )
154
- application.memory.to_i
118
+ disk_space_free
155
119
  end
156
120
 
157
121
  # @return [Integer]
@@ -160,15 +124,6 @@ class System
160
124
  platform.memory_free
161
125
  end
162
126
 
163
- # @param [Integer] pgid
164
- # Process group ID.
165
- #
166
- # @return [Integer]
167
- # Amount of RAM in bytes used by the given GPID.
168
- def memory_for_process_group( pgid )
169
- platform.memory_for_process_group( pgid )
170
- end
171
-
172
127
  # @return [Integer]
173
128
  # Amount of free disk space in bytes.
174
129
  def disk_space_free
@@ -216,7 +171,6 @@ class System
216
171
 
217
172
  # @private
218
173
  def reset
219
- @pids.clear
220
174
  @cpu_count = nil
221
175
  @platform = nil
222
176
  end
data/lib/slotz/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Slotz
2
- VERSION = '0.1'
2
+ VERSION = '0.1.3'
3
3
  end
data/lib/slotz.rb CHANGED
@@ -2,6 +2,7 @@ module Slotz
2
2
  require_relative 'slotz/version'
3
3
  require_relative 'slotz/options'
4
4
  require_relative 'slotz/system'
5
+ require_relative 'slotz/loader'
5
6
  require_relative 'slotz/reservation'
6
7
 
7
8
  RESERVED = {
@@ -10,26 +11,51 @@ module Slotz
10
11
  cores: 0
11
12
  }
12
13
 
13
- def self.filter( reservation )
14
- # fail 'Max utilization.' if Slotz::System.max_utilization?
14
+ def self.utilization
15
+ disk = RESERVED[:disk].to_f / System.disk_space_free
16
+ memory = RESERVED[:memory].to_f / System.memory_free
17
+ [disk, memory].max
18
+ end
19
+
20
+ def self.filter( klass )
21
+ if !klass.is_a?( Class ) && !klass.is_a?( Module )
22
+ klass = Object.const_get( klass.to_s )
23
+ end
24
+
25
+ return if !klass.respond_to?(:_slotz_disk) || !klass.respond_to?(:_slotz_memory)
15
26
 
16
- if RESERVED[:disk] + reservation.disk <= System.disk_space_free
17
- RESERVED[:disk] += reservation.disk
27
+ disk = klass._slotz_disk
28
+ memory = klass._slotz_memory
29
+
30
+ if RESERVED[:disk].to_i + disk.to_i <= System.disk_space_free.to_i
31
+ RESERVED[:disk] += disk.to_i
18
32
  else
19
33
  fail 'Not enough disk resources.'
20
34
  end
21
35
 
22
- if RESERVED[:memory] + reservation.memory <= System.memory_free
23
- RESERVED[:memory] += reservation.memory
36
+ if RESERVED[:memory].to_i + memory.to_i <= System.memory_free.to_i
37
+ RESERVED[:memory] += memory.to_i
24
38
  else
25
39
  fail 'Not enough memory resources.'
26
40
  end
27
41
 
28
- # if RESERVED[:cores] + requirements[:cores] <= System.cores
29
- # RESERVED[:cores] += requirements[:cores]
42
+ # if RESERVED[:cores].to_i + cores.to_i <= System.cores.to_i
43
+ # RESERVED[:cores] += cores.to_i
30
44
  # else
31
- # fail 'Not enough CPU resources.'
45
+ # fail 'Not enough memory resources.'
32
46
  # end
47
+
48
+ ObjectSpace.define_finalizer(klass, proc {
49
+ Slotz::RESERVED[:disk] -= klass._slotz_disk
50
+ Slotz::RESERVED[:memory] -= klass._slotz_memory
51
+ # Slotz::RESERVED[:cores] -= klass._slotz_cores
52
+ })
53
+
54
+ {
55
+ disk: disk,
56
+ memory: memory
57
+ # cores: 0
58
+ }
33
59
  end
34
60
 
35
61
  end
data/slotz.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.homepage = 'https://github.com/qadron/cuboid'
12
12
  s.email = 'tasos.laskos@gmail.com'
13
13
  s.authors = [ 'Tasos Laskos' ]
14
- s.licenses = ['MIT']
14
+ s.licenses = ['MPL v2']
15
15
 
16
16
  s.files += Dir.glob( 'config/**/**' )
17
17
  s.files += Dir.glob( 'lib/**/**' )
@@ -31,6 +31,9 @@ Gem::Specification.new do |s|
31
31
  # latest one.
32
32
  s.add_dependency 'bundler'
33
33
 
34
+ s.add_dependency 'ostruct'
35
+ s.add_dependency 'base64'
36
+
34
37
  s.add_dependency 'concurrent-ruby'
35
38
  s.add_dependency 'vmstat', '~> 2.3.1'
36
39
  s.add_dependency 'sys-proctable', '~> 1.3.0'
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'slotz/system'
2
3
 
3
4
  describe Slotz::System::Platforms::Linux do
4
5
  it_should_behave_like 'Slotz::System::Platforms::Mixins::Unix'
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'slotz/system'
2
3
 
3
4
  describe Slotz::System::Platforms::OSX do
4
5
  it_should_behave_like 'Slotz::System::Platforms::Mixins::Unix'
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'slotz/system'
2
3
 
3
4
  describe Slotz::System::Platforms::Windows, if: Slotz::System.windows? do
4
5
  it_should_behave_like 'Slotz::System::Platforms::Base'
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'slotz/system'
2
3
 
3
4
  describe Slotz::System do
4
5
  subject { described_class.instance }
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'slotz/system'
2
3
 
3
4
  describe Slotz do
4
5
  subject { system }
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,7 @@ require_relative 'support/helpers/paths'
2
2
 
3
3
  Dir.glob( "#{support_path}/{lib,helpers,shared,factories}/**/*.rb" ).each { |f| require f }
4
4
 
5
+
5
6
  RSpec::Core::MemoizedHelpers.module_eval do
6
7
  alias to should
7
8
  alias to_not should_not
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slotz
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tasos Laskos
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-10-19 00:00:00.000000000 Z
10
+ date: 2026-01-04 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: awesome_print
@@ -38,6 +37,34 @@ dependencies:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
39
  version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: ostruct
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: base64
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
41
68
  - !ruby/object:Gem::Dependency
42
69
  name: concurrent-ruby
43
70
  requirement: !ruby/object:Gem::Requirement
@@ -85,13 +112,15 @@ email: tasos.laskos@gmail.com
85
112
  executables: []
86
113
  extensions: []
87
114
  extra_rdoc_files:
88
- - README.md
89
115
  - LICENSE.md
116
+ - README.md
90
117
  files:
91
118
  - Gemfile
92
119
  - LICENSE.md
93
120
  - README.md
94
121
  - lib/slotz.rb
122
+ - lib/slotz/loader.rb
123
+ - lib/slotz/loader/base.rb
95
124
  - lib/slotz/options.rb
96
125
  - lib/slotz/reservation.rb
97
126
  - lib/slotz/system.rb
@@ -107,17 +136,16 @@ files:
107
136
  - spec/slotz/system/platforms/linux_spec.rb
108
137
  - spec/slotz/system/platforms/osx_spec.rb
109
138
  - spec/slotz/system/platforms/windows_spec.rb
110
- - spec/slotz/system/slotz_spec.rb
111
139
  - spec/slotz/system_spec.rb
140
+ - spec/slotz_spec.rb
112
141
  - spec/spec_helper.rb
113
142
  - spec/support/helpers/paths.rb
114
143
  - spec/support/shared/system/platforms/base.rb
115
144
  - spec/support/shared/system/platforms/mixins/unix.rb
116
145
  homepage: https://github.com/qadron/cuboid
117
146
  licenses:
118
- - MIT
147
+ - MPL v2
119
148
  metadata: {}
120
- post_install_message:
121
149
  rdoc_options:
122
150
  - "--charset=UTF-8"
123
151
  require_paths:
@@ -133,17 +161,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
161
  - !ruby/object:Gem::Version
134
162
  version: '0'
135
163
  requirements: []
136
- rubygems_version: 3.4.22
137
- signing_key:
164
+ rubygems_version: 3.6.9
138
165
  specification_version: 4
139
166
  summary: An application-centric, decentralised and distributed computing solution.
140
167
  test_files:
141
- - spec/slotz/system/slotz_spec.rb
142
- - spec/slotz/system/platforms/osx_spec.rb
143
168
  - spec/slotz/system/platforms/linux_spec.rb
169
+ - spec/slotz/system/platforms/osx_spec.rb
144
170
  - spec/slotz/system/platforms/windows_spec.rb
145
171
  - spec/slotz/system_spec.rb
172
+ - spec/slotz_spec.rb
173
+ - spec/spec_helper.rb
174
+ - spec/support/helpers/paths.rb
146
175
  - spec/support/shared/system/platforms/base.rb
147
176
  - spec/support/shared/system/platforms/mixins/unix.rb
148
- - spec/support/helpers/paths.rb
149
- - spec/spec_helper.rb