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

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

Instance Method Summary collapse

Instance Method Details

#activate!(categories) ⇒ Object



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

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



75
76
77
78
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 75

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

#delayed_jobs_shardsObject



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

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.where(id: ::Switchman::Shard.default.id) if @jobs_scope_empty

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

#periodic_clear_shard_cacheObject



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

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



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

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