Sha256: a7a37d114dbb96db64b6ee92a80aa40d776e702b14fd87aec1a837ad51c351a7

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require 'active_support/concern'

module BackgroundWorker
  module State
    extend ActiveSupport::Concern
    attr_accessor :state

    # Report progress...
    def report_progress(message)
      state.message = message
      state.save
    end

    # Report a minor progress -- may get called a lot, so don't save it so often
    def report_minor_progress(message)
      state.message = message

      # Only report minor events once per second
      @last_report ||= Time.now - 2
      time_elapsed =  Time.now - @last_report
      return unless time_elapsed > 1

      @last_report = Time.now
      state.save
    end

    def report_successful(message = 'Finished successfully')
      state.set_completed(message, :successful)
    end

    def report_failed(message = 'Failed', detailed_message = nil)
      state.detailed_message = detailed_message
      state.set_completed(message, :failed)
    end

    def state
      # Store state persistently, to enable status checkups & progress reporting
      @state ||= BackgroundWorker::PersistentState.new(job_id, @options)
    end

    module ClassMethods
      def get_state_of(job_id)
        BackgroundWorker::PersistentState.get_state_of(job_id)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
background_worker-0.9.0 lib/background_worker/state.rb