class PSched::Operation

Implements a recurring operation. @example General usage. Note the `sleep` call.

op = PSched::Operation.new(0.5)
op.start(10) do |i|
  puts "Ping #{i}"
end

Signal.trap("SIGINT") do
  print "Stopping recurring process..."
  op.stop
  puts "done!"
end

sleep(0.2) while op.active?  # THIS IS IMPORTANT!!!

@author Paolo Bosetti

Attributes

start_time[R]

@!attribute [r] tet

The Task Execution Time

@!attribute [r] step

The time step in seconds

@!attribute [r] start_time

The time at the beginning of the current Operation
step[R]

@!attribute [r] tet

The Task Execution Time

@!attribute [r] step

The time step in seconds

@!attribute [r] start_time

The time at the beginning of the current Operation
strict_timing[RW]

@!attribute [rw] strict_timing

If true, raise a {RealTimeError} when `TET >= step`
(default to `false`)
tet[R]

@!attribute [r] tet

The Task Execution Time

@!attribute [r] step

The time step in seconds

@!attribute [r] start_time

The time at the beginning of the current Operation

Public Class Methods

new(step) click to toggle source

Initializer @param [Numeric] step the timestep in seconds

# File lib/psched.rb, line 97
def initialize(step)
  @step          = step
  @active        = false
  @lock          = false
  @strict_timing = false
  @tet           = 0
end
prioritize(nice = -20) click to toggle source

Changes scheduling priority of the current process @param [Fixnum] nice sets the nice level (-20..+20) @todo better return message and error management

# File lib/psched.rb, line 77
def self.prioritize(nice = -20)
  result = PSched.setpriority(:prio_process,0,nice)
  puts "Setting max priority: #{result == 0 ? 'success' : 'failure (missing sudo?)'}"
end

Public Instance Methods

active?() click to toggle source

Tells id the recurring operation is active or not. @return [Boolean] the status of the recurring alarm operation

# File lib/psched.rb, line 122
def active?; @active; end
schedule() click to toggle source

Updates scheduling of pending alarms. @note Usually, there's no need to call this, since {#step=} automatically

calls it after having set the +@step+ attribute.
# File lib/psched.rb, line 115
def schedule
  usecs = @step * 1E6
  PSched::ualarm(usecs, usecs)
end
start(n_iter=nil) { |i, now - start_time, tet| ... } click to toggle source

Starts a recurring operation, described by the passed block. If the block returns the symbol :stop, the recurrin operation gets disabled. @param [Fixnum,nil] n_iter the maximum number of iterations.

If nil it loops indefinitedly

@yieldparam [Fixnum] i the number of elapsed iterations @yieldparam [Float] t the time elapsed since the beginning of current Operation @yieldparam [Float] tet the Task Execution Time of previous step @raise [ArgumentError] unless a block is given @raise [RealTimeError] if TET exceeds @step

# File lib/psched.rb, line 134
def start(n_iter=nil)
  raise ArgumentError, "Need a block!" unless block_given?
  @active = true
  i = 0
  @start_time = Time.now
  Signal.trap(:ALRM) do
    # If there is still a pending step, raises an error containing
    # information about the CURRENT step
    if @lock then
      if @strict_timing
        @lock = false
        raise RealTimeError.new({:tet => @tet, :step => @step, :i => i, :time => Time.now})
      end
    else
      start = Time.now
      @lock = true
      result = yield(i, Time.now - @start_time, @tet)
      i += 1
      self.stop if (n_iter and i >= n_iter)
      self.stop if (result.kind_of? Symbol and result == :stop)
      @tet = Time.now - start
      @lock = false
    end
  end
  self.schedule
end
step=(secs) click to toggle source

Sets the time step (in seconds) and reschedule pending alarms. @param [Numeric] secs Timestep in seconds

# File lib/psched.rb, line 107
def step=(secs)
  @step = secs
  self.schedule
end
stop() click to toggle source

Stops the recurring process by resetting alarm and disabling management of `SIGALRM` signal.

# File lib/psched.rb, line 163
def stop
  PSched::ualarm(0, 0)
  Signal.trap(:ALRM, "DEFAULT")
  @active = false
  @lock = false
end