Guest User

Untitled

a guest
Oct 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.90 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Copyright 2012 IllFonic, LLC.
  3.  
  4. require 'thread'
  5.  
  6. module Dossier
  7.     class AsyncSubCommand < SubCommand
  8.         def main arguments
  9.             @exiting = false
  10.             @thread = Thread.new do
  11.                 Thread.current.abort_on_exception = true
  12.  
  13.                 init arguments
  14.                 until @exiting do
  15.                     now = Time.now.to_f
  16.                     tick now - (@last_time || now)
  17.                     @last_time = now
  18.                 end
  19.                 shutdown
  20.             end
  21.  
  22.             # Make sure we return self
  23.             self
  24.         end
  25.  
  26.         def alive?
  27.             @thread.alive?
  28.         end
  29.  
  30.         def die!
  31.             @exiting = true
  32.             @thread.wakeup unless @thread.nil? or !@thread.alive? or @thread == Thread.current
  33.         end
  34.  
  35.         def exiting?
  36.             @exiting
  37.         end
  38.  
  39.         def join
  40.             @thread.join unless @thread.nil? or !@thread.alive?
  41.         end
  42.  
  43.         def die_and_join!
  44.             @exiting = true
  45.             unless @thread.nil? or !@thread.alive? or @thread == Thread.current
  46.                 @thread.wakeup
  47.                 @thread.join
  48.             end
  49.         end
  50.     end
  51. end
Add Comment
Please, Sign In to add comment