Sha256: f80eb1c480a1dbe896583f86379baafd1f20e78ae708d13f0601b844b8fb3fe3

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

module Marty
  module BackgroundJob
    class Schedule < Marty::Base
      self.table_name = 'marty_background_job_schedules'

      # Copied and adjusted:
      # https://github.com/javan/whenever/blob/e4507e2ed2158c603f0c334a8b0a957711db343a/lib/whenever/cron.rb
      REGEX = %r{\A(((\*?[\d/,\-]*)\s){3,4}(\*?([\d/,\-])*\s)(\*?([\d/,\-])*))\z}i

      validates :job_class, :cron, :state, presence: true
      validates :cron, format: { with: REGEX }

      validate :job_class_validation
      validate :arguments_array_validation
      validate :job_class_uniqueness_validation

      has_one :delayed_job, class_name: '::Delayed::Job', dependent: :destroy

      ALL_STATES = %w[on off].freeze
      enum state: ALL_STATES.zip(ALL_STATES).to_h

      scope :by_arguments, lambda { |arguments|
        where('arguments = ?', arguments.to_json)
      }

      def job_class_validation
        job_class.constantize.respond_to?(:schedule)
      rescue NameError
        errors.add(:job_class, "doesn't exist")
        false
      end

      def arguments_array_validation
        return if arguments.is_a? Array

        errors.add(:arguments, 'must be an Array')
        false
      end

      def job_class_uniqueness_validation
        return unless self.class.by_arguments(arguments).
                        where.not(id: id).
                        where(job_class: job_class).any?

        errors.add(:arguments, 'are not unique')
        false
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
marty-14.3.0 app/models/marty/background_job/schedule.rb
marty-14.0.0 app/models/marty/background_job/schedule.rb
marty-13.0.2 app/models/marty/background_job/schedule.rb
marty-11.0.0 app/models/marty/background_job/schedule.rb
marty-10.0.3 app/models/marty/background_job/schedule.rb
marty-10.0.2 app/models/marty/background_job/schedule.rb
marty-10.0.0 app/models/marty/background_job/schedule.rb