Sha256: 7db3263307fbf62980d11ab9a0e2a6e7027737aba59bb0ec7154260b55916589

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module BackgroundWorker
  class WorkerExecution
    attr_reader :worker, :method_name, :options

    def initialize(worker, method_name, options)
      fail ArgumentError, "'uid' is required to identify worker" unless options['uid'].present?
      @worker = worker
      @method_name = method_name
      @options = options
    end

    def call
      worker.send(method_name, options)
      report_implicitly_successful unless completed?

    rescue StandardError => e
      log_worker_error(e)
      BackgroundWorker.after_exception.call(e)

    ensure
      log_worker_finality
    end

    private

    def completed?
      worker.state.completed
    end

    def report_implicitly_successful
      worker.report_successful
    end

    def log_worker_error(e)
      worker.log("Implicit failure: Exception: #{e}", severity: :error)
      worker.report_failed("An unhandled error occurred: #{e}") unless completed?
    end

    def log_worker_finality
      worker.log "Final state: #{worker.state.data}"
      worker.log "Job was #{worker.state.status}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
background_worker-0.0.4 lib/background_worker/worker_execution.rb
background_worker-0.0.3 lib/background_worker/worker_execution.rb