Sha256: 6e96430e949168b2a62f441405e5f877c07423eede9dbc4add46eba2da8cca6f

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'belated/job'
require 'belated/logging'
class Belated
  class Queue
    include Logging
    attr_accessor :future_jobs

    FILE_NAME = 'belated_dump'

    def initialize(queue: Thread::Queue.new, future_jobs: [])
      @queue = queue
      self.future_jobs = future_jobs
    end

    def push(job, at: nil)
      if at.nil?
        @queue.push(job)
      else
        @future_jobs << Job.new(job, at)
      end
    end

    def pop
      @queue.pop
    end

    def clear
      @queue.clear
      self.future_jobs = []
    end

    def empty?
      @queue.empty?
    end

    def length
      @queue.length
    end

    def load_jobs
      log "reloading... if file exists #{File.exist?(FILE_NAME)}"
      return unless File.exist?(FILE_NAME)

      jobs = YAML.load(File.binread(FILE_NAME))
      jobs.each do |job|
        if job.is_a?(Job)
          future_jobs.push(job)
        else
          @queue.push(job)
        end
      end
      File.delete(FILE_NAME)
    end

    def save_jobs
      class_array = []
      @queue.length.times do |_i|
        unless proc_or_shutdown?(klass = @queue.pop)
          class_array << klass
        end
      end
      future_jobs.each do |_job|
        unless proc_or_shutdown?(klass = future_jobs.pop)
          class_array << klass
        end
      end

      pp File.open(FILE_NAME, 'wb') { |f| f.write(YAML.dump(class_array)) }
    end

    private

    def proc_or_shutdown?(job)
      job.instance_of?(Proc) || job == :shutdown
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
belated-0.4.4 lib/belated/queue.rb
belated-0.4.3 lib/belated/queue.rb
belated-0.4.2 lib/belated/queue.rb
belated-0.4.1 lib/belated/queue.rb