class HardWorker

HardWorker is a pure Ruby job backend. It has limited functionality, as it only accepts jobs as procs, but that might make it useful if you don't need anything as big as Redis. Saves jobs into a file as YAML as long as they're not procs and reloads them when started again.

Constants

FILE_NAME
URI
VERSION

Public Class Methods

clear_queue!() click to toggle source
# File lib/hard_worker.rb, line 131
def self.clear_queue!
  @@queue.clear
end
fetch_job() click to toggle source
# File lib/hard_worker.rb, line 139
def self.fetch_job
  @@queue.pop
end
stop_workers() click to toggle source
# File lib/hard_worker.rb, line 86
def self.stop_workers
  @worker_list&.each do |worker|
    Thread.kill(worker)
  end
  class_array = []
  @@queue.size.times do |_i|
    next if (klass_or_proc = @@queue.pop).instance_of?(Proc)

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

Public Instance Methods

banner() click to toggle source

rubocop:disable Layout/LineLength rubocop:disable Metrics/MethodLength

banner_and_info() click to toggle source
boot_app() click to toggle source
# File lib/hard_worker.rb, line 46
def boot_app
  return unless rails?

  ENV['RAILS_ENV'] ||= HardWorker.config.environment
  require File.expand_path("#{HardWorker.config.rails_path}/config/environment.rb")
  require 'rails/all'
  require 'hard_worker/rails'
end
initialize()
Alias for: start
job_list() click to toggle source
# File lib/hard_worker.rb, line 135
def job_list
  @@queue
end
load_jobs() click to toggle source
# File lib/hard_worker.rb, line 59
def load_jobs
  jobs = YAML.load(File.binread(FILE_NAME))
  jobs.each do |job|
    @@queue.push(job)
  end
  File.delete(HardWorker::FILE_NAME) if File.exist?(HardWorker::FILE_NAME)
rescue StandardError
  # do nothing
end
rails?() click to toggle source
# File lib/hard_worker.rb, line 55
def rails?
  HardWorker.config.rails
end
reload() click to toggle source
# File lib/hard_worker.rb, line 69
def reload
  load_jobs
end
start() click to toggle source
# File lib/hard_worker.rb, line 31
def start
  boot_app
  load_jobs
  @worker_list = []
  HardWorker.config.workers.times do |_i|
    @worker_list << Thread.new { Worker.new }
  end
  return unless HardWorker.config.connect

  DRb.start_service(URI, @@queue, verbose: true)
  puts banner_and_info
  DRb.thread.join
end
Also aliased as: initialize
stats() click to toggle source
# File lib/hard_worker.rb, line 124
def stats
  {
    jobs: @@queue.size,
    workers: @worker_list&.length
  }
end
stop_workers() click to toggle source
# File lib/hard_worker.rb, line 73
def stop_workers
  @worker_list.each do |worker|
    Thread.kill(worker)
  end
  class_array = []
  @@queue.size.times do |_i|
    next if (klass_or_proc = @@queue.pop).instance_of?(Proc)

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