Module: SwitchmanInstJobs::Switchman::Shard::ClassMethods

Defined in:
lib/switchman_inst_jobs/switchman/shard.rb

Instance Method Summary collapse

Instance Method Details

#activate!(categories) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 78

def activate!(categories)
  if !@skip_delayed_job_auto_activation &&
     !categories[::Delayed::Backend::ActiveRecord::AbstractJob] &&
     categories[::ActiveRecord::Base] &&
     categories[::ActiveRecord::Base] != ::Switchman::Shard.current(::ActiveRecord::Base)
    skip_delayed_job_auto_activation do
      categories[::Delayed::Backend::ActiveRecord::AbstractJob] =
        categories[::ActiveRecord::Base].delayed_jobs_shard
    end
  end
  super
end

#clear_cacheObject



73
74
75
76
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 73

def clear_cache
  super
  remove_instance_variable(:@delayed_jobs_shards) if instance_variable_defined?(:@delayed_jobs_shards)
end

#delayed_jobs_shardsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 107

def delayed_jobs_shards
  return none unless ::Switchman::Shard.columns_hash.key?('delayed_jobs_shard_id')

  scope = ::Switchman::Shard.unscoped.
    where(id: ::Switchman::Shard.unscoped.distinct.where.not(delayed_jobs_shard_id: nil).
    select(:delayed_jobs_shard_id))
  db_jobs_shards = ::Switchman::DatabaseServer.all.map { |db| db.config[:delayed_jobs_shard] }.uniq
  db_jobs_shards.delete(nil)
  has_self = db_jobs_shards.delete('self')
  scope = scope.or(::Switchman::Shard.unscoped.where(id: db_jobs_shards)) unless db_jobs_shards.empty?

  if has_self
    self_dbs = ::Switchman::DatabaseServer.all.
      select { |db| db.config[:delayed_jobs_shard] == 'self' }.map(&:id)
    scope = scope.or(::Switchman::Shard.unscoped.
      where(id: ::Switchman::Shard.unscoped.where(delayed_jobs_shard_id: nil, database_server_id: self_dbs).
      select(:id)))
  end
  @jobs_scope_empty = !scope.exists? unless instance_variable_defined?(:@jobs_scope_empty)
  return [::Switchman::Shard.default] if @jobs_scope_empty

  ::Switchman::Shard.merge(scope)
end

#periodic_clear_shard_cacheObject



99
100
101
102
103
104
105
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 99

def periodic_clear_shard_cache
  # TODO: make this configurable
  @timed_cache ||= TimedCache.new(-> { 60.to_i.seconds.ago }) do
    ::Switchman::Shard.clear_cache
  end
  @timed_cache.clear
end

#skip_delayed_job_auto_activationObject



91
92
93
94
95
96
97
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 91

def skip_delayed_job_auto_activation
  was = @skip_delayed_job_auto_activation
  @skip_delayed_job_auto_activation = true
  yield
ensure
  @skip_delayed_job_auto_activation = was
end