Class: Gaspar
- Inherits:
-
Object
- Object
- Gaspar
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/gaspar.rb,
lib/gaspar/version.rb
Constant Summary collapse
- VERSION =
"0.1.3"
Class Attribute Summary collapse
-
.singleton ⇒ Object
readonly
Returns the value of attribute singleton.
Instance Attribute Summary collapse
-
#drift ⇒ Object
readonly
Returns the value of attribute drift.
-
#scheduler ⇒ Object
readonly
Returns the value of attribute scheduler.
Class Method Summary collapse
-
.configure(options = {}, &block) ⇒ Object
Public: Configure Gaspar.
-
.configured? ⇒ Boolean
Public: Get whether Gaspar has been configured yet or not.
-
.destruct! ⇒ Object
Public: Stop processing jobs and destroy the singleton.
- .log(logger, message) ⇒ Object
- .retire ⇒ Object
-
.start!(redis) ⇒ Object
Public: Execute the configuration and start processing jobs.
Instance Method Summary collapse
- #after_each(&block) ⇒ Object
- #around_each(&block) ⇒ Object
- #before_each(&block) ⇒ Object
- #can_run_if(&block) ⇒ Object
- #cron(timing, *args, &block) ⇒ Object
- #every(timing, *args, &block) ⇒ Object
Class Attribute Details
.singleton ⇒ Object (readonly)
Returns the value of attribute singleton.
16 17 18 |
# File 'lib/gaspar.rb', line 16 def singleton @singleton end |
Instance Attribute Details
#drift ⇒ Object (readonly)
Returns the value of attribute drift.
11 12 13 |
# File 'lib/gaspar.rb', line 11 def drift @drift end |
#scheduler ⇒ Object (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( = {}, &block) @singleton ||= new(, &block) self end |
.configured? ⇒ Boolean
Public: Get whether Gaspar has been configured yet or not
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, ) logger.debug "[%s] %s" % ["Gaspar".yellow, ] if logger end |
.retire ⇒ Object
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) = args. cron = Rufus::CronLine.new(timing) next_fire = cron.next_time next_next_fire = cron.next_time(next_fire + 0.001) [:period] = next_next_fire.to_i - next_fire.to_i schedule :cron, timing, args, , &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) = args. # 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)) ) = .merge(:first_at => start_at) [:period] = seconds schedule :every, timing, args, , &block end |