# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com># # Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:# # The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.require'async/reactor'require_relative'controller'require_relative'statistics'moduleAsync# Manages a reactor within one or more threads.moduleContainerclassGroupdefinitialize@pgid=nil@running={}enddefspawn(*arguments)ifpid=::Process.spawn(*arguments)wait_for(pid)endenddeffork(&block)ifpid=::Process.fork(&block)wait_for(pid)endenddefany?@running.any?enddefwaitwhileself.any?self.wait_oneendrescueInterrupt# If the user interrupts the wait, interrupt the process group and wait for them to finish:self.kill(:INT)# If user presses Ctrl-C again (or something else goes wrong), we will come out and kill(:TERM) in the ensure below:wait_allraiseensureself.closeenddefkill(signal=:INT)::Process.kill(signal,-@pgid)if@pgidenddefstop(graceful=false)ifgracefulself.kill(:INT)wait_allendensureself.closeenddefclosebeginself.kill(:TERM)rescueErrno::EPERM# Sometimes, `kill` code can give EPERM, if any signal couldn't be delivered to a child. This might occur if an exception is thrown in the user code (e.g. within the fiber), and there are other zombie processes which haven't been reaped yet. These should be dealt with below, so it shouldn't be an issue to ignore this condition.end# Clean up zombie processes - if user presses Ctrl-C or for some reason something else blows up, exception would propagate back to caller:wait_allendprotecteddefwait_allwhileself.any?self.wait_onedo|fiber,status|beginfiber.resume(nil)rescueInterrupt# Graceful exit.endendendend# Wait for one process, should only be called when a child process has finished, otherwise would block.defwait_one(flags=0)returnunless@pgid# Wait for processes in this group:pid,status=::Process.wait2(-@pgid,flags)returnifflags&::Process::WNOHANGandpid==nilfiber=@running.delete(pid)if@running.empty?@pgid=nilendifblock_given?yieldfiber,statuselsefiber.resume(status)endenddefwait_for(pid)if@pgid# Set this process as part of the existing process group:::Process.setpgid(pid,@pgid)else# Establishes the child process as a process group leader:::Process.setpgid(pid,0)# Save the process group id:@pgid=pidend@running[pid]=Fiber.current# Return process status:ifresult=Fiber.yieldreturnresultelseraiseInterruptendendendendend