Module: LoopDance::Commands

Included in:
Dancer
Defined in:
lib/loop_dance/commands.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/loop_dance/commands.rb', line 17

def timeout
  @timeout
end

Class Method Details

.super_attr_accessor(*syms) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/loop_dance/commands.rb', line 5

def self.super_attr_accessor(*syms)
  attr_writer *syms
  syms.each do |sym|
    next if sym.is_a?(Hash)
    class_eval  "def #{sym} value = (nil;flag=true); if flag; return @#{sym}; else; @#{sym} = value; end ; end"
  end
end

Instance Method Details

#danceObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/loop_dance/commands.rb', line 31

def dance
  loop_init
  if @tasks.empty?
    log "No tasks defined.", true
  else
    while (@run) do
      @tasks.each_with_index do |task, index|
        break unless @run
        if task.time_to_run?
          log "Run task ##{index} for every #{task.interval.inspect}"
          task.run 
        end
        break unless @run
      end
        log "Sleep for #{@timeout.inspect}"
        sleep @timeout.to_i if @run
      end
    end
  log "shutting down", true
end

#every(interval, &block) ⇒ Object



24
25
26
27
28
29
# File 'lib/loop_dance/commands.rb', line 24

def every( interval, &block )
  @tasks = [] unless @tasks
  @tasks << LoopDance::Task.new( self, interval, &block )
  find_minimal_timeout interval
  @maximal_timeout = interval if !@maximal_timeout || @maximal_timeout < interval
end

#inherited(subclass) ⇒ Object



20
21
22
# File 'lib/loop_dance/commands.rb', line 20

def inherited(subclass)
  subclass.trap_signals = true
end

#log(text, forced = false) ⇒ Object



60
61
62
# File 'lib/loop_dance/commands.rb', line 60

def log(text, forced=false)
  puts "#{Time.now} #{self}: #{text}" if forced || !mute_log
end

#pid_fileObject



56
57
58
# File 'lib/loop_dance/commands.rb', line 56

def pid_file
  "log/#{name.underscore}.pid"
end


64
65
66
67
68
69
70
71
72
73
# File 'lib/loop_dance/commands.rb', line 64

def print_status
  puts  "#{self}: timeout: #{@timeout.inspect}, status: #{self.controller.running? ? 'running' : 'stopped'}\n"
  if tasks.empty?
    puts "  no tasks defined"
  else
    tasks.each_with_index do |task,index|
      puts "  Task ##{index} runs every #{task.interval.inspect}"
    end
  end
end

#stop_meObject



52
53
54
# File 'lib/loop_dance/commands.rb', line 52

def stop_me
  @run = false
end