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

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

Instance Method Summary collapse

Instance Method Details

#activate!(categories) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 86

def activate!(categories)
  if !@skip_delayed_job_auto_activation &&
     !categories[:delayed_jobs] &&
     categories[:primary] &&
     categories[:primary] != active_shards[:primary]
    skip_delayed_job_auto_activation do
      categories[:delayed_jobs] =
        categories[:primary].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

#createObject



107
108
109
110
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 107

def create
  db = ::Switchman::DatabaseServer.server_for_new_shard
  db.create_new_shard
end

#current(category = :primary) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 78

def current(category = :primary)
  if category == :delayed_jobs
    active_shards[category] || super(:primary).delayed_jobs_shard
  else
    super
  end
end

#delayed_jobs_shardsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 120

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



112
113
114
115
116
117
118
# File 'lib/switchman_inst_jobs/switchman/shard.rb', line 112

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



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

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