Module: PoolParty::Scheduler

Included in:
Remoter::InstanceMethods, Remoting
Defined in:
lib/poolparty/scheduler.rb

Overview

Scheduler class

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#taskerObject (readonly)

Returns the value of attribute tasker.



34
35
36
# File 'lib/poolparty/scheduler.rb', line 34

def tasker
  @tasker
end

Instance Method Details

#_taskerObject

Get the tasks or ScheduleTasks



36
37
38
# File 'lib/poolparty/scheduler.rb', line 36

def _tasker
  @_tasker ||= ScheduleTasks.new
end

#add_task(&blk) ⇒ Object

Add a task to the new threaded block



40
41
42
# File 'lib/poolparty/scheduler.rb', line 40

def add_task(&blk)
  _tasker.push blk
end

#daemonizeObject

Daemonize the process



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/poolparty/scheduler.rb', line 53

def daemonize
  PoolParty.message "Daemonizing..."
  
  pid = fork do
    Signal.trap('HUP', 'IGNORE') # Don't die upon logout
    File.open("/dev/null", "r+") do |devnull|
      $stdout.reopen(devnull)
      $stderr.reopen(devnull)
      $stdin.reopen(devnull) unless @use_stdin
    end
    yield if block_given?
  end
  Process.detach(pid)
  pid
end

#intervalObject

Grab the polling_time



44
45
46
# File 'lib/poolparty/scheduler.rb', line 44

def interval
  @interval ||= Application.polling_time
end

#run_thread_listObject



88
89
90
# File 'lib/poolparty/scheduler.rb', line 88

def run_thread_list      
  run_threads
end

#run_thread_loop(opts = {}, &blk) ⇒ Object

Run the loop and wait the amount of time between running the tasks You can send it daemonize => true and it will daemonize



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/poolparty/scheduler.rb', line 70

def run_thread_loop(opts={}, &blk)
  block = lambda {        
    loop do
      begin
        yield if block_given?
        add_task { Signal.trap("INT") { exit } }            
        run_thread_list
        PoolParty.message "Waiting: #{interval}"
        wait interval
      rescue Exception => e
        Process.kill("INT", Process.pid)
      end
    end
  }
  # Run the tasks
  opts[:daemonize] ? daemonize(&block) : block.call   
end

#run_threadsObject Also known as: run_tasks

Run the threads



48
49
50
# File 'lib/poolparty/scheduler.rb', line 48

def run_threads
  _tasker.run
end