Class: Gaspar

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/gaspar.rb,
lib/gaspar/version.rb

Constant Summary collapse

VERSION =
"0.1.3"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.singletonObject (readonly)

Returns the value of attribute singleton.



16
17
18
# File 'lib/gaspar.rb', line 16

def singleton
  @singleton
end

Instance Attribute Details

#driftObject (readonly)

Returns the value of attribute drift.



11
12
13
# File 'lib/gaspar.rb', line 11

def drift
  @drift
end

#schedulerObject (readonly)

Returns the value of attribute scheduler.



11
12
13
# File 'lib/gaspar.rb', line 11

def scheduler
  @scheduler
end

Class Method Details

.configure(options = {}, &block) ⇒ Object

Public: Configure Gaspar

options - an options hash



21
22
23
24
# File 'lib/gaspar.rb', line 21

def configure(options = {}, &block)
  @singleton ||= new(options, &block)
  self
end

.configured?Boolean

Public: Get whether Gaspar has been configured yet or not

Returns: [Boolean]

Returns:

  • (Boolean)


29
30
31
# File 'lib/gaspar.rb', line 29

def configured?
  !@singleton.nil?
end

.destruct!Object

Public: Stop processing jobs and destroy the singleton. Returns Gaspar to an unconfigured state.



34
35
36
37
# File 'lib/gaspar.rb', line 34

def destruct!
  retire
  @singleton = nil
end

.log(logger, message) ⇒ Object



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

def log(logger, message)
  logger.debug "[%s] %s" % ["Gaspar".yellow, message] if logger
end

.retireObject



47
48
49
50
# File 'lib/gaspar.rb', line 47

def retire
  return unless @singleton
  @singleton.send(:shutdown!)
end

.start!(redis) ⇒ Object

Public: Execute the configuration and start processing jobs.

redis - The redis instance to use for synchronization



42
43
44
45
# File 'lib/gaspar.rb', line 42

def start!(redis)
  raise "Gaspar#configure has not been called, or did not succeed" if @singleton.nil?
  @singleton.send(:start!, redis) if @singleton
end

Instance Method Details

#after_each(&block) ⇒ Object



65
66
67
# File 'lib/gaspar.rb', line 65

def after_each(&block)
  self.class.set_callback :run, :after, &block
end

#around_each(&block) ⇒ Object



69
70
71
# File 'lib/gaspar.rb', line 69

def around_each(&block)
  self.class.set_callback :run, :around, &block
end

#before_each(&block) ⇒ Object



61
62
63
# File 'lib/gaspar.rb', line 61

def before_each(&block)
  self.class.set_callback :run, :before, &block
end

#can_run_if(&block) ⇒ Object



57
58
59
# File 'lib/gaspar.rb', line 57

def can_run_if(&block)
  @can_run_if = block
end

#cron(timing, *args, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/gaspar.rb', line 93

def cron(timing, *args, &block)
  options = args.extract_options!
  cron = Rufus::CronLine.new(timing)
  next_fire = cron.next_time
  next_next_fire = cron.next_time(next_fire + 0.001)

  options[:period] = next_next_fire.to_i - next_fire.to_i
  schedule :cron, timing, args, options, &block
end

#every(timing, *args, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gaspar.rb', line 73

def every(timing, *args, &block)
  options = args.extract_options!

  # In order to make sure that jobs are executed at the same time regardless of who runs them
  # we quantitize the start time to the next-nearest time slice. This more closely emulates
  # cron-style behavior.
  if timing.is_a? String
    seconds = Rufus.parse_duration_string(timing)
  else
    seconds = timing.to_i
  end
  now = Time.now.to_i - drift
  start_at = Time.at( now + (seconds - (now % seconds)) )

  options = options.merge(:first_at => start_at)
  options[:period] = seconds

  schedule :every, timing, args, options, &block
end