lib/rekiq/job.rb in rekiq-0.0.1 vs lib/rekiq/job.rb in rekiq-0.5.0

- old
+ new

@@ -1,81 +1,80 @@ require 'yaml' -require 'rekiq/schedule_format_validator' module Rekiq class Job - include ActiveModel::Validations - include ActiveModel::Validations::Callbacks + include Validator - attr_accessor :schedule, :shift, :reschedule_post_work, :schedule_expired, + attr_accessor :schedule, :shift, :schedule_post_work, :schedule_expired, :expiration_margin - validates :schedule, 'rekiq::_schedule_format' => true - validates :shift, numericality: true - validates :reschedule_post_work, :schedule_expired, - inclusion: { in: [true, false], allow_nil: true } - validates :expiration_margin, - numericality: { greater_than_or_equal_to: 0, allow_nil: true } + validate :schedule, :schedule + validate :shift, :numeric, allow_nil: true + validate :schedule_post_work, :bool, allow_nil: true + validate :schedule_expired, :numeric, allow_nil: true + validate :expiration_margin, :bool, allow_nil: true - def self.from_short_key_hash(hash) - hash['schedule'] = YAML::load(hash['sch']) - hash['shift'] = hash['sft'] - hash['reschedule_post_work'] = hash['rpw'] - hash['schedule_expired'] = hash['se'] - hash['expiration_margin'] = hash['em'] + def self.from_array(array) + attributes = {}.tap do |hash| + hash['schedule'] = YAML::load(array[0]) + hash['shift'] = array[1] + hash['schedule_post_work'] = array[2] + hash['schedule_expired'] = array[3] + hash['expiration_margin'] = array[4] + end - new(hash) + new(attributes) end def initialize(attributes = {}) - self.schedule = attributes['schedule'] - self.shift = attributes['shift'] || 0 - self.reschedule_post_work = attributes['reschedule_post_work'] - self.schedule_expired = attributes['schedule_expired'] - self.expiration_margin = attributes['expiration_margin'] + self.schedule = attributes['schedule'] + self.shift = attributes['shift'] + self.schedule_post_work = attributes['schedule_post_work'] + self.schedule_expired = attributes['schedule_expired'] + self.expiration_margin = attributes['expiration_margin'] end - def to_short_key_hash - { - 'sch' => YAML::dump(schedule), - 'sft' => shift, - 'rpw' => reschedule_post_work, - 'se' => schedule_expired, - 'em' => expiration_margin - } + def to_array + [ + YAML::dump(schedule), + shift, + schedule_post_work, + schedule_expired, + expiration_margin + ] end def next_work_time(from = Time.now) - shifted_from = shift > 0 ? from - shift : from + from_with_shift = shift_val > 0 ? from - shift_val : from - search_next_work_time(shifted_from) + search_next_work_time(from_with_shift) end def next_work_time_from_work_time(from) - shifted_from = from - shift + from_with_shift = from - shift_val - search_next_work_time(shifted_from) + search_next_work_time(from_with_shift) end - def reschedule_post_work? - unless reschedule_post_work.nil? - reschedule_post_work + def schedule_post_work? + unless schedule_post_work.nil? + schedule_post_work else - Rekiq.configuration.reschedule_post_work + Rekiq.configuration.schedule_post_work end end private def search_next_work_time(from) if schedule_expired? from = schedule.next_occurrence(from) - work_time = from.nil? ? nil : from + shift + work_time = from.nil? ? nil : from + shift_val else begin from = schedule.next_occurrence(from) - work_time = from.nil? ? nil : from + shift + work_time = from.nil? ? nil : from + shift_val end until work_time.nil? || work_time > expiration_time end work_time end @@ -91,9 +90,17 @@ def expiration_margin_val unless expiration_margin.nil? expiration_margin else Rekiq.configuration.expiration_margin + end + end + + def shift_val + unless shift.nil? + shift + else + Rekiq.configuration.shift end end def expiration_time Time.now - expiration_margin_val \ No newline at end of file