Module: SwitchmanInstJobs::Delayed::Backend::Base

Defined in:
lib/switchman_inst_jobs/delayed/backend/base.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



68
69
70
71
72
73
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 68

def self.prepended(base)
  base.singleton_class.prepend(ClassMethods)
  return unless base.name == "Delayed::Backend::ActiveRecord::Job"

  ::Delayed::Backend::ActiveRecord::AbstractJob.sharded_model
end

Instance Method Details

#current_shardObject



75
76
77
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 75

def current_shard
  @current_shard ||= ::Switchman::Shard.lookup(shard_id)
end

#current_shard=(shard) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 79

def current_shard=(shard)
  @current_shard = nil
  self.shard_id = shard.id
  self.shard_id = nil if shard.is_a?(::Switchman::DefaultShard)
  # If jobs are held for a shard, enqueue new ones as held as well
  return unless ::Switchman::Shard.columns_hash.key?("jobs_held") && shard.jobs_held

  self.locked_by = ::Delayed::Backend::Base::ON_HOLD_LOCKED_BY
  self.locked_at = ::Delayed::Job.db_time_now
  self.attempts = ::Delayed::Backend::Base::ON_HOLD_COUNT
end

#deserialize(source) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 103

def deserialize(source)
  raise ShardNotFoundError, shard_id unless current_shard

  current_shard.activate { super }
rescue PG::ConnectionBad, PG::UndefinedTable
  # likely a missing shard with a stale cache
  current_shard.send(:clear_cache)
  ::Switchman::Shard.clear_cache
  raise ShardNotFoundError, shard_id unless ::Switchman::Shard.exists?(id: shard_id)

  raise
end

#invoke_jobObject

Raises:



91
92
93
94
95
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 91

def invoke_job
  raise ShardNotFoundError, shard_id unless current_shard

  current_shard.activate { super }
end

#invoke_payload_object_cbObject

Raises:



97
98
99
100
101
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 97

def invoke_payload_object_cb(...)
  raise ShardNotFoundError, shard_id unless current_shard

  current_shard.activate { super }
end