Class: Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/sfdc_elf/scheduler.rb

Overview

Handel when to schedule the next process based on the poll interval specified. The poll interval provided has to be in seconds.

Constant Summary collapse

LOG_KEY =
'SFDC - Scheduler'

Instance Method Summary collapse

Constructor Details

#initialize(poll_interval_in_seconds) ⇒ Scheduler

Returns a new instance of Scheduler.



8
9
10
11
# File 'lib/logstash/inputs/sfdc_elf/scheduler.rb', line 8

def initialize(poll_interval_in_seconds)
  @logger = Cabin::Channel.get(LogStash)
  @poll_interval_in_seconds = poll_interval_in_seconds
end

Instance Method Details

#schedule(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstash/inputs/sfdc_elf/scheduler.rb', line 19

def schedule(&block)
  # Grab the current time and one @interval to it so that the while loop knows when it need to compute again.
  next_schedule_time = Time.now + @poll_interval_in_seconds

  # sleep until start time
  loop do
    block.call

    # Depending on the next_schedule_time and the time taking the compute the code above,
    # sleep this loop and adjust the next_schedule_time.
    @logger.info("#{LOG_KEY}: next_schedule_time = #{next_schedule_time}")
    next_schedule_time = stall_schedule(next_schedule_time)
  end
end

#stall_schedule(next_schedule_time) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/logstash/inputs/sfdc_elf/scheduler.rb', line 55

def stall_schedule(next_schedule_time)
  current_time = Time.now
  @logger.info("#{LOG_KEY}: time before sleep  = #{current_time}")

  # Example 2 case from above.
  if current_time > next_schedule_time
    @logger.info("#{LOG_KEY}: missed next schedule time, proceeding to next task without sleeping")
    next_schedule_time += @poll_interval_in_seconds while current_time > next_schedule_time

    # Example 1 case from above.
  else
    @logger.info("#{LOG_KEY}: sleeping for #{(next_schedule_time - current_time)} seconds")
    sleep(next_schedule_time - current_time)
    next_schedule_time += @poll_interval_in_seconds
  end
  @logger.info("#{LOG_KEY} time after sleep   = #{Time.now}")
  next_schedule_time
end