Skip to content

Instantly share code, notes, and snippets.

@hone
Created July 27, 2012 22:17
Show Gist options
  • Select an option

  • Save hone/3190730 to your computer and use it in GitHub Desktop.

Select an option

Save hone/3190730 to your computer and use it in GitHub Desktop.

Revisions

  1. hone revised this gist Jul 27, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions worker.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    RUN_TIME = 10
    RESCUE_TIME = 3
    RUN_TIME = 100
    RESCUE_TIME = 10
    TERMFILE = "term.txt"

    class Job
  2. hone revised this gist Jul 27, 2012. 1 changed file with 2 additions and 38 deletions.
    40 changes: 2 additions & 38 deletions worker.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    RUN_TIME = 10
    RESCUE_TIME = 3
    TERM_WAIT_TIME = 7
    TERMFILE = "term.txt"

    class Job
    @@ -15,51 +14,16 @@ def self.perform(run_time, rescue_time)
    end
    end

    class Worker
    def self.work(term_timeout)
    @shutdown = false
    trap('TERM') do
    if @child && !Process.waitpid(@child, Process::WNOHANG)
    Process.kill("TERM", @child)
    (term_timeout.to_f * 10).round.times do |i|
    sleep(0.1)
    return if Process.waitpid(@child, Process::WNOHANG)
    end
    Process.kill("KILL", @child)
    end
    end

    loop do
    break if @shutdown

    if @child = fork
    begin
    Process.waitpid(@child)
    @shutdown = true
    rescue SystemCallError
    nil
    end
    else
    trap('TERM', 'DEFAULT')
    Job.perform(RUN_TIME, RESCUE_TIME)
    @shutdown = true
    end

    @child = nil
    end
    end
    end

    if $0 == __FILE__
    require 'fileutils'

    FileUtils.rm(TERMFILE) if File.exists?(TERMFILE)

    pid = Kernel.fork do
    Worker.work(TERM_WAIT_TIME)
    Job.perform(RUN_TIME, RESCUE_TIME)
    end

    sleep(1)
    sleep(2)

    Process.kill("TERM", pid)
    Process.waitpid(pid)
  3. hone created this gist Jul 27, 2012.
    72 changes: 72 additions & 0 deletions worker.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    RUN_TIME = 10
    RESCUE_TIME = 3
    TERM_WAIT_TIME = 7
    TERMFILE = "term.txt"

    class Job
    def self.perform(run_time, rescue_time)
    sleep(run_time)
    puts "Processed job!"
    rescue SignalException => e
    sleep rescue_time
    File.open(TERMFILE, 'w') do |file|
    file.puts "Caught Signal Exception: #{e.message}"
    end
    end
    end

    class Worker
    def self.work(term_timeout)
    @shutdown = false
    trap('TERM') do
    if @child && !Process.waitpid(@child, Process::WNOHANG)
    Process.kill("TERM", @child)
    (term_timeout.to_f * 10).round.times do |i|
    sleep(0.1)
    return if Process.waitpid(@child, Process::WNOHANG)
    end
    Process.kill("KILL", @child)
    end
    end

    loop do
    break if @shutdown

    if @child = fork
    begin
    Process.waitpid(@child)
    @shutdown = true
    rescue SystemCallError
    nil
    end
    else
    trap('TERM', 'DEFAULT')
    Job.perform(RUN_TIME, RESCUE_TIME)
    @shutdown = true
    end

    @child = nil
    end
    end
    end

    if $0 == __FILE__
    require 'fileutils'

    FileUtils.rm(TERMFILE) if File.exists?(TERMFILE)

    pid = Kernel.fork do
    Worker.work(TERM_WAIT_TIME)
    end

    sleep(1)

    Process.kill("TERM", pid)
    Process.waitpid(pid)

    if File.exists?(TERMFILE) && File.read(TERMFILE).match(/Caught Signal Exception/)
    puts 'SUCCESS!'
    else
    puts 'FAILURE'
    end
    end