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



61
62
63
64
65
66
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 61

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



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

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

#current_shard=(shard) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 72

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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 90

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:



84
85
86
87
88
# File 'lib/switchman_inst_jobs/delayed/backend/base.rb', line 84

def invoke_job
  raise ShardNotFoundError, shard_id unless current_shard

  current_shard.activate { super }
end