Sha256: 510581f7f5cc020372e43f40020cb6929872eee10425d5cd1218c5cb63774a8d

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Massive
  class Process
    include Mongoid::Document
    include Mongoid::Timestamps

    field :cancelled_at, type: Time

    embeds_many :steps, class_name: 'Massive::Step'

    def self.find_step(process_id, step_id)
      find(process_id).steps.find(step_id)
    end

    def self.find_job(process_id, step_id, job_id)
      find_step(process_id, step_id).jobs.find(job_id)
    end

    def enqueue_next
      next_step.try(:enqueue)
    end

    def next_step
      step = steps.not_completed.not_started.first
      step.try(:enqueued?) ? nil : step
    end

    def processed_percentage
      total_weight > 0 ? total_steps_processed_percentage.to_f / total_weight : 0
    end

    def completed?
      steps.not_completed.none?
    end

    def failed?
      steps.any? { |step| step.failed_at? }
    end

    def cancelled?
      cancelled_at? || redis.exists(cancelled_key)
    end

    def cancel
      self.cancelled_at = Time.now
      redis.setex(cancelled_key, 1.day, true)
      save
    end

    def active_model_serializer
      super || Massive::ProcessSerializer
    end

    protected
      def redis
        Massive.redis
      end

      def cancelled_key
        "#{self.class.name.underscore}:#{id}:cancelled"
      end

    private
      def total_weight
        steps.map(&:weight).sum
      end

      def total_steps_processed_percentage
        steps.inject(0) do |result, step|
          result += step.processed_percentage * step.weight
        end
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
massive-0.3.0 lib/massive/process.rb
massive-0.2.0 lib/massive/process.rb
massive-0.1.1 lib/massive/process.rb