Sha256: a5b94552b5ce24bbbe9580a3bda18da86cedca490ee1385d901f1b0c39121b68

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require_relative 'logging'
require 'pstore'

class Belated
  # The worker class that actually gets the jobs from the queue
  # and calls them. Expects the jobs to be procs or
  # classes that have a perform method.
  class Worker
    include Logging

    def initialize(number: 1)
      @number = number
      start_working
    end

    def start_working
      loop do
        log "Worker #{@number} fetching jobs!"
        next unless (job = Belated.fetch_job)

        break if job.is_a?(Symbol)

        log "Worker #{@number} got job: #{job.inspect}"
        log job.perform
        history_insert(job) unless job.proc_klass || !job.completed
      rescue DRb::DRbConnError, Errno::ECONNREFUSED, RangeError => e
        log e
      end
    end

    private

    def history_insert(job)
      store.transaction do
        store[job.id] = job
      end
    rescue StandardError => e
      error e
    end

    def store
      today = Time.now.strftime('%F')
      return @store if @store&.path&.include?(today)

      @store = PStore.new("history_#{Belated.environment}-#{today}.pstore", true)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
belated-0.8.3 lib/belated/worker.rb