class Belated
Belated
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
- URI
- VERSION
Public Class Methods
clear_queue!()
click to toggle source
# File lib/belated.rb, line 161 def clear_queue! @@queue.clear end
delete(job_id)
click to toggle source
# File lib/belated.rb, line 149 def delete(job_id) job = find(job_id) @@queue.delete_job(job) end
fetch_job()
click to toggle source
# File lib/belated.rb, line 165 def fetch_job @@queue.pop end
find(job_id)
click to toggle source
# File lib/belated.rb, line 145 def find(job_id) @@queue.find(job_id) end
job_list()
click to toggle source
# File lib/belated.rb, line 174 def self.job_list @@queue end
kill_and_clear_queue!()
click to toggle source
# File lib/belated.rb, line 154 def kill_and_clear_queue! @worker_list&.each do |worker| Thread.kill(worker) end clear_queue! end
Public Instance Methods
boot_app()
click to toggle source
# File lib/belated.rb, line 85 def boot_app return unless rails? ENV['RAILS_ENV'] ||= Belated.config.environment require File.expand_path("#{Belated.config.rails_path}/config/environment.rb") require 'rails/all' require 'belated/rails' require 'active_job/queue_adapters/belated_adapter' end
connect!()
click to toggle source
Handles connection to DRb server.
# File lib/belated.rb, line 59 def connect! i = 0 DRb.start_service(URI, @@queue, verbose: true) rescue DRb::DRbConnError, Errno::EADDRINUSE sleep 0.1 and retry if (i += 1) < 5 error 'Could not connect to DRb server.' end
job_list()
click to toggle source
# File lib/belated.rb, line 170 def job_list @@queue end
rails?()
click to toggle source
# File lib/belated.rb, line 95 def rails? Belated.config.rails end
reload()
click to toggle source
# File lib/belated.rb, line 99 def reload log 'reloading...' @@queue.load_jobs end
start()
click to toggle source
Since it's running as a singleton, we need something to start it up. Aliased for testing purposes. This is only run from the bin file.
# File lib/belated.rb, line 43 def start boot_app && @@queue.load_jobs @worker_list = [] Belated.config.workers.times do |i| @worker_list << Thread.new { Worker.new(number: i.next) } end return unless Belated.config.connect connect! banner_and_info trap_signals @@queue.enqueue_future_jobs end
Also aliased as: initialize
stats()
click to toggle source
# File lib/belated.rb, line 137 def stats { jobs: @@queue.size, workers: @worker_list&.length } end
stop_workers()
click to toggle source
# File lib/belated.rb, line 104 def stop_workers @worker_list&.each do |worker| i = 0 sleep 0.1 while worker.alive? || (i + 0.1) < 10 Thread.kill(worker) end @@queue.save_jobs exit unless $TESTING end
trap_signals()
click to toggle source
# File lib/belated.rb, line 67 def trap_signals pp 'trap' %w[INT TERM].each do |signal| Signal.trap(signal) do @worker_list.length.times do @@queue.push(:shutdown) end Thread.new { stop_workers } # Max 40 seconds to shutdown timeout = 0 until (timeout += 0.1) >= 40 || @@queue.empty? || $TESTING sleep 0.1 end exit end end end