Class: TimeScheduler::Schedule
- Inherits:
-
Object
- Object
- TimeScheduler::Schedule
- Defined in:
- lib/time_scheduler/scheduler.rb
Constant Summary collapse
- NICE_TIME =
0.001
Class Method Summary collapse
Instance Method Summary collapse
- #cancel ⇒ Object
-
#initialize ⇒ Schedule
constructor
A new instance of Schedule.
- #setup(**option, &block) ⇒ Object
- #wait_each(**option, &block) ⇒ Object
- #wait_once(**option) ⇒ Object
- #wait_reset(**option, &block) ⇒ Object
Constructor Details
#initialize ⇒ Schedule
Returns a new instance of Schedule.
22 23 24 25 26 27 28 29 |
# File 'lib/time_scheduler/scheduler.rb', line 22 def initialize @schedule_thread = nil @schedule_mutex = ::Mutex.new @signal_queue = ::Queue.new @@pause = false @@EventQueue ||= TimeScheduler::EventQueue.new end |
Class Method Details
.active? ⇒ Boolean
9 10 11 |
# File 'lib/time_scheduler/scheduler.rb', line 9 def active? ! @@pause end |
.resume ⇒ Object
17 18 19 |
# File 'lib/time_scheduler/scheduler.rb', line 17 def resume @@pause = false end |
.suspend ⇒ Object
13 14 15 |
# File 'lib/time_scheduler/scheduler.rb', line 13 def suspend @@pause = true end |
Instance Method Details
#cancel ⇒ Object
31 32 33 |
# File 'lib/time_scheduler/scheduler.rb', line 31 def cancel @signal_queue.push( nil ) end |
#setup(**option, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/time_scheduler/scheduler.rb', line 35 def setup( **option, &block ) @option = option @action = option[:action] || block || @action opt = {} @option.each do |k, v| if [:at, :cron, :year, :month, :day, :wday, :hour, :min, :sec, :msec].include?(k) opt[k] = v end end @time_cursor = TimeCursor.new( **opt ) @nice_time = option[:nice].to_i * NICE_TIME time = Time.now @ident = time.to_i * 1000000 + time.usec end |
#wait_each(**option, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/time_scheduler/scheduler.rb', line 51 def wait_each( **option, &block ) setup( **option, &block ) @schedule_thread = ::Thread.start do while true target_time = @time_cursor.next if target_time.nil? @signal_queue.clear break end @@EventQueue.reserve( target_time + @nice_time, @signal_queue, @ident ) while true time, ident = * @signal_queue.pop break if time.nil? || ident == @ident end if time.nil? @signal_queue.clear break end next if @@pause ::Thread.start( time ) do @schedule_mutex.synchronize do @action.call( time ) end end end nil end nil end |
#wait_once(**option) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/time_scheduler/scheduler.rb', line 87 def wait_once( **option ) setup( **option ) @schedule_thread = ::Thread.start do target_time = @time_cursor.next if target_time.nil? @signal_queue.clear break end @@EventQueue.reserve( target_time + @nice_time, @signal_queue, @ident ) while true time, ident = * @signal_queue.pop break if time.nil? || ident == @ident end if time.nil? @signal_queue.clear end time end @schedule_thread.join time = @schedule_thread.value time end |
#wait_reset(**option, &block) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/time_scheduler/scheduler.rb', line 115 def wait_reset( **option, &block ) if @action setup( **option, &block ) else option[:action] = nil setup( **option ) end time_next = @time_cursor.next @@EventQueue.reserve( time_next + @nice_time, @signal_queue, @ident ) if time_next end |