Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # Copyright 2012 IllFonic, LLC.
- require 'thread'
- module Dossier
- class AsyncSubCommand < SubCommand
- def main arguments
- @exiting = false
- @thread = Thread.new do
- Thread.current.abort_on_exception = true
- init arguments
- until @exiting do
- now = Time.now.to_f
- tick now - (@last_time || now)
- @last_time = now
- end
- shutdown
- end
- # Make sure we return self
- self
- end
- def alive?
- @thread.alive?
- end
- def die!
- @exiting = true
- @thread.wakeup unless @thread.nil? or !@thread.alive? or @thread == Thread.current
- end
- def exiting?
- @exiting
- end
- def join
- @thread.join unless @thread.nil? or !@thread.alive?
- end
- def die_and_join!
- @exiting = true
- unless @thread.nil? or !@thread.alive? or @thread == Thread.current
- @thread.wakeup
- @thread.join
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment