Sha256: a13ec30dce069a6d4d25e4f6442e19c67a4d9f0563fb553433038d8d4697aaaa

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 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 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

1 entries across 1 versions & 1 rubygems

Version Path
massive-0.1.0 lib/massive/process.rb