lib/clockwork/event.rb in clockwork-2.0.2 vs lib/clockwork/event.rb in clockwork-2.0.3

- old
+ new

@@ -6,24 +6,28 @@ validate_if_option(options[:if]) @manager = manager @period = period @job = job @at = At.parse(options[:at]) - @last = nil @block = block @if = options[:if] @thread = options.fetch(:thread, @manager.config[:thread]) @timezone = options.fetch(:tz, @manager.config[:tz]) + @skip_first_run = options[:skip_first_run] + @last = @skip_first_run ? convert_timezone(Time.now) : nil end def convert_timezone(t) - @timezone ? t.in_time_zone(@timezone) : t + @timezone ? t.getlocal(TZInfo::Timezone.get(@timezone).period_for_utc(t).utc_total_offset) : t end def run_now?(t) t = convert_timezone(t) - elapsed_ready(t) and (@at.nil? or @at.ready?(t)) and (@if.nil? or @if.call(t)) + return false unless elapsed_ready?(t) + return false unless run_at?(t) + return false unless run_if?(t) + true end def thread? @thread end @@ -55,11 +59,19 @@ rescue => e @manager.log_error e @manager.handle_error e end - def elapsed_ready(t) + def elapsed_ready?(t) @last.nil? || (t - @last.to_i).to_i >= @period + end + + def run_at?(t) + @at.nil? || @at.ready?(t) + end + + def run_if?(t) + @if.nil? || @if.call(t) end def validate_if_option(if_option) if if_option && !if_option.respond_to?(:call) raise ArgumentError.new(':if expects a callable object, but #{if_option} does not respond to call')