Class: ServiceThread
- Inherits:
-
Object
- Object
- ServiceThread
- Defined in:
- lib/buzzcore/thread_utils.rb
Overview
How to use :
class Worker < ServiceThread def initialize super(:name => ‘worker’,:auto_start => true) end
def starting # startup code end
def running # repeated code, implicitly looped. set self.stopping = true to quit thread end
def stopping # clean up code end end
logger.info “worker #? ‘has’:‘hasn”t’ started and ? ‘has’:‘hasn”t’ stopped”
Instance Attribute Summary collapse
-
#is_started ⇒ Object
readonly
Returns the value of attribute is_started.
-
#is_stopped ⇒ Object
readonly
Returns the value of attribute is_stopped.
-
#is_stopping ⇒ Object
Returns the value of attribute is_stopping.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #gentle_stop ⇒ Object
-
#initialize(aOptions) ⇒ ServiceThread
constructor
if inheriting from ServiceThread and overriding initialize, remember to call super(aOptions) and that this method may start the thread, so any setup must be done before super, not after.
- #main_proc ⇒ Object
- #random_word(min, max) ⇒ Object
- #running ⇒ Object
- #start ⇒ Object
- #starting ⇒ Object
- #stop ⇒ Object
- #stopping ⇒ Object
Constructor Details
#initialize(aOptions) ⇒ ServiceThread
if inheriting from ServiceThread and overriding initialize, remember to call super(aOptions) and that this method may start the thread, so any setup must be done before super, not after
637 638 639 640 641 642 643 644 645 646 |
# File 'lib/buzzcore/thread_utils.rb', line 637 def initialize(aOptions) @options = aOptions @thread = nil @name = aOptions[:name] || random_word(8,8) if not @logger = aOptions[:logger] @logger = Logger.new(STDERR) @logger.level = Logger::DEBUG end self.start() if aOptions[:auto_start] end |
Instance Attribute Details
#is_started ⇒ Object (readonly)
Returns the value of attribute is_started.
626 627 628 |
# File 'lib/buzzcore/thread_utils.rb', line 626 def is_started @is_started end |
#is_stopped ⇒ Object (readonly)
Returns the value of attribute is_stopped.
626 627 628 |
# File 'lib/buzzcore/thread_utils.rb', line 626 def is_stopped @is_stopped end |
#is_stopping ⇒ Object
Returns the value of attribute is_stopping.
625 626 627 |
# File 'lib/buzzcore/thread_utils.rb', line 625 def is_stopping @is_stopping end |
#logger ⇒ Object
Returns the value of attribute logger.
625 626 627 |
# File 'lib/buzzcore/thread_utils.rb', line 625 def logger @logger end |
#name ⇒ Object
Returns the value of attribute name.
625 626 627 |
# File 'lib/buzzcore/thread_utils.rb', line 625 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
626 627 628 |
# File 'lib/buzzcore/thread_utils.rb', line 626 def @options end |
Instance Method Details
#gentle_stop ⇒ Object
699 700 701 |
# File 'lib/buzzcore/thread_utils.rb', line 699 def gentle_stop @is_stopping = true end |
#main_proc ⇒ Object
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'lib/buzzcore/thread_utils.rb', line 663 def main_proc begin @is_stopped = false @is_started = false self.starting() @is_started = true @is_stopping = false while !@is_stopping do running(); logger.debug { "ServiceThread running loop: @is_stopping="+@is_stopping.to_s } end rescue Exception => e @is_stopping = true logger.warn { "Thread #{@name} #{e.inspect} exception in Starting() or Running()" } logger.warn { e.backtrace } end begin stopping() rescue Exception => e logger.warn { "Thread #{@name} #{e.inspect} exception in stopping()" } logger.warn { e.backtrace } end logger.info { "Thread #{@name} dropped out" } @is_stopped = true end |
#random_word(min, max) ⇒ Object
628 629 630 631 632 633 |
# File 'lib/buzzcore/thread_utils.rb', line 628 def random_word(min,max) len = min + rand(max-min+1) result = ' '*len (len-1).downto(0) {|i| result[i] = (?a + rand(?z-?a+1)).chr} return result end |
#running ⇒ Object
693 694 |
# File 'lib/buzzcore/thread_utils.rb', line 693 def running end |
#start ⇒ Object
648 649 650 651 652 |
# File 'lib/buzzcore/thread_utils.rb', line 648 def start raise Exception.new("ServiceThread already started") if @thread @thread = Thread.new() { main_proc() } #Thread.pass unless @is_started or @is_stopped # no timeout ! end |
#starting ⇒ Object
690 691 |
# File 'lib/buzzcore/thread_utils.rb', line 690 def starting end |
#stop ⇒ Object
703 704 705 706 |
# File 'lib/buzzcore/thread_utils.rb', line 703 def stop @is_stopping = true @thread.exit unless !@thread or (@thread.join(0) and not @thread.alive?) end |
#stopping ⇒ Object
696 697 |
# File 'lib/buzzcore/thread_utils.rb', line 696 def stopping end |