Skip to content

Commit

Permalink
Add database primary replica configuration for sidekiqs
Browse files Browse the repository at this point in the history
  • Loading branch information
praveendhawan authored and ikirifqi committed Apr 12, 2024
1 parent eab03cf commit 35e8ccf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
9 changes: 7 additions & 2 deletions libraries/drivers_worker_sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def add_replica_config
Chef::Log.info('Rewrite Sidekiq database.yml for read-replica')

deploy = node['deploy'][app['shortname']]
database = deploy['database']
replica_database = deploy['sidekiq_on_replica']&.dup
replica_database = database.dup if replica_database.nil?
replica_database[:replica] = true

Chef::Log.info(deploy.inspect)
context.template "#{deploy_dir(app)}/shared/config/database.yml" do
source 'sidekiq_database.yml.erb'
Expand All @@ -141,9 +146,9 @@ def add_replica_config
group node['deployer']['group']
owner node['deployer']['user']
variables(
database: deploy['database'],
database: database,
environment: deploy['global']['environment'],
sidekiq_on_replica: deploy['sidekiq_on_replica']
sidekiq_on_replica: replica_database,
)
end
end
Expand Down
32 changes: 18 additions & 14 deletions sidekiq_custom/templates/default/sidekiq_database.yml.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<% (['development', 'production'] + [@environment]).uniq.each do |env| -%>
<%= env %>:
database: <%= @database[:database].to_s.inspect %>
encoding: <%= (@database[:encoding] || 'utf8').to_s.inspect %>
adapter: <%= @database[:adapter].to_s.inspect %>
host: <%= (@database[:host] || 'localhost').to_s.inspect %>
username: <%= @database[:username].to_s.inspect %>
password: <%= @database[:password].to_s.inspect %>
reconnect: <%= @database[:reconnect] ? 'true' : 'false' %>
<%- if @database[:pool] -%>
pool: <%= @database[:pool].to_i.inspect %>
<%- end -%>
<%- if @database[:port] -%>
port: <%= @database[:port].to_i.inspect %>
<%- end -%>
primary:
database: <%= @database[:database].to_s.inspect %>
encoding: <%= (@database[:encoding] || 'utf8').to_s.inspect %>
adapter: <%= @database[:adapter].to_s.inspect %>
host: <%= (@database[:host] || 'localhost').to_s.inspect %>
username: <%= @database[:username].to_s.inspect %>
password: <%= @database[:password].to_s.inspect %>
reconnect: <%= @database[:reconnect] ? 'true' : 'false' %>
<%- if @database[:pool] -%>
pool: <%= @database[:pool].to_i.inspect %>
<%- end -%>
<%- if @database[:port] -%>
port: <%= @database[:port].to_i.inspect %>
<%- end -%>
<%- if @sidekiq_on_replica.present? -%>
replica:
primary_replica:
host: <%= (@sidekiq_on_replica[:host] || 'localhost').to_s.inspect %>
username: <%= @sidekiq_on_replica[:username].to_s.inspect %>
password: <%= @sidekiq_on_replica[:password].to_s.inspect %>
Expand All @@ -24,5 +25,8 @@
<%- if @sidekiq_on_replica[:port] -%>
port: <%= @sidekiq_on_replica[:port].to_i.inspect %>
<%- end -%>
<%- if @sidekiq_on_replica[:replica] -%>
replica: true
<%- end -%>
<% end -%>
<%- end -%>

0 comments on commit 35e8ccf

Please sign in to comment.