class TimeScheduler::Schedule

Constants

NICE_TIME

Public Class Methods

active?() click to toggle source
# File lib/time_scheduler/scheduler.rb, line 9
def active?
  ! @@pause
end
new() click to toggle source
# 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
resume() click to toggle source
# File lib/time_scheduler/scheduler.rb, line 17
def resume
  @@pause  =  false
end
suspend() click to toggle source
# File lib/time_scheduler/scheduler.rb, line 13
def suspend
  @@pause  =  true
end

Public Instance Methods

cancel() click to toggle source
# File lib/time_scheduler/scheduler.rb, line 31
def cancel
  @signal_queue.push( nil )
end
setup( **option, &block ) click to toggle source
# 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 ) click to toggle source
# 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 |time|
        @schedule_mutex.synchronize do
          @action.call( time )
        end
      end
    end
    nil
  end
  nil
end
wait_once( **option ) click to toggle source
# 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 ) click to toggle source
# 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