Sha256: 0ff41075e86f45ca619ac99158b8aacfd31814158afc0e41410544398e811754
Contents?: true
Size: 1000 Bytes
Versions: 1
Compression:
Stored size: 1000 Bytes
Contents
# frozen_string_literal: true require_relative "db_background_job/version" module DbBackgroundJob class Error < StandardError; end def self.spawn(&block) with_db_connection do pid = Process.fork do setup_child(&block) end Process.detach(pid) end end def self.spawn_and_wait(&block) with_db_connection do pid = Process.fork do begin setup_child(&block) exit(0) rescue Exception => e puts "Error in background job: #{e.inspect}" exit(1) end end _, status = Process.wait2(pid) status.exitstatus end end private def self.with_db_connection(&block) dbconfig = ActiveRecord::Base.remove_connection begin yield ensure ActiveRecord::Base.establish_connection(dbconfig) end end def self.setup_child(&block) ActiveRecord::Base.establish_connection block.call ensure ActiveRecord::Base.remove_connection end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
db_background_job-0.1.0 | lib/db_background_job.rb |