Class: ThreadWithTask
- Defined in:
- lib/mrpin/core/threads/thread_with_task.rb
Overview
todo: review and mb use scheduler
Instance Method Summary collapse
-
#initialize(sleep_time, name, delay = 0) ⇒ ThreadWithTask
constructor
A new instance of ThreadWithTask.
- #run(&block) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(sleep_time, name, delay = 0) ⇒ ThreadWithTask
Returns a new instance of ThreadWithTask.
5 6 7 8 9 10 11 12 |
# File 'lib/mrpin/core/threads/thread_with_task.rb', line 5 def initialize(sleep_time, name, delay = 0) # in ms @sleep_time = sleep_time * 1000.0 # in seconds @delay = delay @name = name @thread = nil end |
Instance Method Details
#run(&block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mrpin/core/threads/thread_with_task.rb', line 15 def run(&block) logger = AppInfo.instance.logger @thread = Thread.new do sleep @delay logger.info "[THREAD STARTED]:\t #{@name}" loop do begin time_now_ms = Time.now.to_ms next_tick_time = time_now_ms + @sleep_time block.call sleep_time_seconds = (next_tick_time - Time.now.to_ms) / 1000.0 sleep_time_seconds = [sleep_time_seconds, 0.01].max sleep(sleep_time_seconds) rescue Exception => e AppInfo.instance.on_server_error("Error on #{@name} thread: #{e}", e) sleep (0.05) end #begin end #loop end # thread @thread.name = 'thread_with_task' nil end |
#stop ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/mrpin/core/threads/thread_with_task.rb', line 53 def stop return if @thread.nil? @thread.stop @thread = nil nil end |